Merge pull request #340 from simPod/cs-language-server

Fix CS in tests/Server
This commit is contained in:
Vladimir Razuvaev 2018-09-02 21:23:47 +07:00 committed by GitHub
commit ce0272b447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 719 additions and 654 deletions

View File

@ -1,9 +1,13 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server\Psr7;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use function strtolower;
class PsrRequestStub implements ServerRequestInterface
{
@ -13,14 +17,10 @@ class PsrRequestStub implements ServerRequestInterface
public $method;
/**
* @var PsrStreamStub
*/
/** @var PsrStreamStub */
public $body;
/**
* @var array
*/
/** @var mixed[] */
public $parsedBody;
/**
@ -32,7 +32,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getProtocolVersion()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -50,7 +50,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withProtocolVersion($version)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -80,7 +80,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getHeaders()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -93,7 +93,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function hasHeader($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -113,7 +113,8 @@ class PsrRequestStub implements ServerRequestInterface
public function getHeader($name)
{
$name = strtolower($name);
return isset($this->headers[$name]) ? $this->headers[$name] : [];
return $this->headers[$name] ?? [];
}
/**
@ -137,7 +138,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getHeaderLine($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -157,7 +158,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withHeader($name, $value)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -178,7 +179,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withAddedHeader($name, $value)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -195,7 +196,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withoutHeader($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -223,7 +224,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withBody(StreamInterface $body)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -244,7 +245,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getRequestTarget()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -266,7 +267,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withRequestTarget($requestTarget)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -296,7 +297,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withMethod($method)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -345,7 +346,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withUri(UriInterface $uri, $preserveHost = false)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -359,7 +360,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getServerParams()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -374,7 +375,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getCookieParams()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -396,7 +397,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withCookieParams(array $cookies)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -440,7 +441,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withQueryParams(array $query)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -457,7 +458,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getUploadedFiles()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -473,7 +474,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withUploadedFiles(array $uploadedFiles)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -526,7 +527,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withParsedBody($data)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -542,7 +543,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getAttributes()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -562,7 +563,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function getAttribute($name, $default = null)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -582,7 +583,7 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withAttribute($name, $value)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -601,6 +602,6 @@ class PsrRequestStub implements ServerRequestInterface
*/
public function withoutAttribute($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
}

View File

@ -1,6 +1,8 @@
<?php
namespace GraphQL\Tests\Server\Psr7;
declare(strict_types=1);
namespace GraphQL\Tests\Server\Psr7;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
@ -22,7 +24,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function getProtocolVersion()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -40,7 +42,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function withProtocolVersion($version)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -70,7 +72,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function getHeaders()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -83,7 +85,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function hasHeader($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -102,7 +104,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function getHeader($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -126,7 +128,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function getHeaderLine($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -148,6 +150,7 @@ class PsrResponseStub implements ResponseInterface
{
$tmp = clone $this;
$tmp->headers[$name][] = $value;
return $tmp;
}
@ -169,7 +172,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function withAddedHeader($name, $value)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -186,7 +189,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function withoutHeader($name)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -196,7 +199,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function getBody()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -216,6 +219,7 @@ class PsrResponseStub implements ResponseInterface
{
$tmp = clone $this;
$tmp->body = $body;
return $tmp;
}
@ -229,7 +233,7 @@ class PsrResponseStub implements ResponseInterface
*/
public function getStatusCode()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -256,6 +260,7 @@ class PsrResponseStub implements ResponseInterface
{
$tmp = clone $this;
$tmp->statusCode = $code;
return $tmp;
}
@ -274,6 +279,6 @@ class PsrResponseStub implements ResponseInterface
*/
public function getReasonPhrase()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
}

View File

@ -1,7 +1,12 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server\Psr7;
use Psr\Http\Message\StreamInterface;
use function strlen;
use const SEEK_SET;
class PsrStreamStub implements StreamInterface
{
@ -33,7 +38,7 @@ class PsrStreamStub implements StreamInterface
*/
public function close()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -45,7 +50,7 @@ class PsrStreamStub implements StreamInterface
*/
public function detach()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -66,7 +71,7 @@ class PsrStreamStub implements StreamInterface
*/
public function tell()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -76,7 +81,7 @@ class PsrStreamStub implements StreamInterface
*/
public function eof()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -86,7 +91,7 @@ class PsrStreamStub implements StreamInterface
*/
public function isSeekable()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -103,7 +108,7 @@ class PsrStreamStub implements StreamInterface
*/
public function seek($offset, $whence = SEEK_SET)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -118,7 +123,7 @@ class PsrStreamStub implements StreamInterface
*/
public function rewind()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -141,6 +146,7 @@ class PsrStreamStub implements StreamInterface
public function write($string)
{
$this->content = $string;
return strlen($string);
}
@ -151,7 +157,7 @@ class PsrStreamStub implements StreamInterface
*/
public function isReadable()
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -166,7 +172,7 @@ class PsrStreamStub implements StreamInterface
*/
public function read($length)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
/**
@ -195,6 +201,6 @@ class PsrStreamStub implements StreamInterface
*/
public function getMetadata($key = null)
{
throw new \Exception("Not implemented");
throw new \Exception('Not implemented');
}
}

View File

@ -1,11 +1,15 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Server\Helper;
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
use GraphQL\Tests\Server\Psr7\PsrResponseStub;
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
use PHPUnit\Framework\TestCase;
use function json_encode;
class PsrResponseTest extends TestCase
{

View File

@ -1,11 +1,12 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Deferred;
use GraphQL\Error\Debug;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\UserError;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\Parser;
@ -16,12 +17,12 @@ use GraphQL\Server\ServerConfig;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\CustomValidationRule;
use GraphQL\Validator\ValidationContext;
use function count;
use function sprintf;
class QueryExecutionTest extends ServerTestCase
{
/**
* @var ServerConfig
*/
/** @var ServerConfig */
private $config;
public function setUp()
@ -36,14 +37,30 @@ class QueryExecutionTest extends ServerTestCase
$query = '{f1}';
$expected = [
'data' => [
'f1' => 'f1'
]
'data' => ['f1' => 'f1'],
];
$this->assertQueryResultEquals($expected, $query);
}
private function assertQueryResultEquals($expected, $query, $variables = null)
{
$result = $this->executeQuery($query, $variables);
$this->assertArraySubset($expected, $result->toArray(true));
return $result;
}
private function executeQuery($query, $variables = null, $readonly = false)
{
$op = OperationParams::create(['query' => $query, 'variables' => $variables], $readonly);
$helper = new Helper();
$result = $helper->executeOperation($this->config, $op);
$this->assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
public function testReturnsSyntaxErrors() : void
{
$query = '{f1';
@ -72,15 +89,15 @@ class QueryExecutionTest extends ServerTestCase
$expected = [
'data' => [
'fieldWithException' => null,
'f1' => 'f1'
'f1' => 'f1',
],
'errors' => [
[
'message' => 'This is the exception we want',
'path' => ['fieldWithException'],
'trace' => []
]
]
'trace' => [],
],
],
];
$result = $this->executeQuery($query)->toArray();
@ -119,8 +136,8 @@ class QueryExecutionTest extends ServerTestCase
$expected = [
'data' => [
'a' => 'a',
'b' => 'b'
]
'b' => 'b',
],
];
$this->assertQueryResultEquals($expected, $query, $variables);
}
@ -132,8 +149,8 @@ class QueryExecutionTest extends ServerTestCase
';
$expected = [
'errors' => [
['message' => 'Cannot query field "nonExistentField" on type "Query".']
]
['message' => 'Cannot query field "nonExistentField" on type "Query".'],
],
];
$this->assertQueryResultEquals($expected, $query);
@ -143,13 +160,14 @@ class QueryExecutionTest extends ServerTestCase
$rules = [
new CustomValidationRule('SomeRule', function () use (&$called) {
$called = true;
return [];
})
}),
];
$this->config->setValidationRules($rules);
$expected = [
'data' => []
'data' => [],
];
$this->assertQueryResultEquals($expected, $query);
$this->assertTrue($called);
@ -165,6 +183,7 @@ class QueryExecutionTest extends ServerTestCase
$params = $p;
$doc = $d;
$operationType = $o;
return [];
});
@ -186,15 +205,17 @@ class QueryExecutionTest extends ServerTestCase
$this->config->setValidationRules(function (OperationParams $params) use ($q1, $q2, &$called1, &$called2) {
if ($params->query === $q1) {
$called1 = true;
return DocumentValidator::allRules();
} else {
}
$called2 = true;
return [
new CustomValidationRule('MyRule', function (ValidationContext $context) {
$context->reportError(new Error("This is the error we are looking for!"));
})
$context->reportError(new Error('This is the error we are looking for!'));
}),
];
}
});
$expected = ['data' => ['f1' => 'f1']];
@ -226,22 +247,28 @@ class QueryExecutionTest extends ServerTestCase
'errors' => [
[
'message' => 'Persisted queries are not supported by this server',
'category' => 'request'
]
]
'category' => 'request',
],
],
];
$this->assertEquals($expected, $result->toArray());
}
private function executePersistedQuery($queryId, $variables = null)
{
$op = OperationParams::create(['queryId' => $queryId, 'variables' => $variables]);
$helper = new Helper();
$result = $helper->executeOperation($this->config, $op);
$this->assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
public function testBatchedQueriesAreDisabledByDefault() : void
{
$batch = [
[
'query' => '{invalid}'
],
[
'query' => '{f1,fieldWithException}'
]
['query' => '{invalid}'],
['query' => '{f1,fieldWithException}'],
];
$result = $this->executeBatchedQuery($batch);
@ -251,17 +278,17 @@ class QueryExecutionTest extends ServerTestCase
'errors' => [
[
'message' => 'Batched queries are not supported by this server',
'category' => 'request'
]
]
'category' => 'request',
],
],
],
[
'errors' => [
[
'message' => 'Batched queries are not supported by this server',
'category' => 'request'
]
]
'category' => 'request',
],
],
],
];
@ -269,6 +296,31 @@ class QueryExecutionTest extends ServerTestCase
$this->assertEquals($expected[1], $result[1]->toArray());
}
/**
* @param mixed[][] $qs
*/
private function executeBatchedQuery(array $qs)
{
$batch = [];
foreach ($qs as $params) {
$batch[] = OperationParams::create($params);
}
$helper = new Helper();
$result = $helper->executeBatch($this->config, $batch);
$this->assertInternalType('array', $result);
$this->assertCount(count($qs), $result);
foreach ($result as $index => $entry) {
$this->assertInstanceOf(
ExecutionResult::class,
$entry,
sprintf('Result at %s is not an instance of %s', $index, ExecutionResult::class)
);
}
return $result;
}
public function testMutationsAreNotAllowedInReadonlyMode() : void
{
$mutation = 'mutation { a }';
@ -277,9 +329,9 @@ class QueryExecutionTest extends ServerTestCase
'errors' => [
[
'message' => 'GET supports only query operation',
'category' => 'request'
]
]
'category' => 'request',
],
],
];
$result = $this->executeQuery($mutation, null, true);
@ -292,6 +344,7 @@ class QueryExecutionTest extends ServerTestCase
$this->config->setPersistentQueryLoader(function ($queryId, OperationParams $params) use (&$called) {
$called = true;
$this->assertEquals('some-id', $queryId);
return '{f1}';
});
@ -299,9 +352,7 @@ class QueryExecutionTest extends ServerTestCase
$this->assertTrue($called);
$expected = [
'data' => [
'f1' => 'f1'
]
'data' => ['f1' => 'f1'],
];
$this->assertEquals($expected, $result->toArray());
@ -310,6 +361,7 @@ class QueryExecutionTest extends ServerTestCase
$this->config->setPersistentQueryLoader(function ($queryId, OperationParams $params) use (&$called) {
$called = true;
$this->assertEquals('some-id', $queryId);
return Parser::parse('{f1}');
});
$result = $this->executePersistedQuery('some-id');
@ -341,12 +393,11 @@ class QueryExecutionTest extends ServerTestCase
[
'message' => 'Cannot query field "invalid" on type "Query".',
'locations' => [['line' => 1, 'column' => 2]],
'category' => 'graphql'
]
]
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
}
public function testAllowSkippingValidationForPersistedQueries() : void
@ -355,21 +406,21 @@ class QueryExecutionTest extends ServerTestCase
->setPersistentQueryLoader(function ($queryId) {
if ($queryId === 'some-id') {
return '{invalid}';
} else {
return '{invalid2}';
}
return '{invalid2}';
})
->setValidationRules(function (OperationParams $params) {
if ($params->queryId === 'some-id') {
return [];
} else {
return DocumentValidator::allRules();
}
return DocumentValidator::allRules();
});
$result = $this->executePersistedQuery('some-id');
$expected = [
'data' => []
'data' => [],
];
$this->assertEquals($expected, $result->toArray());
@ -379,9 +430,9 @@ class QueryExecutionTest extends ServerTestCase
[
'message' => 'Cannot query field "invalid2" on type "Query".',
'locations' => [['line' => 1, 'column' => 2]],
'category' => 'graphql'
]
]
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
}
@ -401,12 +452,8 @@ class QueryExecutionTest extends ServerTestCase
$this->config->setQueryBatching(true);
$batch = [
[
'query' => '{invalid}'
],
[
'query' => '{f1,fieldWithException}'
],
['query' => '{invalid}'],
['query' => '{f1,fieldWithException}'],
[
'query' => '
query ($a: String!, $b: String!) {
@ -415,30 +462,30 @@ class QueryExecutionTest extends ServerTestCase
}
',
'variables' => ['a' => 'a', 'b' => 'b'],
]
],
];
$result = $this->executeBatchedQuery($batch);
$expected = [
[
'errors' => [['message' => 'Cannot query field "invalid" on type "Query".']]
'errors' => [['message' => 'Cannot query field "invalid" on type "Query".']],
],
[
'data' => [
'f1' => 'f1',
'fieldWithException' => null
'fieldWithException' => null,
],
'errors' => [
['message' => 'This is the exception we want']
]
['message' => 'This is the exception we want'],
],
],
[
'data' => [
'a' => 'a',
'b' => 'b'
]
]
'b' => 'b',
],
],
];
$this->assertArraySubset($expected[0], $result[0]->toArray());
@ -449,15 +496,9 @@ class QueryExecutionTest extends ServerTestCase
public function testDeferredsAreSharedAmongAllBatchedQueries() : void
{
$batch = [
[
'query' => '{dfd(num: 1)}'
],
[
'query' => '{dfd(num: 2)}'
],
[
'query' => '{dfd(num: 3)}',
]
['query' => '{dfd(num: 1)}'],
['query' => '{dfd(num: 2)}'],
['query' => '{dfd(num: 3)}'],
];
$calls = [];
@ -467,12 +508,13 @@ class QueryExecutionTest extends ServerTestCase
->setRootValue('1')
->setContext([
'buffer' => function ($num) use (&$calls) {
$calls[] = "buffer: $num";
$calls[] = sprintf('buffer: %d', $num);
},
'load' => function ($num) use (&$calls) {
$calls[] = "load: $num";
return "loaded: $num";
}
$calls[] = sprintf('load: %d', $num);
return sprintf('loaded: %d', $num);
},
]);
$result = $this->executeBatchedQuery($batch);
@ -489,19 +531,13 @@ class QueryExecutionTest extends ServerTestCase
$expected = [
[
'data' => [
'dfd' => 'loaded: 1'
]
'data' => ['dfd' => 'loaded: 1'],
],
[
'data' => [
'dfd' => 'loaded: 2'
]
'data' => ['dfd' => 'loaded: 2'],
],
[
'data' => [
'dfd' => 'loaded: 3'
]
'data' => ['dfd' => 'loaded: 3'],
],
];
@ -578,6 +614,7 @@ class QueryExecutionTest extends ServerTestCase
$this->config->setErrorFormatter(function ($e) use (&$called, &$error) {
$called = true;
$error = $e;
return ['test' => 'formatted'];
});
@ -586,8 +623,8 @@ class QueryExecutionTest extends ServerTestCase
$formatted = $result->toArray();
$expected = [
'errors' => [
['test' => 'formatted']
]
['test' => 'formatted'],
],
];
$this->assertTrue($called);
$this->assertArraySubset($expected, $formatted);
@ -599,9 +636,9 @@ class QueryExecutionTest extends ServerTestCase
'errors' => [
[
'test' => 'formatted',
'trace' => []
]
]
'trace' => [],
],
],
];
$this->assertArraySubset($expected, $formatted);
}
@ -615,8 +652,9 @@ class QueryExecutionTest extends ServerTestCase
$called = true;
$errors = $e;
$formatter = $f;
return [
['test' => 'handled']
['test' => 'handled'],
];
});
@ -626,8 +664,8 @@ class QueryExecutionTest extends ServerTestCase
$formatted = $result->toArray();
$expected = [
'errors' => [
['test' => 'handled']
]
['test' => 'handled'],
],
];
$this->assertTrue($called);
$this->assertArraySubset($expected, $formatted);
@ -636,46 +674,4 @@ class QueryExecutionTest extends ServerTestCase
$this->assertInternalType('callable', $formatter);
$this->assertArraySubset($expected, $formatted);
}
private function executePersistedQuery($queryId, $variables = null)
{
$op = OperationParams::create(['queryId' => $queryId, 'variables' => $variables]);
$helper = new Helper();
$result = $helper->executeOperation($this->config, $op);
$this->assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
private function executeQuery($query, $variables = null, $readonly = false)
{
$op = OperationParams::create(['query' => $query, 'variables' => $variables], $readonly);
$helper = new Helper();
$result = $helper->executeOperation($this->config, $op);
$this->assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
private function executeBatchedQuery(array $qs)
{
$batch = [];
foreach ($qs as $params) {
$batch[] = OperationParams::create($params);
}
$helper = new Helper();
$result = $helper->executeBatch($this->config, $batch);
$this->assertInternalType('array', $result);
$this->assertCount(count($qs), $result);
foreach ($result as $index => $entry) {
$this->assertInstanceOf(ExecutionResult::class, $entry, "Result at $index is not an instance of " . ExecutionResult::class);
}
return $result;
}
private function assertQueryResultEquals($expected, $query, $variables = null)
{
$result = $this->executeQuery($query, $variables);
$this->assertArraySubset($expected, $result->toArray(true));
return $result;
}
}

View File

@ -1,7 +1,9 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Server\Helper;
use GraphQL\Server\OperationParams;
@ -9,6 +11,8 @@ use GraphQL\Server\RequestError;
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
use PHPUnit\Framework\TestCase;
use function json_decode;
use function json_encode;
class RequestParsingTest extends TestCase
{
@ -17,7 +21,7 @@ class RequestParsingTest extends TestCase
$query = '{my query}';
$parsed = [
'raw' => $this->parseRawRequest('application/graphql', $query),
'psr' => $this->parsePsrRequest('application/graphql', $query)
'psr' => $this->parsePsrRequest('application/graphql', $query),
];
foreach ($parsed as $source => $parsedBody) {
@ -26,6 +30,77 @@ class RequestParsingTest extends TestCase
}
}
/**
* @param string $contentType
* @param string $content
*
* @return OperationParams|OperationParams[]
*/
private function parseRawRequest($contentType, $content, string $method = 'POST')
{
$_SERVER['CONTENT_TYPE'] = $contentType;
$_SERVER['REQUEST_METHOD'] = $method;
$helper = new Helper();
return $helper->parseHttpRequest(function () use ($content) {
return $content;
});
}
/**
* @param string $contentType
* @param string $content
*
* @return OperationParams|OperationParams[]
*/
private function parsePsrRequest($contentType, $content, string $method = 'POST')
{
$psrRequestBody = new PsrStreamStub();
$psrRequestBody->content = $content;
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = $method;
$psrRequest->body = $psrRequestBody;
if ($contentType === 'application/json') {
$parsedBody = json_decode($content, true);
$parsedBody = $parsedBody === false ? null : $parsedBody;
} else {
$parsedBody = null;
}
$psrRequest->parsedBody = $parsedBody;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
/**
* @param OperationParams $params
* @param string $query
* @param string $queryId
* @param mixed|null $variables
* @param string $operation
*/
private function assertValidOperationParams(
$params,
$query,
$queryId = null,
$variables = null,
$operation = null,
$message = ''
) {
$this->assertInstanceOf(OperationParams::class, $params, $message);
$this->assertSame($query, $params->query, $message);
$this->assertSame($queryId, $params->queryId, $message);
$this->assertSame($variables, $params->variables, $message);
$this->assertSame($operation, $params->operation, $message);
}
public function testParsesUrlencodedRequest() : void
{
$query = '{my query}';
@ -35,11 +110,11 @@ class RequestParsingTest extends TestCase
$post = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawFormUrlencodedRequest($post),
'psr' => $this->parsePsrFormUrlEncodedRequest($post)
'psr' => $this->parsePsrFormUrlEncodedRequest($post),
];
foreach ($parsed as $method => $parsedBody) {
@ -48,6 +123,39 @@ class RequestParsingTest extends TestCase
}
}
/**
* @param mixed[] $postValue
* @return OperationParams|OperationParams[]
*/
private function parseRawFormUrlencodedRequest($postValue)
{
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $postValue;
$helper = new Helper();
return $helper->parseHttpRequest(function () {
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
});
}
/**
* @param mixed[] $postValue
* @return OperationParams[]|OperationParams
*/
private function parsePsrFormUrlEncodedRequest($postValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = ['application/x-www-form-urlencoded'];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $postValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
public function testParsesGetRequest() : void
{
$query = '{my query}';
@ -57,11 +165,11 @@ class RequestParsingTest extends TestCase
$get = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawGetRequest($get),
'psr' => $this->parsePsrGetRequest($get)
'psr' => $this->parsePsrGetRequest($get),
];
foreach ($parsed as $method => $parsedBody) {
@ -70,6 +178,37 @@ class RequestParsingTest extends TestCase
}
}
/**
* @param mixed[] $getValue
* @return OperationParams
*/
private function parseRawGetRequest($getValue)
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET = $getValue;
$helper = new Helper();
return $helper->parseHttpRequest(function () {
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
});
}
/**
* @param mixed[] $getValue
* @return OperationParams[]|OperationParams
*/
private function parsePsrGetRequest($getValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->method = 'GET';
$psrRequest->queryParams = $getValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
public function testParsesMultipartFormdataRequest() : void
{
$query = '{my query}';
@ -79,11 +218,11 @@ class RequestParsingTest extends TestCase
$post = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawMultipartFormdataRequest($post),
'psr' => $this->parsePsrMultipartFormdataRequest($post)
'psr' => $this->parsePsrMultipartFormdataRequest($post),
];
foreach ($parsed as $method => $parsedBody) {
@ -92,6 +231,39 @@ class RequestParsingTest extends TestCase
}
}
/**
* @param mixed[] $postValue
* @return OperationParams|OperationParams[]
*/
private function parseRawMultipartFormDataRequest($postValue)
{
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data; boundary=----FormBoundary';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $postValue;
$helper = new Helper();
return $helper->parseHttpRequest(function () {
throw new InvariantViolation("Shouldn't read from php://input for multipart/form-data request");
});
}
/**
* @param mixed[] $postValue
* @return OperationParams|OperationParams[]
*/
private function parsePsrMultipartFormDataRequest($postValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = ['multipart/form-data; boundary=----FormBoundary'];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $postValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
public function testParsesJSONRequest() : void
{
$query = '{my query}';
@ -101,11 +273,11 @@ class RequestParsingTest extends TestCase
$body = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
'psr' => $this->parsePsrRequest('application/json', json_encode($body))
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
@ -122,11 +294,11 @@ class RequestParsingTest extends TestCase
$body = [
'query' => $query,
'variables' => json_encode($variables),
'operationName' => $operation
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
'psr' => $this->parsePsrRequest('application/json', json_encode($body))
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
@ -143,7 +315,7 @@ class RequestParsingTest extends TestCase
$body = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
@ -161,23 +333,37 @@ class RequestParsingTest extends TestCase
[
'query' => '{my query}',
'variables' => ['test' => 1, 'test2' => 2],
'operationName' => 'op'
'operationName' => 'op',
],
[
'queryId' => 'my-query-id',
'variables' => ['test' => 1, 'test2' => 2],
'operationName' => 'op2'
'operationName' => 'op2',
],
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
'psr' => $this->parsePsrRequest('application/json', json_encode($body))
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
$this->assertInternalType('array', $parsedBody, $method);
$this->assertCount(2, $parsedBody, $method);
$this->assertValidOperationParams($parsedBody[0], $body[0]['query'], null, $body[0]['variables'], $body[0]['operationName'], $method);
$this->assertValidOperationParams($parsedBody[1], null, $body[1]['queryId'], $body[1]['variables'], $body[1]['operationName'], $method);
$this->assertValidOperationParams(
$parsedBody[0],
$body[0]['query'],
null,
$body[0]['variables'],
$body[0]['operationName'],
$method
);
$this->assertValidOperationParams(
$parsedBody[1],
null,
$body[1]['queryId'],
$body[1]['variables'],
$body[1]['operationName'],
$method
);
}
}
@ -272,169 +458,13 @@ class RequestParsingTest extends TestCase
{
$this->expectException(RequestError::class);
$this->expectExceptionMessage('HTTP Method "PUT" is not supported');
$this->parseRawRequest('application/json', json_encode([]), "PUT");
$this->parseRawRequest('application/json', json_encode([]), 'PUT');
}
public function testFailsOnMethodsOtherThanPostOrGetPsr() : void
{
$this->expectException(RequestError::class);
$this->expectExceptionMessage('HTTP Method "PUT" is not supported');
$this->parsePsrRequest('application/json', json_encode([]), "PUT");
}
/**
* @param string $contentType
* @param string $content
* @param $method
*
* @return OperationParams|OperationParams[]
*/
private function parseRawRequest($contentType, $content, $method = 'POST')
{
$_SERVER['CONTENT_TYPE'] = $contentType;
$_SERVER['REQUEST_METHOD'] = $method;
$helper = new Helper();
return $helper->parseHttpRequest(function() use ($content) {
return $content;
});
}
/**
* @param string $contentType
* @param string $content
* @param $method
*
* @return OperationParams|OperationParams[]
*/
private function parsePsrRequest($contentType, $content, $method = 'POST')
{
$psrRequestBody = new PsrStreamStub();
$psrRequestBody->content = $content;
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = $method;
$psrRequest->body = $psrRequestBody;
if ($contentType === 'application/json') {
$parsedBody = json_decode($content, true);
$parsedBody = $parsedBody === false ? null : $parsedBody;
} else {
$parsedBody = null;
}
$psrRequest->parsedBody = $parsedBody;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
/**
* @param array $postValue
* @return OperationParams|OperationParams[]
*/
private function parseRawFormUrlencodedRequest($postValue)
{
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $postValue;
$helper = new Helper();
return $helper->parseHttpRequest(function() {
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
});
}
/**
* @param $postValue
* @return array|Helper
*/
private function parsePsrFormUrlEncodedRequest($postValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = ['application/x-www-form-urlencoded'];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $postValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
/**
* @param array $postValue
* @return OperationParams|OperationParams[]
*/
private function parseRawMultipartFormDataRequest($postValue)
{
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data; boundary=----FormBoundary';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $postValue;
$helper = new Helper();
return $helper->parseHttpRequest(function() {
throw new InvariantViolation("Shouldn't read from php://input for multipart/form-data request");
});
}
/**
* @param $postValue
* @return array|Helper
*/
private function parsePsrMultipartFormDataRequest($postValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = ['multipart/form-data; boundary=----FormBoundary'];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $postValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
/**
* @param $getValue
* @return OperationParams
*/
private function parseRawGetRequest($getValue)
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET = $getValue;
$helper = new Helper();
return $helper->parseHttpRequest(function() {
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
});
}
/**
* @param $getValue
* @return array|Helper
*/
private function parsePsrGetRequest($getValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->method = 'GET';
$psrRequest->queryParams = $getValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
/**
* @param OperationParams $params
* @param string $query
* @param string $queryId
* @param array $variables
* @param string $operation
*/
private function assertValidOperationParams($params, $query, $queryId = null, $variables = null, $operation = null, $message = '')
{
$this->assertInstanceOf(OperationParams::class, $params, $message);
$this->assertSame($query, $params->query, $message);
$this->assertSame($queryId, $params->queryId, $message);
$this->assertSame($variables, $params->variables, $message);
$this->assertSame($operation, $params->operation, $message);
$this->parsePsrRequest('application/json', json_encode([]), 'PUT');
}
}

View File

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Server\Helper;
@ -22,6 +25,13 @@ class RequestValidationTest extends TestCase
$this->assertValid($parsedBody);
}
private function assertValid($parsedRequest)
{
$helper = new Helper();
$errors = $helper->validateOperationParams($parsedRequest);
$this->assertEmpty($errors, isset($errors[0]) ? $errors[0]->getMessage() : '');
}
public function testRequestWithQueryIdShouldValidate() : void
{
$queryId = 'some-query-id';
@ -50,6 +60,17 @@ class RequestValidationTest extends TestCase
);
}
private function assertInputError($parsedRequest, $expectedMessage)
{
$helper = new Helper();
$errors = $helper->validateOperationParams($parsedRequest);
if (! empty($errors[0])) {
$this->assertEquals($expectedMessage, $errors[0]->getMessage());
} else {
$this->fail('Expected error not returned');
}
}
public function testFailsWhenBothQueryAndQueryIdArePresent() : void
{
$parsedBody = OperationParams::create([
@ -66,7 +87,7 @@ class RequestValidationTest extends TestCase
public function testFailsWhenQueryParameterIsNotString() : void
{
$parsedBody = OperationParams::create([
'query' => ['t' => '{my query}']
'query' => ['t' => '{my query}'],
]);
$this->assertInputError(
@ -78,7 +99,7 @@ class RequestValidationTest extends TestCase
public function testFailsWhenQueryIdParameterIsNotString() : void
{
$parsedBody = OperationParams::create([
'queryId' => ['t' => '{my query}']
'queryId' => ['t' => '{my query}'],
]);
$this->assertInputError(
@ -91,7 +112,7 @@ class RequestValidationTest extends TestCase
{
$parsedBody = OperationParams::create([
'query' => '{my query}',
'operationName' => []
'operationName' => [],
]);
$this->assertInputError(
@ -108,14 +129,14 @@ class RequestValidationTest extends TestCase
$query = '{my q}';
$parsedBody = OperationParams::create([
'query' => $query,
'variables' => null
'variables' => null,
]);
$this->assertValid($parsedBody);
$variables = "";
$variables = '';
$parsedBody = OperationParams::create([
'query' => $query,
'variables' => $variables
'variables' => $variables,
]);
$this->assertValid($parsedBody);
}
@ -124,7 +145,7 @@ class RequestValidationTest extends TestCase
{
$parsedBody = OperationParams::create([
'query' => '{my query}',
'variables' => 0
'variables' => 0,
]);
$this->assertInputError(
@ -132,22 +153,4 @@ class RequestValidationTest extends TestCase
'GraphQL Request parameter "variables" must be object or JSON string parsed to object, but got 0'
);
}
private function assertValid($parsedRequest)
{
$helper = new Helper();
$errors = $helper->validateOperationParams($parsedRequest);
$this->assertEmpty($errors, isset($errors[0]) ? $errors[0]->getMessage() : '');
}
private function assertInputError($parsedRequest, $expectedMessage)
{
$helper = new Helper();
$errors = $helper->validateOperationParams($parsedRequest);
if (!empty($errors[0])) {
$this->assertEquals($expectedMessage, $errors[0]->getMessage());
} else {
$this->fail('Expected error not returned');
}
}
}

View File

@ -1,12 +1,15 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Type\Schema;
use GraphQL\Server\ServerConfig;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
class ServerConfigTest extends TestCase
@ -70,7 +73,8 @@ class ServerConfigTest extends TestCase
{
$config = ServerConfig::create();
$formatter = function() {};
$formatter = function () {
};
$config->setErrorFormatter($formatter);
$this->assertSame($formatter, $config->getErrorFormatter());
@ -83,7 +87,8 @@ class ServerConfigTest extends TestCase
{
$config = ServerConfig::create();
$handler = function() {};
$handler = function () {
};
$config->setErrorsHandler($handler);
$this->assertSame($handler, $config->getErrorsHandler());
@ -113,11 +118,17 @@ class ServerConfigTest extends TestCase
$config->setValidationRules($rules);
$this->assertSame($rules, $config->getValidationRules());
$rules = [function() {}];
$rules = [function () {
},
];
$config->setValidationRules($rules);
$this->assertSame($rules, $config->getValidationRules());
$rules = function() {return [function() {}];};
$rules = function () {
return [function () {
},
];
};
$config->setValidationRules($rules);
$this->assertSame($rules, $config->getValidationRules());
}
@ -126,7 +137,8 @@ class ServerConfigTest extends TestCase
{
$config = ServerConfig::create();
$resolver = function() {};
$resolver = function () {
};
$config->setFieldResolver($resolver);
$this->assertSame($resolver, $config->getFieldResolver());
@ -139,7 +151,8 @@ class ServerConfigTest extends TestCase
{
$config = ServerConfig::create();
$loader = function() {};
$loader = function () {
};
$config->setPersistentQueryLoader($loader);
$this->assertSame($loader, $config->getPersistentQueryLoader());
@ -162,16 +175,21 @@ class ServerConfigTest extends TestCase
public function testAcceptsArray() : void
{
$arr = [
'schema' => new \GraphQL\Type\Schema([
'query' => new ObjectType(['name' => 't', 'fields' => ['a' => Type::string()]])
'schema' => new Schema([
'query' => new ObjectType(['name' => 't', 'fields' => ['a' => Type::string()]]),
]),
'context' => new \stdClass(),
'rootValue' => new \stdClass(),
'errorFormatter' => function() {},
'errorFormatter' => function () {
},
'promiseAdapter' => new SyncPromiseAdapter(),
'validationRules' => [function() {}],
'fieldResolver' => function() {},
'persistentQueryLoader' => function() {},
'validationRules' => [function () {
},
],
'fieldResolver' => function () {
},
'persistentQueryLoader' => function () {
},
'debug' => true,
'queryBatching' => true,
];
@ -192,9 +210,7 @@ class ServerConfigTest extends TestCase
public function testThrowsOnInvalidArrayKey() : void
{
$arr = [
'missingKey' => 'value'
];
$arr = ['missingKey' => 'value'];
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('Unknown server config option "missingKey"');

View File

@ -1,6 +1,8 @@
<?php
namespace GraphQL\Tests\Server;
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Deferred;
use GraphQL\Error\UserError;
@ -8,6 +10,10 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;
use function trigger_error;
use const E_USER_DEPRECATED;
use const E_USER_NOTICE;
use const E_USER_WARNING;
abstract class ServerTestCase extends TestCase
{
@ -21,7 +27,7 @@ abstract class ServerTestCase extends TestCase
'type' => Type::string(),
'resolve' => function ($root, $args, $context, $info) {
return $info->fieldName;
}
},
],
'fieldWithPhpError' => [
'type' => Type::string(),
@ -31,38 +37,40 @@ abstract class ServerTestCase extends TestCase
trigger_error('warning', E_USER_WARNING);
$a = [];
$a['test']; // should produce PHP notice
return $info->fieldName;
}
},
],
'fieldWithException' => [
'type' => Type::string(),
'resolve' => function ($root, $args, $context, $info) {
throw new UserError("This is the exception we want");
}
throw new UserError('This is the exception we want');
},
],
'testContextAndRootValue' => [
'type' => Type::string(),
'resolve' => function ($root, $args, $context, $info) {
$context->testedRootValue = $root;
return $info->fieldName;
}
},
],
'fieldWithArg' => [
'type' => Type::string(),
'args' => [
'arg' => [
'type' => Type::nonNull(Type::string())
'type' => Type::nonNull(Type::string()),
],
],
'resolve' => function ($root, $args) {
return $args['arg'];
}
},
],
'dfd' => [
'type' => Type::string(),
'args' => [
'num' => [
'type' => Type::nonNull(Type::int())
'type' => Type::nonNull(Type::int()),
],
],
'resolve' => function ($root, $args, $context) {
@ -71,9 +79,9 @@ abstract class ServerTestCase extends TestCase
return new Deferred(function () use ($args, $context) {
return $context['load']($args['num']);
});
}
]
]
},
],
],
]),
'mutation' => new ObjectType([
'name' => 'Mutation',
@ -82,13 +90,14 @@ abstract class ServerTestCase extends TestCase
'type' => new ObjectType([
'name' => 'TestMutation',
'fields' => [
'result' => Type::string()
]
])
]
]
])
'result' => Type::string(),
],
]),
],
],
]),
]);
return $schema;
}
}

View File

@ -1,4 +1,7 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Executor\ExecutionResult;
@ -6,13 +9,11 @@ use GraphQL\Server\Helper;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
use function json_encode;
class StandardServerTest extends ServerTestCase
{
/**
* @var ServerConfig
*/
/** @var ServerConfig */
private $config;
public function setUp()
@ -24,88 +25,82 @@ class StandardServerTest extends ServerTestCase
public function testSimpleRequestExecutionWithOutsideParsing() : void
{
$body = json_encode([
'query' => '{f1}'
]);
$body = json_encode(['query' => '{f1}']);
$parsedBody = $this->parseRawRequest('application/json', $body);
$server = new StandardServer($this->config);
$result = $server->executeRequest($parsedBody);
$expected = [
'data' => [
'f1' => 'f1',
]
'data' => ['f1' => 'f1'],
];
$this->assertEquals($expected, $result->toArray(true));
}
public function testSimplePsrRequestExecution() : void
{
$body = [
'query' => '{f1}'
];
$expected = [
'data' => [
'f1' => 'f1'
]
];
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}
public function testMultipleOperationPsrRequestExecution() : void
{
$body = [
'query' => 'query firstOp {fieldWithPhpError} query secondOp {f1}',
'operationName' => 'secondOp'
];
$expected = [
'data' => [
'f1' => 'f1'
]
];
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}
private function executePsrRequest($psrRequest)
{
$server = new StandardServer($this->config);
$result = $server->executePsrRequest($psrRequest);
$this->assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
private function assertPsrRequestEquals($expected, $request)
{
$result = $this->executePsrRequest($request);
$this->assertArraySubset($expected, $result->toArray(true));
return $result;
}
private function preparePsrRequest($contentType, $parsedBody)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $parsedBody;
return $psrRequest;
}
private function parseRawRequest($contentType, $content, $method = 'POST')
{
$_SERVER['CONTENT_TYPE'] = $contentType;
$_SERVER['REQUEST_METHOD'] = $method;
$helper = new Helper();
return $helper->parseHttpRequest(function () use ($content) {
return $content;
});
}
public function testSimplePsrRequestExecution() : void
{
$body = ['query' => '{f1}'];
$expected = [
'data' => ['f1' => 'f1'],
];
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}
private function preparePsrRequest($contentType, $parsedBody)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $parsedBody;
return $psrRequest;
}
private function assertPsrRequestEquals($expected, $request)
{
$result = $this->executePsrRequest($request);
$this->assertArraySubset($expected, $result->toArray(true));
return $result;
}
private function executePsrRequest($psrRequest)
{
$server = new StandardServer($this->config);
$result = $server->executePsrRequest($psrRequest);
$this->assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
public function testMultipleOperationPsrRequestExecution() : void
{
$body = [
'query' => 'query firstOp {fieldWithPhpError} query secondOp {f1}',
'operationName' => 'secondOp',
];
$expected = [
'data' => ['f1' => 'f1'],
];
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}
}