From d95fb461eefe23f357fa6471f5b30e00a04ac974 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Mon, 21 Aug 2017 01:16:55 +0700 Subject: [PATCH] Server: fixed constructor screwed during rebasing + restored tests for the server --- src/Server/StandardServer.php | 3 +- tests/Server/QueryExecutionTest.php | 83 +-------------------- tests/Server/StandardServerTest.php | 107 ++++++++++++++++++++++++++++ tests/Server/TestCase.php | 93 ++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 82 deletions(-) create mode 100644 tests/Server/StandardServerTest.php create mode 100644 tests/Server/TestCase.php diff --git a/src/Server/StandardServer.php b/src/Server/StandardServer.php index 1c67ec4..9684205 100644 --- a/src/Server/StandardServer.php +++ b/src/Server/StandardServer.php @@ -79,7 +79,8 @@ class StandardServer if (!$config instanceof ServerConfig) { throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config)); } - return new static($config); + $this->config = $config; + $this->helper = new Helper(); } /** diff --git a/tests/Server/QueryExecutionTest.php b/tests/Server/QueryExecutionTest.php index 16c99a7..75982cb 100644 --- a/tests/Server/QueryExecutionTest.php +++ b/tests/Server/QueryExecutionTest.php @@ -9,18 +9,15 @@ use GraphQL\Error\UserError; use GraphQL\Executor\ExecutionResult; use GraphQL\Language\AST\DocumentNode; use GraphQL\Language\Parser; -use GraphQL\Schema; use GraphQL\Server\Helper; use GraphQL\Server\OperationParams; use GraphQL\Server\RequestError; use GraphQL\Server\ServerConfig; -use GraphQL\Type\Definition\ObjectType; -use GraphQL\Type\Definition\Type; use GraphQL\Validator\DocumentValidator; use GraphQL\Validator\Rules\CustomValidationRule; use GraphQL\Validator\ValidationContext; -class QueryExecutionTest extends \PHPUnit_Framework_TestCase +class QueryExecutionTest extends TestCase { /** * @var ServerConfig @@ -29,83 +26,7 @@ class QueryExecutionTest extends \PHPUnit_Framework_TestCase public function setUp() { - $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() - ] - ]) - ] - ] - ]) - ]); - + $schema = $this->buildSchema(); $this->config = ServerConfig::create() ->setSchema($schema); } diff --git a/tests/Server/StandardServerTest.php b/tests/Server/StandardServerTest.php new file mode 100644 index 0000000..6fa2ceb --- /dev/null +++ b/tests/Server/StandardServerTest.php @@ -0,0 +1,107 @@ +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; + }); + } +} diff --git a/tests/Server/TestCase.php b/tests/Server/TestCase.php new file mode 100644 index 0000000..c9dcca6 --- /dev/null +++ b/tests/Server/TestCase.php @@ -0,0 +1,93 @@ + 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; + } +}