Server: fixed constructor screwed during rebasing + restored tests for the server

This commit is contained in:
Vladimir Razuvaev 2017-08-21 01:16:55 +07:00
parent a1652468f0
commit d95fb461ee
4 changed files with 204 additions and 82 deletions

View File

@ -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();
}
/**

View File

@ -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);
}

View 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
View 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;
}
}