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;
|
2017-03-12 23:27:43 -05:00
|
|
|
use function React\Promise\resolve;
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class PipeTest extends \PHPUnit\Framework\TestCase {
|
2016-11-14 13:59:21 -06:00
|
|
|
public function testSuccessfulPromise() {
|
2016-07-12 11:20:06 -05:00
|
|
|
$invoked = false;
|
|
|
|
$callback = function ($value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
return $value + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$value = 1;
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Success($value);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2017-03-15 11:12:49 -05:00
|
|
|
$promise = Promise\pipe($promise, $callback);
|
2016-11-14 13:59:21 -06:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value + 1, $result);
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
public function testFailedPromise() {
|
2016-07-12 11:20:06 -05:00
|
|
|
$invoked = false;
|
|
|
|
$callback = function ($value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
return $value + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Failure($exception);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2017-03-15 11:12:49 -05:00
|
|
|
$promise = Promise\pipe($promise, $callback);
|
2016-11-14 13:59:21 -06:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 11:20:06 -05:00
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* @depends testSuccessfulPromise
|
2016-07-12 11:20:06 -05:00
|
|
|
*/
|
|
|
|
public function testCallbackThrowing() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
$callback = function ($value) use (&$invoked, $exception) {
|
|
|
|
$invoked = true;
|
|
|
|
throw $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 11:20:06 -05:00
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Success($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 11:12:49 -05:00
|
|
|
$promise = Promise\pipe($promise, $callback);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 11:20:06 -05:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 11:20:06 -05:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-03-12 23:27:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSuccessfulPromise
|
|
|
|
*/
|
|
|
|
public function testReactPromise() {
|
|
|
|
$invoked = false;
|
|
|
|
$callback = function ($value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
return $value + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$promise = resolve($value);
|
|
|
|
|
2017-03-15 11:12:49 -05:00
|
|
|
$promise = Promise\pipe($promise, $callback);
|
2017-03-12 23:27:43 -05:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
|
|
|
$callback = function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve($callback);
|
2017-03-12 23:27:43 -05:00
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value + 1, $result);
|
|
|
|
}
|
2017-03-14 22:15:36 +01:00
|
|
|
|
|
|
|
public function testNonPromise() {
|
2017-04-23 19:08:40 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
2017-03-15 11:12:49 -05:00
|
|
|
Promise\pipe(42, function () {});
|
2017-03-14 22:15:36 +01:00
|
|
|
}
|
2016-07-12 11:20:06 -05:00
|
|
|
}
|