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

59 lines
1.3 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
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;
public function setUp() {
$this->deferred = new Deferred;
}
2016-11-14 20:59:21 +01:00
public function testGetPromise() {
$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
*/
public function testResolve() {
$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;
2016-11-14 20:59:21 +01:00
$promise->when(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
*/
public function testFail() {
$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;
2016-11-14 20:59:21 +01:00
$promise->when(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);
}
}