1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 17:37:34 +01:00
amp/test/UnresolvedTest.php

96 lines
2.8 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() {
$promisor = new PrivateFuture;
$promise = $promisor->promise();
$promisor->succeed(42);
2014-09-22 22:47:48 +02:00
$promise->watch(function($p, $e, $r) {
$this->assertSame(42, $r);
$this->assertNull($p);
$this->assertNull($e);
});
}
public function testWatchInvokesCallbackWithErrorIfAlreadyFailed() {
$promisor = new PrivateFuture;
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);
});
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Promise already resolved
*/
public function testSucceedThrowsIfAlreadyResolved() {
$promisor = new PrivateFuture;
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() {
$promisor = new PrivateFuture;
2014-09-22 22:47:48 +02:00
$promise = $promisor->promise();
$promisor->succeed($promise);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Promise already resolved
*/
public function testFailThrowsIfAlreadyResolved() {
$promisor = new PrivateFuture;
2014-09-22 22:47:48 +02:00
$promisor->succeed(42);
$promisor->fail(new \Exception);
}
public function testSucceedingWithPromisePipelinesResult() {
(new NativeReactor)->run(function($reactor) {
$promisor = new PrivateFuture;
$next = new Future;
2014-09-22 22:47:48 +02:00
$reactor->once(function() use ($next) {
$next->succeed(42);
}, $msDelay = 1);
2014-09-22 22:47:48 +02:00
$promisor->succeed($next->promise());
2014-09-22 22:47:48 +02:00
$this->assertSame(42, (yield $promisor->promise()));
});
2014-09-22 22:47:48 +02:00
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage fugazi
*/
public function testFailingWithPromisePipelinesResult() {
(new NativeReactor)->run(function($reactor) {
$promisor = new PrivateFuture;
$next = new Future;
2014-09-22 22:47:48 +02:00
$reactor->once(function() use ($next) {
$next->fail(new \RuntimeException('fugazi'));
}, $msDelay = 10);
2014-09-22 22:47:48 +02:00
$promisor->succeed($next->promise());
yield $promisor->promise();
});
2014-09-22 22:47:48 +02:00
}
}