1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/test/DeferredTest.php

63 lines
1.4 KiB
PHP
Raw Normal View History

2016-07-12 18:20:06 +02:00
<?php
declare(strict_types=1);
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp\Deferred;
use Interop\Async\Awaitable;
class DeferredTest extends \PHPUnit_Framework_TestCase {
/**
* @var \Amp\Deferred
*/
private $deferred;
public function setUp() {
$this->deferred = new Deferred;
}
public function testGetAwaitable() {
$awaitable = $this->deferred->getAwaitable();
$this->assertInstanceOf(Awaitable::class, $awaitable);
}
/**
* @depends testGetAwaitable
*/
public function testResolve() {
$value = "Resolution value";
$awaitable = $this->deferred->getAwaitable();
$invoked = false;
$awaitable->when(function ($exception, $value) use (&$invoked, &$result) {
$invoked = true;
$result = $value;
});
$this->deferred->resolve($value);
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
/**
* @depends testGetAwaitable
*/
public function testFail() {
$exception = new \Exception;
$awaitable = $this->deferred->getAwaitable();
$invoked = false;
$awaitable->when(function ($exception, $value) use (&$invoked, &$result) {
$invoked = true;
$result = $exception;
});
$this->deferred->fail($exception);
$this->assertTrue($invoked);
$this->assertSame($exception, $result);
}
}