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