1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00
amp/test/DeferredTest.php

64 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp\Deferred;
use Amp\Promise;
2016-07-12 18:20:06 +02:00
2018-06-18 20:00:01 +02:00
class DeferredTest extends \PHPUnit\Framework\TestCase
{
2016-08-18 05:25:54 +02:00
/** @var \Amp\Deferred */
2016-07-12 18:20:06 +02:00
private $deferred;
2018-06-18 20:00:01 +02:00
public function setUp()
{
2016-07-12 18:20:06 +02:00
$this->deferred = new Deferred;
}
2018-06-18 20:00:01 +02:00
public function testGetPromise()
{
2016-11-14 20:59:21 +01:00
$promise = $this->deferred->promise();
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 18:20:06 +02:00
}
2016-07-12 18:20:06 +02:00
/**
2016-11-14 20:59:21 +01:00
* @depends testGetPromise
2016-07-12 18:20:06 +02:00
*/
2018-06-18 20:00:01 +02:00
public function testResolve()
{
2016-07-12 18:20:06 +02:00
$value = "Resolution value";
2016-11-14 20:59:21 +01:00
$promise = $this->deferred->promise();
2016-07-12 18:20:06 +02:00
$invoked = false;
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
2016-07-12 18:20:06 +02:00
$invoked = true;
$result = $value;
});
2016-07-12 18:20:06 +02:00
$this->deferred->resolve($value);
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
/**
2016-11-14 20:59:21 +01:00
* @depends testGetPromise
2016-07-12 18:20:06 +02:00
*/
2018-06-18 20:00:01 +02:00
public function testFail()
{
2016-07-12 18:20:06 +02:00
$exception = new \Exception;
2016-11-14 20:59:21 +01:00
$promise = $this->deferred->promise();
2016-07-12 18:20:06 +02:00
$invoked = false;
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
2016-07-12 18:20:06 +02:00
$invoked = true;
$result = $exception;
});
$this->deferred->fail($exception);
$this->assertTrue($invoked);
$this->assertSame($exception, $result);
}
}