mirror of
https://github.com/danog/amp.git
synced 2024-11-26 20:15:00 +01:00
c12828081f
This has been an edge case potentially hiding some exceptions. The tests have been refactored to error if the loop has watchers leaking from one test to another test.
64 lines
1.3 KiB
PHP
64 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Amp\Test;
|
|
|
|
use Amp\Deferred;
|
|
use Amp\Promise;
|
|
|
|
class DeferredTest extends BaseTest
|
|
{
|
|
/** @var \Amp\Deferred */
|
|
private $deferred;
|
|
|
|
public function setUp()
|
|
{
|
|
$this->deferred = new Deferred;
|
|
}
|
|
|
|
public function testGetPromise()
|
|
{
|
|
$promise = $this->deferred->promise();
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
}
|
|
|
|
/**
|
|
* @depends testGetPromise
|
|
*/
|
|
public function testResolve()
|
|
{
|
|
$value = "Resolution value";
|
|
$promise = $this->deferred->promise();
|
|
|
|
$invoked = false;
|
|
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
|
|
$invoked = true;
|
|
$result = $value;
|
|
});
|
|
|
|
$this->deferred->resolve($value);
|
|
|
|
$this->assertTrue($invoked);
|
|
$this->assertSame($value, $result);
|
|
}
|
|
|
|
/**
|
|
* @depends testGetPromise
|
|
*/
|
|
public function testFail()
|
|
{
|
|
$exception = new \Exception;
|
|
$promise = $this->deferred->promise();
|
|
|
|
$invoked = false;
|
|
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
|
|
$invoked = true;
|
|
$result = $exception;
|
|
});
|
|
|
|
$this->deferred->fail($exception);
|
|
|
|
$this->assertTrue($invoked);
|
|
$this->assertSame($exception, $result);
|
|
}
|
|
}
|