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

179 lines
5.8 KiB
PHP
Raw Permalink 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\Executor\Promise\Adapter\ReactPromiseAdapter;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
use React\Promise\Deferred;
use React\Promise\FulfilledPromise;
use React\Promise\LazyPromise;
use React\Promise\Promise as ReactPromise;
use React\Promise\RejectedPromise;
2018-09-26 12:07:23 +03:00
use stdClass;
2018-09-01 18:07:06 +03:00
use function class_exists;
/**
* @group ReactPromise
*/
2018-07-29 18:43:10 +03:00
class ReactPromiseAdapterTest extends TestCase
{
public function setUp()
{
2018-09-01 18:07:06 +03:00
if (class_exists('React\Promise\Promise')) {
return;
}
2018-09-01 18:07:06 +03:00
self::markTestSkipped('react/promise package must be installed to run GraphQL\Tests\Executor\Promise\ReactPromiseAdapterTest');
}
public function testIsThenableReturnsTrueWhenAReactPromiseIsGiven() : void
{
$reactAdapter = new ReactPromiseAdapter();
2018-09-19 18:12:09 +03:00
self::assertTrue(
2018-09-26 12:07:23 +03:00
$reactAdapter->isThenable(new ReactPromise(static function () {
2018-09-01 18:07:06 +03:00
}))
);
2018-09-19 18:12:09 +03:00
self::assertTrue($reactAdapter->isThenable(new FulfilledPromise()));
self::assertTrue($reactAdapter->isThenable(new RejectedPromise()));
self::assertTrue(
2018-09-26 12:07:23 +03:00
$reactAdapter->isThenable(new LazyPromise(static function () {
2018-09-01 18:07:06 +03:00
}))
);
2018-09-19 18:12:09 +03:00
self::assertFalse($reactAdapter->isThenable(false));
self::assertFalse($reactAdapter->isThenable(true));
self::assertFalse($reactAdapter->isThenable(1));
self::assertFalse($reactAdapter->isThenable(0));
self::assertFalse($reactAdapter->isThenable('test'));
self::assertFalse($reactAdapter->isThenable(''));
self::assertFalse($reactAdapter->isThenable([]));
2018-09-26 12:07:23 +03:00
self::assertFalse($reactAdapter->isThenable(new stdClass()));
}
public function testConvertsReactPromisesToGraphQlOnes() : void
{
$reactAdapter = new ReactPromiseAdapter();
$reactPromise = new FulfilledPromise(1);
$promise = $reactAdapter->convertThenable($reactPromise);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $promise);
self::assertInstanceOf('React\Promise\FulfilledPromise', $promise->adoptedPromise);
}
public function testThen() : void
{
$reactAdapter = new ReactPromiseAdapter();
$reactPromise = new FulfilledPromise(1);
2018-09-01 18:07:06 +03:00
$promise = $reactAdapter->convertThenable($reactPromise);
$result = null;
2018-09-01 18:07:06 +03:00
$resultPromise = $reactAdapter->then(
$promise,
2018-09-26 12:07:23 +03:00
static function ($value) use (&$result) {
2018-09-01 18:07:06 +03:00
$result = $value;
}
);
2018-09-19 18:12:09 +03:00
self::assertSame(1, $result);
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $resultPromise);
self::assertInstanceOf('React\Promise\FulfilledPromise', $resultPromise->adoptedPromise);
}
public function testCreate() : void
{
2018-09-01 18:07:06 +03:00
$reactAdapter = new ReactPromiseAdapter();
2018-09-26 12:07:23 +03:00
$resolvedPromise = $reactAdapter->create(static function ($resolve) {
2018-09-01 18:07:06 +03:00
$resolve(1);
});
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $resolvedPromise);
self::assertInstanceOf('React\Promise\Promise', $resolvedPromise->adoptedPromise);
$result = null;
2018-09-26 12:07:23 +03:00
$resolvedPromise->then(static function ($value) use (&$result) {
2018-09-01 18:07:06 +03:00
$result = $value;
});
2018-09-19 18:12:09 +03:00
self::assertSame(1, $result);
}
public function testCreateFulfilled() : void
{
2018-09-01 18:07:06 +03:00
$reactAdapter = new ReactPromiseAdapter();
$fulfilledPromise = $reactAdapter->createFulfilled(1);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $fulfilledPromise);
self::assertInstanceOf('React\Promise\FulfilledPromise', $fulfilledPromise->adoptedPromise);
$result = null;
2018-09-26 12:07:23 +03:00
$fulfilledPromise->then(static function ($value) use (&$result) {
$result = $value;
});
2018-09-19 18:12:09 +03:00
self::assertSame(1, $result);
}
public function testCreateRejected() : void
{
2018-09-01 18:07:06 +03:00
$reactAdapter = new ReactPromiseAdapter();
2018-09-26 12:07:23 +03:00
$rejectedPromise = $reactAdapter->createRejected(new Exception('I am a bad promise'));
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $rejectedPromise);
self::assertInstanceOf('React\Promise\RejectedPromise', $rejectedPromise->adoptedPromise);
$exception = null;
2018-09-01 18:07:06 +03:00
$rejectedPromise->then(
null,
2018-09-26 12:07:23 +03:00
static function ($error) use (&$exception) {
2018-09-01 18:07:06 +03:00
$exception = $error;
}
);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('\Exception', $exception);
self::assertEquals('I am a bad promise', $exception->getMessage());
}
public function testAll() : void
{
$reactAdapter = new ReactPromiseAdapter();
2018-09-01 18:07:06 +03:00
$promises = [new FulfilledPromise(1), new FulfilledPromise(2), new FulfilledPromise(3)];
$allPromise = $reactAdapter->all($promises);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf('GraphQL\Executor\Promise\Promise', $allPromise);
self::assertInstanceOf('React\Promise\FulfilledPromise', $allPromise->adoptedPromise);
$result = null;
2018-09-26 12:07:23 +03:00
$allPromise->then(static function ($values) use (&$result) {
2018-09-01 18:07:06 +03:00
$result = $values;
});
2018-09-19 18:12:09 +03:00
self::assertSame([1, 2, 3], $result);
}
public function testAllShouldPreserveTheOrderOfTheArrayWhenResolvingAsyncPromises() : void
{
$reactAdapter = new ReactPromiseAdapter();
2018-09-01 18:07:06 +03:00
$deferred = new Deferred();
$promises = [new FulfilledPromise(1), $deferred->promise(), new FulfilledPromise(3)];
$result = null;
2018-09-26 12:07:23 +03:00
$reactAdapter->all($promises)->then(static function ($values) use (&$result) {
$result = $values;
});
// Resolve the async promise
$deferred->resolve(2);
2018-09-19 18:12:09 +03:00
self::assertSame([1, 2, 3], $result);
}
}