graphql-php/tests/Executor/SyncTest.php

224 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;
use GraphQL\Deferred;
use GraphQL\Error\FormattedError;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
use GraphQL\GraphQL;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;
use GraphQL\Validator\DocumentValidator;
use PHPUnit\Framework\TestCase;
class SyncTest extends TestCase
{
/** @var Schema */
private $schema;
/** @var SyncPromiseAdapter */
private $promiseAdapter;
public function setUp()
{
$this->schema = new Schema([
2018-09-01 18:07:06 +03:00
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
2018-09-01 18:07:06 +03:00
'syncField' => [
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function ($rootValue) {
return $rootValue;
2018-09-01 18:07:06 +03:00
},
],
'asyncField' => [
2018-09-01 18:07:06 +03:00
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function ($rootValue) {
return new Deferred(static function () use ($rootValue) {
return $rootValue;
});
2018-09-01 18:07:06 +03:00
},
],
],
]),
'mutation' => new ObjectType([
2018-09-01 18:07:06 +03:00
'name' => 'Mutation',
'fields' => [
'syncMutationField' => [
2018-09-01 18:07:06 +03:00
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function ($rootValue) {
return $rootValue;
2018-09-01 18:07:06 +03:00
},
],
],
]),
]);
2018-09-01 18:07:06 +03:00
$this->promiseAdapter = new SyncPromiseAdapter();
}
// Describe: Execute: synchronously when possible
/**
* @see it('does not return a Promise for initial errors')
*/
public function testDoesNotReturnAPromiseForInitialErrors() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'fragment Example on Query { syncField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
2018-09-19 18:12:09 +03:00
self::assertSync(['errors' => [['message' => 'Must provide an operation.']]], $result);
}
2018-09-01 18:07:06 +03:00
private function execute($schema, $doc, $rootValue = null)
{
return Executor::promiseToExecute($this->promiseAdapter, $schema, $doc, $rootValue);
}
2018-09-19 18:12:09 +03:00
private static function assertSync($expectedFinalArray, $actualResult) : void
2018-09-01 18:07:06 +03:00
{
$message = 'Failed assertion that execution was synchronous';
2018-09-19 18:12:09 +03:00
self::assertInstanceOf(Promise::class, $actualResult, $message);
self::assertInstanceOf(SyncPromise::class, $actualResult->adoptedPromise, $message);
self::assertEquals(SyncPromise::FULFILLED, $actualResult->adoptedPromise->state, $message);
self::assertInstanceOf(ExecutionResult::class, $actualResult->adoptedPromise->result, $message);
self::assertArraySubset(
2018-09-01 18:07:06 +03:00
$expectedFinalArray,
$actualResult->adoptedPromise->result->toArray(),
false,
$message
);
}
/**
* @see it('does not return a Promise if fields are all synchronous')
*/
public function testDoesNotReturnAPromiseIfFieldsAreAllSynchronous() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'query Example { syncField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
2018-09-19 18:12:09 +03:00
self::assertSync(['data' => ['syncField' => 'rootValue']], $result);
}
2018-09-01 18:07:06 +03:00
// Describe: graphqlSync
/**
* @see it('does not return a Promise if mutation fields are all synchronous')
*/
public function testDoesNotReturnAPromiseIfMutationFieldsAreAllSynchronous() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'mutation Example { syncMutationField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
2018-09-19 18:12:09 +03:00
self::assertSync(['data' => ['syncMutationField' => 'rootValue']], $result);
}
/**
* @see it('returns a Promise if any field is asynchronous')
*/
public function testReturnsAPromiseIfAnyFieldIsAsynchronous() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'query Example { syncField, asyncField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
$this->assertAsync(['data' => ['syncField' => 'rootValue', 'asyncField' => 'rootValue']], $result);
}
2018-09-01 18:07:06 +03:00
private function assertAsync($expectedFinalArray, $actualResult)
{
$message = 'Failed assertion that execution was asynchronous';
2018-09-19 18:12:09 +03:00
self::assertInstanceOf(Promise::class, $actualResult, $message);
self::assertInstanceOf(SyncPromise::class, $actualResult->adoptedPromise, $message);
self::assertEquals(SyncPromise::PENDING, $actualResult->adoptedPromise->state, $message);
2018-09-01 18:07:06 +03:00
$resolvedResult = $this->promiseAdapter->wait($actualResult);
2018-09-19 18:12:09 +03:00
self::assertInstanceOf(ExecutionResult::class, $resolvedResult, $message);
self::assertArraySubset($expectedFinalArray, $resolvedResult->toArray(), false, $message);
2018-09-01 18:07:06 +03:00
}
/**
* @see it('does not return a Promise for syntax errors')
*/
public function testDoesNotReturnAPromiseForSyntaxErrors() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'fragment Example on Query { { { syncField }';
$result = $this->graphqlSync(
$this->schema,
$doc
);
2018-09-19 18:12:09 +03:00
self::assertSync(
2018-09-01 18:07:06 +03:00
[
'errors' => [
[
'message' => 'Syntax Error: Expected Name, found {',
'locations' => [['line' => 1, 'column' => 29]],
],
],
],
$result
);
}
private function graphqlSync($schema, $doc, $rootValue = null)
{
return GraphQL::promiseToExecute($this->promiseAdapter, $schema, $doc, $rootValue);
}
/**
* @see it('does not return a Promise for validation errors')
*/
public function testDoesNotReturnAPromiseForValidationErrors() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'fragment Example on Query { unknownField }';
$validationErrors = DocumentValidator::validate($this->schema, Parser::parse($doc));
2018-09-01 18:07:06 +03:00
$result = $this->graphqlSync(
$this->schema,
$doc
);
2018-09-01 18:07:06 +03:00
$expected = [
'errors' => Utils::map(
$validationErrors,
2018-09-26 12:07:23 +03:00
static function ($e) {
2018-09-01 18:07:06 +03:00
return FormattedError::createFromException($e);
}
),
];
2018-09-19 18:12:09 +03:00
self::assertSync($expected, $result);
}
/**
* @see it('does not return a Promise for sync execution')
*/
public function testDoesNotReturnAPromiseForSyncExecution() : void
{
2018-09-01 18:07:06 +03:00
$doc = 'query Example { syncField }';
$result = $this->graphqlSync(
$this->schema,
$doc,
'rootValue'
);
2018-09-19 18:12:09 +03:00
self::assertSync(['data' => ['syncField' => 'rootValue']], $result);
}
}