2017-07-17 12:57:30 +03:00
|
|
|
<?php
|
2018-09-01 23:00:00 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-07-17 12:57:30 +03:00
|
|
|
namespace GraphQL\Tests\Server;
|
|
|
|
|
|
|
|
use GraphQL\Server\Helper;
|
|
|
|
use GraphQL\Server\OperationParams;
|
2018-07-29 18:43:10 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-07-17 12:57:30 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class RequestValidationTest extends TestCase
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testSimpleRequestShouldValidate() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my q}';
|
2017-07-17 12:57:30 +03:00
|
|
|
$variables = ['a' => 'b', 'c' => 'd'];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
2018-03-04 02:41:55 +03:00
|
|
|
'operationName' => $operation,
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertValid($parsedBody);
|
|
|
|
}
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
private function assertValid($parsedRequest)
|
|
|
|
{
|
|
|
|
$helper = new Helper();
|
|
|
|
$errors = $helper->validateOperationParams($parsedRequest);
|
|
|
|
$this->assertEmpty($errors, isset($errors[0]) ? $errors[0]->getMessage() : '');
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testRequestWithQueryIdShouldValidate() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$queryId = 'some-query-id';
|
2017-07-17 12:57:30 +03:00
|
|
|
$variables = ['a' => 'b', 'c' => 'd'];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'queryId' => $queryId,
|
|
|
|
'variables' => $variables,
|
2018-03-04 02:41:55 +03:00
|
|
|
'operationName' => $operation,
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertValid($parsedBody);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testRequiresQueryOrQueryId() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'variables' => ['foo' => 'bar'],
|
2018-03-04 02:41:55 +03:00
|
|
|
'operationName' => 'op',
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$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])) {
|
|
|
|
$this->assertEquals($expectedMessage, $errors[0]->getMessage());
|
|
|
|
} else {
|
|
|
|
$this->fail('Expected error not returned');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWhenBothQueryAndQueryIdArePresent() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => '{my query}',
|
2017-07-17 12:57:30 +03:00
|
|
|
'queryId' => 'my-query-id',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInputError(
|
|
|
|
$parsedBody,
|
|
|
|
'GraphQL Request parameters "query" and "queryId" are mutually exclusive'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWhenQueryParameterIsNotString() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => ['t' => '{my query}'],
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInputError(
|
|
|
|
$parsedBody,
|
2018-02-13 18:51:44 +03:00
|
|
|
'GraphQL Request parameter "query" must be string, but got {"t":"{my query}"}'
|
2017-07-17 12:57:30 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWhenQueryIdParameterIsNotString() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'queryId' => ['t' => '{my query}'],
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInputError(
|
|
|
|
$parsedBody,
|
2018-02-13 18:51:44 +03:00
|
|
|
'GraphQL Request parameter "queryId" must be string, but got {"t":"{my query}"}'
|
2017-07-17 12:57:30 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWhenOperationParameterIsNotString() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => '{my query}',
|
|
|
|
'operationName' => [],
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInputError(
|
|
|
|
$parsedBody,
|
2018-02-13 18:51:44 +03:00
|
|
|
'GraphQL Request parameter "operation" must be string, but got []'
|
2017-07-17 12:57:30 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:26:45 +03:00
|
|
|
/**
|
|
|
|
* @see https://github.com/webonyx/graphql-php/issues/156
|
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIgnoresNullAndEmptyStringVariables() : void
|
2017-08-30 19:26:45 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my q}';
|
2017-08-30 19:26:45 +03:00
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => null,
|
2017-08-30 19:26:45 +03:00
|
|
|
]);
|
|
|
|
$this->assertValid($parsedBody);
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$variables = '';
|
2017-08-30 19:26:45 +03:00
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
2017-08-30 19:26:45 +03:00
|
|
|
]);
|
|
|
|
$this->assertValid($parsedBody);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWhenVariablesParameterIsNotObject() : void
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => '{my query}',
|
|
|
|
'variables' => 0,
|
2017-07-17 12:57:30 +03:00
|
|
|
]);
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->assertInputError(
|
|
|
|
$parsedBody,
|
|
|
|
'GraphQL Request parameter "variables" must be object or JSON string parsed to object, but got 0'
|
|
|
|
);
|
2017-07-17 12:57:30 +03:00
|
|
|
}
|
|
|
|
}
|