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

220 lines
7.3 KiB
PHP
Raw Normal View History

<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Executor\Promise;
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-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,
$this->promises->isThenable(new Deferred(function () {
}))
);
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([]));
self::assertEquals(false, $this->promises->isThenable(new \stdClass()));
}
public function testConvert() : void
{
2018-09-01 18:07:06 +03:00
$dfd = new Deferred(function () {
});
$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-01 18:07:06 +03:00
$dfd = new Deferred(function () {
});
$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-01 18:07:06 +03:00
$promise = $this->promises->create(function ($resolve, $reject) {
});
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
$promise = $this->promises->create(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(
function ($nextValue) use (&$actualNextValue, &$onFulfilledCalled) {
$onFulfilledCalled = true;
$actualNextValue = $nextValue;
},
function (\Throwable $reason) use (&$actualNextReason, &$onRejectedCalled) {
$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
{
$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-01 18:07:06 +03:00
function ($value) {
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-01 18:07:06 +03:00
$deferred1 = new Deferred(function () use (&$called) {
$called[] = 1;
2018-09-01 18:07:06 +03:00
return 1;
});
2018-09-01 18:07:06 +03:00
$deferred2 = new Deferred(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) {
$dfd = new Deferred(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-01 18:07:06 +03:00
$p4 = $p3->then(function () use (&$called) {
return new Deferred(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);
}
}