graphql-php/tests/Server/RequestValidationTest.php

157 lines
4.4 KiB
PHP
Raw Permalink Normal View History

<?php
2018-09-01 23:00:00 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Server\Helper;
use GraphQL\Server\OperationParams;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-07-29 18:43:10 +03:00
class RequestValidationTest extends TestCase
{
public function testSimpleRequestShouldValidate() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my q}';
$variables = ['a' => 'b', 'c' => 'd'];
$operation = 'op';
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
]);
2018-09-19 18:12:09 +03:00
self::assertValid($parsedBody);
}
2018-09-19 18:12:09 +03:00
private static function assertValid($parsedRequest)
2018-09-01 23:00:00 +03:00
{
$helper = new Helper();
$errors = $helper->validateOperationParams($parsedRequest);
2018-09-19 18:12:09 +03:00
self::assertEmpty($errors, isset($errors[0]) ? $errors[0]->getMessage() : '');
2018-09-01 23:00:00 +03:00
}
public function testRequestWithQueryIdShouldValidate() : void
{
2018-09-01 23:00:00 +03:00
$queryId = 'some-query-id';
$variables = ['a' => 'b', 'c' => 'd'];
$operation = 'op';
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'queryId' => $queryId,
'variables' => $variables,
'operationName' => $operation,
]);
2018-09-19 18:12:09 +03:00
self::assertValid($parsedBody);
}
public function testRequiresQueryOrQueryId() : void
{
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'variables' => ['foo' => 'bar'],
'operationName' => 'op',
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request must include at least one of those two parameters: "query" or "queryId"'
);
}
2018-09-01 23:00:00 +03:00
private function assertInputError($parsedRequest, $expectedMessage)
{
$helper = new Helper();
$errors = $helper->validateOperationParams($parsedRequest);
if (! empty($errors[0])) {
2018-09-19 18:12:09 +03:00
self::assertEquals($expectedMessage, $errors[0]->getMessage());
2018-09-01 23:00:00 +03:00
} else {
self::fail('Expected error not returned');
2018-09-01 23:00:00 +03:00
}
}
public function testFailsWhenBothQueryAndQueryIdArePresent() : void
{
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => '{my query}',
'queryId' => 'my-query-id',
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameters "query" and "queryId" are mutually exclusive'
);
}
public function testFailsWhenQueryParameterIsNotString() : void
{
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => ['t' => '{my query}'],
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "query" must be string, but got {"t":"{my query}"}'
);
}
public function testFailsWhenQueryIdParameterIsNotString() : void
{
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'queryId' => ['t' => '{my query}'],
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "queryId" must be string, but got {"t":"{my query}"}'
);
}
public function testFailsWhenOperationParameterIsNotString() : void
{
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => '{my query}',
'operationName' => [],
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "operation" must be string, but got []'
);
}
/**
* @see https://github.com/webonyx/graphql-php/issues/156
*/
public function testIgnoresNullAndEmptyStringVariables() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my q}';
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => $query,
'variables' => null,
]);
2018-09-19 18:12:09 +03:00
self::assertValid($parsedBody);
2018-09-01 23:00:00 +03:00
$variables = '';
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => $query,
'variables' => $variables,
]);
2018-09-19 18:12:09 +03:00
self::assertValid($parsedBody);
}
public function testFailsWhenVariablesParameterIsNotObject() : void
{
$parsedBody = OperationParams::create([
2018-09-01 23:00:00 +03:00
'query' => '{my query}',
'variables' => 0,
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "variables" must be object or JSON string parsed to object, but got 0'
);
}
}