1
0
mirror of https://github.com/danog/amp.git synced 2024-12-13 09:57:25 +01:00
amp/test/UnresolvedTest.php

101 lines
3.2 KiB
PHP
Raw Normal View History

2014-09-22 22:47:48 +02:00
<?php
2014-09-23 04:38:32 +02:00
namespace Amp\Test;
2014-09-22 22:47:48 +02:00
2014-09-23 04:38:32 +02:00
use Amp\PrivateFuture;
use Amp\Future;
use Amp\NativeReactor;
2014-09-22 22:47:48 +02:00
class UnresolvedTest extends \PHPUnit_Framework_TestCase {
public function testWatchInvokesCallbackWithResultIfAlreadySucceeded() {
2014-09-23 04:38:32 +02:00
$deferred = new PrivateFuture($this->getMock('Amp\Reactor'));
2014-09-22 22:47:48 +02:00
$promise = $deferred->promise();
$deferred->succeed(42);
$promise->watch(function($p, $e, $r) {
$this->assertSame(42, $r);
$this->assertNull($p);
$this->assertNull($e);
});
}
public function testWatchInvokesCallbackWithErrorIfAlreadyFailed() {
2014-09-23 04:38:32 +02:00
$promisor = new PrivateFuture($this->getMock('Amp\Reactor'));
2014-09-22 22:47:48 +02:00
$promise = $promisor->promise();
$exception = new \Exception('test');
$promisor->fail($exception);
$promise->watch(function($p, $e, $r) use ($exception) {
$this->assertSame($exception, $e);
$this->assertNull($p);
$this->assertNull($r);
});
}
public function testWaitReturnsOnResolution() {
$reactor = new NativeReactor;
$promisor = new PrivateFuture($reactor);
$reactor->once(function() use ($promisor) { $promisor->succeed(42); }, $msDelay = 100);
$this->assertSame(42, $promisor->promise()->wait());
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Promise already resolved
*/
public function testSucceedThrowsIfAlreadyResolved() {
2014-09-23 04:38:32 +02:00
$promisor = new PrivateFuture($this->getMock('Amp\Reactor'));
2014-09-22 22:47:48 +02:00
$promisor->succeed(42);
$promisor->succeed('zanzibar');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage A Promise cannot act as its own resolution result
*/
public function testSucceedThrowsIfPromiseIsTheResolutionValue() {
2014-09-23 04:38:32 +02:00
$promisor = new PrivateFuture($this->getMock('Amp\Reactor'));
2014-09-22 22:47:48 +02:00
$promise = $promisor->promise();
$promisor->succeed($promise);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Promise already resolved
*/
public function testFailThrowsIfAlreadyResolved() {
2014-09-23 04:38:32 +02:00
$promisor = new PrivateFuture($this->getMock('Amp\Reactor'));
2014-09-22 22:47:48 +02:00
$promisor->succeed(42);
$promisor->fail(new \Exception);
}
public function testSucceedingWithPromisePipelinesResult() {
$reactor = new NativeReactor;
$promisor = new PrivateFuture($reactor);
$next = new Future($reactor);
$reactor->once(function() use ($next) {
$next->succeed(42);
}, $msDelay = 1);
$promisor->succeed($next->promise());
$this->assertSame(42, $promisor->promise()->wait());
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage fugazi
*/
public function testFailingWithPromisePipelinesResult() {
$reactor = new NativeReactor;
$promisor = new PrivateFuture($reactor);
$next = new Future($reactor);
$reactor->once(function() use ($next) {
$next->fail(new \RuntimeException('fugazi'));
}, $msDelay = 10);
$promisor->succeed($next->promise());
$promisor->promise()->wait();
}
}