-
-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathStandardServerTest.php
More file actions
124 lines (99 loc) · 3.51 KB
/
Copy pathStandardServerTest.php
File metadata and controls
124 lines (99 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php declare(strict_types=1);
namespace GraphQL\Tests\Server;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Server\Helper;
use GraphQL\Server\OperationParams;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use Nyholm\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use function Safe\json_encode;
final class StandardServerTest extends ServerTestCase
{
use ArraySubsetAsserts;
/** @var ServerConfig */
private $config;
protected function setUp(): void
{
$schema = $this->buildSchema();
$this->config = ServerConfig::create()
->setSchema($schema);
}
public function testSimpleRequestExecutionWithOutsideParsing(): void
{
$body = json_encode(['query' => '{f1}'], JSON_THROW_ON_ERROR);
$parsedBody = $this->parseRawRequest('application/json', $body);
$server = new StandardServer($this->config);
$result = $server->executeRequest($parsedBody);
self::assertInstanceOf(ExecutionResult::class, $result);
self::assertSame(
[
'data' => ['f1' => 'f1'],
],
$result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE)
);
}
/** @throws \GraphQL\Server\RequestError */
private function parseRawRequest(string $contentType, string $content, string $method = 'POST'): OperationParams
{
$_SERVER['CONTENT_TYPE'] = $contentType;
$_SERVER['REQUEST_METHOD'] = $method;
$helper = new Helper();
$operationParams = $helper->parseHttpRequest(static fn () => $content);
self::assertInstanceOf(OperationParams::class, $operationParams);
return $operationParams;
}
public function testSimplePsrRequestExecution(): void
{
$body = ['query' => '{f1}'];
$expected = [
'data' => ['f1' => 'f1'],
];
$request = $this->preparePsrRequest('application/json', json_encode($body, JSON_THROW_ON_ERROR));
$this->assertPsrRequestEquals($expected, $request);
}
private function preparePsrRequest(string $contentType, string $body): RequestInterface
{
return new Request(
'POST',
'',
['Content-Type' => $contentType],
$body
);
}
/**
* @param array<string, mixed> $expected
*
* @throws \Exception
*/
private function assertPsrRequestEquals(array $expected, RequestInterface $request): ExecutionResult
{
$result = $this->executePsrRequest($request);
self::assertArraySubset($expected, $result->toArray(DebugFlag::INCLUDE_DEBUG_MESSAGE));
return $result;
}
/**
* @throws \Exception
* @throws \JsonException
*/
private function executePsrRequest(RequestInterface $psrRequest): ExecutionResult
{
$result = (new StandardServer($this->config))->executePsrRequest($psrRequest);
self::assertInstanceOf(ExecutionResult::class, $result);
return $result;
}
public function testMultipleOperationPsrRequestExecution(): void
{
$body = [
'query' => 'query firstOp {fieldWithPhpError} query secondOp {f1}',
'operationName' => 'secondOp',
];
$expected = [
'data' => ['f1' => 'f1'],
];
$request = $this->preparePsrRequest('application/json', json_encode($body, JSON_THROW_ON_ERROR));
$this->assertPsrRequestEquals($expected, $request);
}
}