Skip to content

Commit d95fb46

Browse files
committed
Server: fixed constructor screwed during rebasing + restored tests for the server
1 parent a165246 commit d95fb46

File tree

4 files changed

+204
-82
lines changed

4 files changed

+204
-82
lines changed

src/Server/StandardServer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public function __construct($config)
7979
if (!$config instanceof ServerConfig) {
8080
throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config));
8181
}
82-
return new static($config);
82+
$this->config = $config;
83+
$this->helper = new Helper();
8384
}
8485

8586
/**

tests/Server/QueryExecutionTest.php

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@
99
use GraphQL\Executor\ExecutionResult;
1010
use GraphQL\Language\AST\DocumentNode;
1111
use GraphQL\Language\Parser;
12-
use GraphQL\Schema;
1312
use GraphQL\Server\Helper;
1413
use GraphQL\Server\OperationParams;
1514
use GraphQL\Server\RequestError;
1615
use GraphQL\Server\ServerConfig;
17-
use GraphQL\Type\Definition\ObjectType;
18-
use GraphQL\Type\Definition\Type;
1916
use GraphQL\Validator\DocumentValidator;
2017
use GraphQL\Validator\Rules\CustomValidationRule;
2118
use GraphQL\Validator\ValidationContext;
2219

23-
class QueryExecutionTest extends \PHPUnit_Framework_TestCase
20+
class QueryExecutionTest extends TestCase
2421
{
2522
/**
2623
* @var ServerConfig
@@ -29,83 +26,7 @@ class QueryExecutionTest extends \PHPUnit_Framework_TestCase
2926

3027
public function setUp()
3128
{
32-
$schema = new Schema([
33-
'query' => new ObjectType([
34-
'name' => 'Query',
35-
'fields' => [
36-
'f1' => [
37-
'type' => Type::string(),
38-
'resolve' => function($root, $args, $context, $info) {
39-
return $info->fieldName;
40-
}
41-
],
42-
'fieldWithPhpError' => [
43-
'type' => Type::string(),
44-
'resolve' => function($root, $args, $context, $info) {
45-
trigger_error('deprecated', E_USER_DEPRECATED);
46-
trigger_error('notice', E_USER_NOTICE);
47-
trigger_error('warning', E_USER_WARNING);
48-
$a = [];
49-
$a['test']; // should produce PHP notice
50-
return $info->fieldName;
51-
}
52-
],
53-
'fieldWithException' => [
54-
'type' => Type::string(),
55-
'resolve' => function($root, $args, $context, $info) {
56-
throw new UserError("This is the exception we want");
57-
}
58-
],
59-
'testContextAndRootValue' => [
60-
'type' => Type::string(),
61-
'resolve' => function($root, $args, $context, $info) {
62-
$context->testedRootValue = $root;
63-
return $info->fieldName;
64-
}
65-
],
66-
'fieldWithArg' => [
67-
'type' => Type::string(),
68-
'args' => [
69-
'arg' => [
70-
'type' => Type::nonNull(Type::string())
71-
],
72-
],
73-
'resolve' => function($root, $args) {
74-
return $args['arg'];
75-
}
76-
],
77-
'dfd' => [
78-
'type' => Type::string(),
79-
'args' => [
80-
'num' => [
81-
'type' => Type::nonNull(Type::int())
82-
],
83-
],
84-
'resolve' => function($root, $args, $context) {
85-
$context['buffer']($args['num']);
86-
87-
return new Deferred(function() use ($args, $context) {
88-
return $context['load']($args['num']);
89-
});
90-
}
91-
]
92-
]
93-
]),
94-
'mutation' => new ObjectType([
95-
'name' => 'Mutation',
96-
'fields' => [
97-
'm1' => [
98-
'type' => new ObjectType([
99-
'name' => 'TestMutation',
100-
'fields' => [
101-
'result' => Type::string()
102-
]
103-
])
104-
]
105-
]
106-
])
107-
]);
108-
29+
$schema = $this->buildSchema();
10930
$this->config = ServerConfig::create()
11031
->setSchema($schema);
11132
}

tests/Server/StandardServerTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
namespace GraphQL\Tests\Server;
3+
4+
use GraphQL\Executor\ExecutionResult;
5+
use GraphQL\Server\Helper;
6+
use GraphQL\Server\ServerConfig;
7+
use GraphQL\Server\StandardServer;
8+
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
9+
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
10+
11+
class StandardServerTest extends TestCase
12+
{
13+
/**
14+
* @var ServerConfig
15+
*/
16+
private $config;
17+
18+
public function setUp()
19+
{
20+
$schema = $this->buildSchema();
21+
$this->config = ServerConfig::create()
22+
->setSchema($schema);
23+
}
24+
25+
public function testSimpleRequestExecutionWithOutsideParsing()
26+
{
27+
$body = json_encode([
28+
'query' => '{f1}'
29+
]);
30+
31+
$parsedBody = $this->parseRawRequest('application/json', $body);
32+
$server = new StandardServer($this->config);
33+
34+
$result = $server->executeRequest($parsedBody);
35+
$expected = [
36+
'data' => [
37+
'f1' => 'f1',
38+
]
39+
];
40+
41+
$this->assertEquals($expected, $result->toArray(true));
42+
}
43+
44+
public function testSimplePsrRequestExecution()
45+
{
46+
$body = json_encode([
47+
'query' => '{f1}'
48+
]);
49+
50+
$request = $this->preparePsrRequest('application/json', $body);
51+
52+
$expected = [
53+
'data' => [
54+
'f1' => 'f1'
55+
]
56+
];
57+
58+
$this->assertPsrRequestEquals($expected, $request);
59+
}
60+
61+
private function executePsrRequest($psrRequest)
62+
{
63+
$server = new StandardServer($this->config);
64+
$result = $server->executePsrRequest($psrRequest);
65+
$this->assertInstanceOf(ExecutionResult::class, $result);
66+
return $result;
67+
}
68+
69+
private function assertPsrRequestEquals($expected, $request)
70+
{
71+
$result = $this->executePsrRequest($request);
72+
$this->assertArraySubset($expected, $result->toArray(true));
73+
return $result;
74+
}
75+
76+
private function preparePsrRequest($contentType, $content, $method = 'POST')
77+
{
78+
$psrRequestBody = new PsrStreamStub();
79+
$psrRequestBody->content = $content;
80+
81+
$psrRequest = new PsrRequestStub();
82+
$psrRequest->headers['content-type'] = [$contentType];
83+
$psrRequest->method = $method;
84+
$psrRequest->body = $psrRequestBody;
85+
86+
if ($contentType === 'application/json') {
87+
$parsedBody = json_decode($content, true);
88+
$parsedBody = $parsedBody === false ? null : $parsedBody;
89+
} else {
90+
$parsedBody = null;
91+
}
92+
93+
$psrRequest->parsedBody = $parsedBody;
94+
return $psrRequest;
95+
}
96+
97+
private function parseRawRequest($contentType, $content, $method = 'POST')
98+
{
99+
$_SERVER['CONTENT_TYPE'] = $contentType;
100+
$_SERVER['REQUEST_METHOD'] = $method;
101+
102+
$helper = new Helper();
103+
return $helper->parseHttpRequest(function() use ($content) {
104+
return $content;
105+
});
106+
}
107+
}

tests/Server/TestCase.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace GraphQL\Tests\Server;
3+
4+
5+
use GraphQL\Deferred;
6+
use GraphQL\Error\UserError;
7+
use GraphQL\Type\Definition\ObjectType;
8+
use GraphQL\Type\Definition\Type;
9+
use GraphQL\Type\Schema;
10+
11+
abstract class TestCase extends \PHPUnit_Framework_TestCase
12+
{
13+
protected function buildSchema()
14+
{
15+
$schema = new Schema([
16+
'query' => new ObjectType([
17+
'name' => 'Query',
18+
'fields' => [
19+
'f1' => [
20+
'type' => Type::string(),
21+
'resolve' => function($root, $args, $context, $info) {
22+
return $info->fieldName;
23+
}
24+
],
25+
'fieldWithPhpError' => [
26+
'type' => Type::string(),
27+
'resolve' => function($root, $args, $context, $info) {
28+
trigger_error('deprecated', E_USER_DEPRECATED);
29+
trigger_error('notice', E_USER_NOTICE);
30+
trigger_error('warning', E_USER_WARNING);
31+
$a = [];
32+
$a['test']; // should produce PHP notice
33+
return $info->fieldName;
34+
}
35+
],
36+
'fieldWithException' => [
37+
'type' => Type::string(),
38+
'resolve' => function($root, $args, $context, $info) {
39+
throw new UserError("This is the exception we want");
40+
}
41+
],
42+
'testContextAndRootValue' => [
43+
'type' => Type::string(),
44+
'resolve' => function($root, $args, $context, $info) {
45+
$context->testedRootValue = $root;
46+
return $info->fieldName;
47+
}
48+
],
49+
'fieldWithArg' => [
50+
'type' => Type::string(),
51+
'args' => [
52+
'arg' => [
53+
'type' => Type::nonNull(Type::string())
54+
],
55+
],
56+
'resolve' => function($root, $args) {
57+
return $args['arg'];
58+
}
59+
],
60+
'dfd' => [
61+
'type' => Type::string(),
62+
'args' => [
63+
'num' => [
64+
'type' => Type::nonNull(Type::int())
65+
],
66+
],
67+
'resolve' => function($root, $args, $context) {
68+
$context['buffer']($args['num']);
69+
70+
return new Deferred(function() use ($args, $context) {
71+
return $context['load']($args['num']);
72+
});
73+
}
74+
]
75+
]
76+
]),
77+
'mutation' => new ObjectType([
78+
'name' => 'Mutation',
79+
'fields' => [
80+
'm1' => [
81+
'type' => new ObjectType([
82+
'name' => 'TestMutation',
83+
'fields' => [
84+
'result' => Type::string()
85+
]
86+
])
87+
]
88+
]
89+
])
90+
]);
91+
return $schema;
92+
}
93+
}

0 commit comments

Comments
 (0)