2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-11 14:43:57 +01:00
|
|
|
use Amp\Failure;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class PromiseMock
|
|
|
|
{
|
2017-03-10 21:58:46 +01:00
|
|
|
/** @var \Amp\Promise */
|
2016-11-14 20:59:21 +01:00
|
|
|
private $promise;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __construct(Promise $promise)
|
|
|
|
{
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->promise = $promise;
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function then(callable $onFulfilled = null, callable $onRejected = null)
|
|
|
|
{
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->promise->onResolve(function ($exception, $value) use ($onFulfilled, $onRejected) {
|
2016-07-12 18:20:06 +02:00
|
|
|
if ($exception) {
|
|
|
|
if ($onRejected) {
|
|
|
|
$onRejected($exception);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($onFulfilled) {
|
|
|
|
$onFulfilled($value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class AdaptTest extends \PHPUnit\Framework\TestCase
|
|
|
|
{
|
|
|
|
public function testThenCalled()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$mock = $this->getMockBuilder(PromiseMock::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$mock->expects($this->once())
|
|
|
|
->method("then")
|
|
|
|
->with(
|
|
|
|
$this->callback(function ($resolve) {
|
2017-11-29 13:36:50 +01:00
|
|
|
return \is_callable($resolve);
|
2016-07-12 18:20:06 +02:00
|
|
|
}),
|
|
|
|
$this->callback(function ($reject) {
|
2017-11-29 13:36:50 +01:00
|
|
|
return \is_callable($reject);
|
2016-07-12 18:20:06 +02:00
|
|
|
})
|
|
|
|
);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$promise = Promise\adapt($mock);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
|
|
|
* @depends testThenCalled
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromiseFulfilled()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$promise = new PromiseMock(new Success($value));
|
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$promise = Promise\adapt($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$result) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
|
|
|
* @depends testThenCalled
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromiseRejected()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$promise = new PromiseMock(new Failure($exception));
|
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$promise = Promise\adapt($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @expectedException \Error
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testScalarValue()
|
|
|
|
{
|
2017-03-15 17:12:49 +01:00
|
|
|
Promise\adapt(1);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @expectedException \Error
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testNonThenableObject()
|
|
|
|
{
|
2017-03-15 17:12:49 +01:00
|
|
|
Promise\adapt(new \stdClass);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
}
|