graphql-php/tests/Executor/Promise/SyncPromiseAdapterTest.php

223 lines
7.4 KiB
PHP
Raw Normal View History

<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Executor\Promise;
2018-09-26 12:07:23 +03:00
use Exception;
use GraphQL\Deferred;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-26 12:07:23 +03:00
use stdClass;
use Throwable;
2018-07-29 18:43:10 +03:00
class SyncPromiseAdapterTest extends TestCase
{
2018-09-01 18:07:06 +03:00
/** @var SyncPromiseAdapter */
private $promises;
public function setUp()
{
$this->promises = new SyncPromiseAdapter();
}
public function testIsThenable() : void
{
2018-09-19 18:12:09 +03:00
self::assertEquals(
2018-09-01 18:07:06 +03:00
true,
2018-09-26 12:07:23 +03:00
$this->promises->isThenable(new Deferred(static function () {
2018-09-01 18:07:06 +03:00
}))
);
2018-09-19 18:12:09 +03:00
self::assertEquals(false, $this->promises->isThenable(false));
self::assertEquals(false, $this->promises->isThenable(true));
self::assertEquals(false, $this->promises->isThenable(1));
self::assertEquals(false, $this->promises->isThenable(0));
self::assertEquals(false, $this->promises->isThenable('test'));
self::assertEquals(false, $this->promises->isThenable(''));
self::assertEquals(false, $this->promises->isThenable([]));
2018-09-26 12:07:23 +03:00
self::assertEquals(false, $this->promises->isThenable(new stdClass()));
}
public function testConvert() : void
{
2018-09-26 12:07:23 +03:00
$dfd = new Deferred(static function () {
2018-09-01 18:07:06 +03:00
});
$result = $this->promises->convertThenable($dfd);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $result);
self::assertInstanceOf('GraphQL\Executor\Promise\Adapter\SyncPromise', $result->adoptedPromise);
2018-07-29 18:43:10 +03:00
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('Expected instance of GraphQL\Deferred, got (empty string)');
$this->promises->convertThenable('');
}
public function testThen() : void
{
2018-09-26 12:07:23 +03:00
$dfd = new Deferred(static function () {
2018-09-01 18:07:06 +03:00
});
$promise = $this->promises->convertThenable($dfd);
$result = $this->promises->then($promise);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $result);
self::assertInstanceOf('GraphQL\Executor\Promise\Adapter\SyncPromise', $result->adoptedPromise);
}
public function testCreatePromise() : void
{
2018-09-26 12:07:23 +03:00
$promise = $this->promises->create(static function ($resolve, $reject) {
2018-09-01 18:07:06 +03:00
});
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $promise);
self::assertInstanceOf('GraphQL\Executor\Promise\Adapter\SyncPromise', $promise->adoptedPromise);
2018-09-26 12:07:23 +03:00
$promise = $this->promises->create(static function ($resolve, $reject) {
$resolve('A');
});
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, null, 'A', SyncPromise::FULFILLED);
}
2018-09-19 18:12:09 +03:00
private static function assertValidPromise($promise, $expectedNextReason, $expectedNextValue, $expectedNextState)
2018-09-01 18:07:06 +03:00
{
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $promise);
self::assertInstanceOf('GraphQL\Executor\Promise\Adapter\SyncPromise', $promise->adoptedPromise);
2018-09-01 18:07:06 +03:00
$actualNextValue = null;
$actualNextReason = null;
$onFulfilledCalled = false;
$onRejectedCalled = false;
$promise->then(
2018-09-26 12:07:23 +03:00
static function ($nextValue) use (&$actualNextValue, &$onFulfilledCalled) {
2018-09-01 18:07:06 +03:00
$onFulfilledCalled = true;
$actualNextValue = $nextValue;
},
2018-09-26 12:07:23 +03:00
static function (Throwable $reason) use (&$actualNextReason, &$onRejectedCalled) {
2018-09-01 18:07:06 +03:00
$onRejectedCalled = true;
$actualNextReason = $reason->getMessage();
}
);
2018-09-19 18:12:09 +03:00
self::assertSame($onFulfilledCalled, false);
self::assertSame($onRejectedCalled, false);
2018-09-01 18:07:06 +03:00
SyncPromise::runQueue();
if ($expectedNextState !== SyncPromise::PENDING) {
2018-09-19 18:12:09 +03:00
self::assertSame(! $expectedNextReason, $onFulfilledCalled);
self::assertSame(! ! $expectedNextReason, $onRejectedCalled);
2018-09-01 18:07:06 +03:00
}
2018-09-19 18:12:09 +03:00
self::assertSame($expectedNextValue, $actualNextValue);
self::assertSame($expectedNextReason, $actualNextReason);
self::assertSame($expectedNextState, $promise->adoptedPromise->state);
2018-09-01 18:07:06 +03:00
}
public function testCreateFulfilledPromise() : void
{
$promise = $this->promises->createFulfilled('test');
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, null, 'test', SyncPromise::FULFILLED);
}
public function testCreateRejectedPromise() : void
{
2018-09-26 12:07:23 +03:00
$promise = $this->promises->createRejected(new Exception('test reason'));
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, 'test reason', null, SyncPromise::REJECTED);
}
public function testCreatePromiseAll() : void
{
$promise = $this->promises->all([]);
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, null, [], SyncPromise::FULFILLED);
$promise = $this->promises->all(['1']);
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, null, ['1'], SyncPromise::FULFILLED);
$promise1 = new SyncPromise();
$promise2 = new SyncPromise();
$promise3 = $promise2->then(
2018-09-26 12:07:23 +03:00
static function ($value) {
2018-09-01 18:07:06 +03:00
return $value . '-value3';
}
);
$data = [
'1',
new Promise($promise1, $this->promises),
new Promise($promise2, $this->promises),
3,
new Promise($promise3, $this->promises),
2018-09-01 18:07:06 +03:00
[],
];
$promise = $this->promises->all($data);
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, null, null, SyncPromise::PENDING);
$promise1->resolve('value1');
2018-09-19 18:12:09 +03:00
self::assertValidPromise($promise, null, null, SyncPromise::PENDING);
$promise2->resolve('value2');
2018-09-19 18:12:09 +03:00
self::assertValidPromise(
2018-09-01 18:07:06 +03:00
$promise,
null,
['1', 'value1', 'value2', 3, 'value2-value3', []],
SyncPromise::FULFILLED
);
}
public function testWait() : void
{
$called = [];
2018-09-26 12:07:23 +03:00
$deferred1 = new Deferred(static function () use (&$called) {
$called[] = 1;
2018-09-01 18:07:06 +03:00
return 1;
});
2018-09-26 12:07:23 +03:00
$deferred2 = new Deferred(static function () use (&$called) {
$called[] = 2;
2018-09-01 18:07:06 +03:00
return 2;
});
$p1 = $this->promises->convertThenable($deferred1);
$p2 = $this->promises->convertThenable($deferred2);
2018-09-01 18:07:06 +03:00
$p3 = $p2->then(function () use (&$called) {
2018-09-26 12:07:23 +03:00
$dfd = new Deferred(static function () use (&$called) {
$called[] = 3;
2018-09-01 18:07:06 +03:00
return 3;
});
2018-09-01 18:07:06 +03:00
return $this->promises->convertThenable($dfd);
});
2018-09-26 12:07:23 +03:00
$p4 = $p3->then(static function () use (&$called) {
return new Deferred(static function () use (&$called) {
$called[] = 4;
2018-09-01 18:07:06 +03:00
return 4;
});
});
$all = $this->promises->all([0, $p1, $p2, $p3, $p4]);
$result = $this->promises->wait($p2);
2018-09-19 18:12:09 +03:00
self::assertEquals(2, $result);
self::assertEquals(SyncPromise::PENDING, $p3->adoptedPromise->state);
self::assertEquals(SyncPromise::PENDING, $all->adoptedPromise->state);
self::assertEquals([1, 2], $called);
2018-09-01 18:07:06 +03:00
$expectedResult = [0, 1, 2, 3, 4];
$result = $this->promises->wait($all);
2018-09-19 18:12:09 +03:00
self::assertEquals($expectedResult, $result);
self::assertEquals([1, 2, 3, 4], $called);
self::assertValidPromise($all, null, [0, 1, 2, 3, 4], SyncPromise::FULFILLED);
}
}