2017-07-17 12:57:30 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Server;
|
|
|
|
|
|
|
|
use GraphQL\Server\Helper;
|
|
|
|
use GraphQL\Server\OperationParams;
|
|
|
|
|
|
|
|
class RequestValidationTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testSimpleRequestShouldValidate()
|
|
|
|
{
|
|
|
|
$query = '{my q}';
|
|
|
|
$variables = ['a' => 'b', 'c' => 'd'];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequestWithQueryIdShouldValidate()
|
|
|
|
{
|
|
|
|
$queryId = 'some-query-id';
|
|
|
|
$variables = ['a' => 'b', 'c' => 'd'];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequiresQueryOrQueryId()
|
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'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"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsWhenBothQueryAndQueryIdArePresent()
|
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'query' => '{my query}',
|
|
|
|
'queryId' => 'my-query-id',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertInputError(
|
|
|
|
$parsedBody,
|
|
|
|
'GraphQL Request parameters "query" and "queryId" are mutually exclusive'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsWhenQueryParameterIsNotString()
|
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'query' => ['t' => '{my query}']
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsWhenQueryIdParameterIsNotString()
|
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'queryId' => ['t' => '{my query}']
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsWhenOperationParameterIsNotString()
|
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'query' => '{my query}',
|
2018-03-04 02:41:55 +03:00
|
|
|
'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
|
|
|
|
*/
|
|
|
|
public function testIgnoresNullAndEmptyStringVariables()
|
|
|
|
{
|
|
|
|
$query = '{my q}';
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => null
|
|
|
|
]);
|
|
|
|
$this->assertValid($parsedBody);
|
|
|
|
|
|
|
|
$variables = "";
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables
|
|
|
|
]);
|
|
|
|
$this->assertValid($parsedBody);
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:57:30 +03:00
|
|
|
public function testFailsWhenVariablesParameterIsNotObject()
|
|
|
|
{
|
|
|
|
$parsedBody = OperationParams::create([
|
|
|
|
'query' => '{my query}',
|
2017-07-19 15:30:39 +03:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
|
|
|
private function assertValid($parsedRequest)
|
|
|
|
{
|
|
|
|
$helper = new Helper();
|
2017-07-19 15:30:39 +03:00
|
|
|
$errors = $helper->validateOperationParams($parsedRequest);
|
2017-07-17 12:57:30 +03:00
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
if (!empty($errors)) {
|
|
|
|
throw $errors[0];
|
2017-07-17 12:57:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
private function assertInputError($parsedRequest, $expectedMessage)
|
2017-07-17 12:57:30 +03:00
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$helper = new Helper();
|
|
|
|
$errors = $helper->validateOperationParams($parsedRequest);
|
|
|
|
if (!empty($errors[0])) {
|
|
|
|
$this->assertEquals($expectedMessage, $errors[0]->getMessage());
|
|
|
|
} else {
|
|
|
|
$this->fail('Expected error not returned');
|
2017-07-17 12:57:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|