mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-11 10:09:24 +03:00
Server: fixed constructor screwed during rebasing + restored tests for the server
This commit is contained in:
parent
a1652468f0
commit
d95fb461ee
@ -79,7 +79,8 @@ class StandardServer
|
|||||||
if (!$config instanceof ServerConfig) {
|
if (!$config instanceof ServerConfig) {
|
||||||
throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config));
|
throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config));
|
||||||
}
|
}
|
||||||
return new static($config);
|
$this->config = $config;
|
||||||
|
$this->helper = new Helper();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -9,18 +9,15 @@ use GraphQL\Error\UserError;
|
|||||||
use GraphQL\Executor\ExecutionResult;
|
use GraphQL\Executor\ExecutionResult;
|
||||||
use GraphQL\Language\AST\DocumentNode;
|
use GraphQL\Language\AST\DocumentNode;
|
||||||
use GraphQL\Language\Parser;
|
use GraphQL\Language\Parser;
|
||||||
use GraphQL\Schema;
|
|
||||||
use GraphQL\Server\Helper;
|
use GraphQL\Server\Helper;
|
||||||
use GraphQL\Server\OperationParams;
|
use GraphQL\Server\OperationParams;
|
||||||
use GraphQL\Server\RequestError;
|
use GraphQL\Server\RequestError;
|
||||||
use GraphQL\Server\ServerConfig;
|
use GraphQL\Server\ServerConfig;
|
||||||
use GraphQL\Type\Definition\ObjectType;
|
|
||||||
use GraphQL\Type\Definition\Type;
|
|
||||||
use GraphQL\Validator\DocumentValidator;
|
use GraphQL\Validator\DocumentValidator;
|
||||||
use GraphQL\Validator\Rules\CustomValidationRule;
|
use GraphQL\Validator\Rules\CustomValidationRule;
|
||||||
use GraphQL\Validator\ValidationContext;
|
use GraphQL\Validator\ValidationContext;
|
||||||
|
|
||||||
class QueryExecutionTest extends \PHPUnit_Framework_TestCase
|
class QueryExecutionTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var ServerConfig
|
* @var ServerConfig
|
||||||
@ -29,83 +26,7 @@ class QueryExecutionTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$schema = new Schema([
|
$schema = $this->buildSchema();
|
||||||
'query' => new ObjectType([
|
|
||||||
'name' => 'Query',
|
|
||||||
'fields' => [
|
|
||||||
'f1' => [
|
|
||||||
'type' => Type::string(),
|
|
||||||
'resolve' => function($root, $args, $context, $info) {
|
|
||||||
return $info->fieldName;
|
|
||||||
}
|
|
||||||
],
|
|
||||||
'fieldWithPhpError' => [
|
|
||||||
'type' => Type::string(),
|
|
||||||
'resolve' => function($root, $args, $context, $info) {
|
|
||||||
trigger_error('deprecated', E_USER_DEPRECATED);
|
|
||||||
trigger_error('notice', E_USER_NOTICE);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
],
|
|
||||||
'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())
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'resolve' => function($root, $args) {
|
|
||||||
return $args['arg'];
|
|
||||||
}
|
|
||||||
],
|
|
||||||
'dfd' => [
|
|
||||||
'type' => Type::string(),
|
|
||||||
'args' => [
|
|
||||||
'num' => [
|
|
||||||
'type' => Type::nonNull(Type::int())
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'resolve' => function($root, $args, $context) {
|
|
||||||
$context['buffer']($args['num']);
|
|
||||||
|
|
||||||
return new Deferred(function() use ($args, $context) {
|
|
||||||
return $context['load']($args['num']);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
]
|
|
||||||
]
|
|
||||||
]),
|
|
||||||
'mutation' => new ObjectType([
|
|
||||||
'name' => 'Mutation',
|
|
||||||
'fields' => [
|
|
||||||
'm1' => [
|
|
||||||
'type' => new ObjectType([
|
|
||||||
'name' => 'TestMutation',
|
|
||||||
'fields' => [
|
|
||||||
'result' => Type::string()
|
|
||||||
]
|
|
||||||
])
|
|
||||||
]
|
|
||||||
]
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->config = ServerConfig::create()
|
$this->config = ServerConfig::create()
|
||||||
->setSchema($schema);
|
->setSchema($schema);
|
||||||
}
|
}
|
||||||
|
107
tests/Server/StandardServerTest.php
Normal file
107
tests/Server/StandardServerTest.php
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
namespace GraphQL\Tests\Server;
|
||||||
|
|
||||||
|
use GraphQL\Executor\ExecutionResult;
|
||||||
|
use GraphQL\Server\Helper;
|
||||||
|
use GraphQL\Server\ServerConfig;
|
||||||
|
use GraphQL\Server\StandardServer;
|
||||||
|
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
|
||||||
|
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
|
||||||
|
|
||||||
|
class StandardServerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ServerConfig
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$schema = $this->buildSchema();
|
||||||
|
$this->config = ServerConfig::create()
|
||||||
|
->setSchema($schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSimpleRequestExecutionWithOutsideParsing()
|
||||||
|
{
|
||||||
|
$body = json_encode([
|
||||||
|
'query' => '{f1}'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$parsedBody = $this->parseRawRequest('application/json', $body);
|
||||||
|
$server = new StandardServer($this->config);
|
||||||
|
|
||||||
|
$result = $server->executeRequest($parsedBody);
|
||||||
|
$expected = [
|
||||||
|
'data' => [
|
||||||
|
'f1' => 'f1',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $result->toArray(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSimplePsrRequestExecution()
|
||||||
|
{
|
||||||
|
$body = json_encode([
|
||||||
|
'query' => '{f1}'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$request = $this->preparePsrRequest('application/json', $body);
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
'data' => [
|
||||||
|
'f1' => 'f1'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
$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, $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;
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
93
tests/Server/TestCase.php
Normal file
93
tests/Server/TestCase.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
namespace GraphQL\Tests\Server;
|
||||||
|
|
||||||
|
|
||||||
|
use GraphQL\Deferred;
|
||||||
|
use GraphQL\Error\UserError;
|
||||||
|
use GraphQL\Type\Definition\ObjectType;
|
||||||
|
use GraphQL\Type\Definition\Type;
|
||||||
|
use GraphQL\Type\Schema;
|
||||||
|
|
||||||
|
abstract class TestCase extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected function buildSchema()
|
||||||
|
{
|
||||||
|
$schema = new Schema([
|
||||||
|
'query' => new ObjectType([
|
||||||
|
'name' => 'Query',
|
||||||
|
'fields' => [
|
||||||
|
'f1' => [
|
||||||
|
'type' => Type::string(),
|
||||||
|
'resolve' => function($root, $args, $context, $info) {
|
||||||
|
return $info->fieldName;
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'fieldWithPhpError' => [
|
||||||
|
'type' => Type::string(),
|
||||||
|
'resolve' => function($root, $args, $context, $info) {
|
||||||
|
trigger_error('deprecated', E_USER_DEPRECATED);
|
||||||
|
trigger_error('notice', E_USER_NOTICE);
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'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())
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'resolve' => function($root, $args) {
|
||||||
|
return $args['arg'];
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'dfd' => [
|
||||||
|
'type' => Type::string(),
|
||||||
|
'args' => [
|
||||||
|
'num' => [
|
||||||
|
'type' => Type::nonNull(Type::int())
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'resolve' => function($root, $args, $context) {
|
||||||
|
$context['buffer']($args['num']);
|
||||||
|
|
||||||
|
return new Deferred(function() use ($args, $context) {
|
||||||
|
return $context['load']($args['num']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]),
|
||||||
|
'mutation' => new ObjectType([
|
||||||
|
'name' => 'Mutation',
|
||||||
|
'fields' => [
|
||||||
|
'm1' => [
|
||||||
|
'type' => new ObjectType([
|
||||||
|
'name' => 'TestMutation',
|
||||||
|
'fields' => [
|
||||||
|
'result' => Type::string()
|
||||||
|
]
|
||||||
|
])
|
||||||
|
]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
return $schema;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user