forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphqlContext.php
178 lines (152 loc) · 5.57 KB
/
GraphqlContext.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\Behat;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behatch\Context\RestContext;
use Behatch\HttpCall\Request;
use GraphQL\Error\Error;
use GraphQL\Type\Introspection;
use PHPUnit\Framework\ExpectationFailedException;
/**
* Context for GraphQL.
*
* @author Alan Poulain <[email protected]>
*/
final class GraphqlContext implements Context
{
private ?RestContext $restContext = null;
private ?JsonContext $jsonContext = null;
private array $graphqlRequest;
private ?int $graphqlLine = null; // @phpstan-ignore-line
public function __construct(private readonly Request $request)
{
}
/**
* Gives access to the Behatch context.
*
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope): void
{
/** @var InitializedContextEnvironment $environment */
$environment = $scope->getEnvironment();
/** @var RestContext $restContext */
$restContext = $environment->getContext(RestContext::class);
$this->restContext = $restContext;
/** @var JsonContext $jsonContext */
$jsonContext = $environment->getContext(JsonContext::class);
$this->jsonContext = $jsonContext;
}
/**
* @When I have the following GraphQL request:
*/
public function IHaveTheFollowingGraphqlRequest(PyStringNode $request): void
{
$this->graphqlRequest = ['query' => $request->getRaw()];
$this->graphqlLine = $request->getLine();
}
/**
* @When I send the following GraphQL request:
*/
public function ISendTheFollowingGraphqlRequest(PyStringNode $request): void
{
$this->IHaveTheFollowingGraphqlRequest($request);
$this->sendGraphqlRequest();
}
/**
* @When I send the GraphQL request with variables:
*/
public function ISendTheGraphqlRequestWithVariables(PyStringNode $variables): void
{
$this->graphqlRequest['variables'] = $variables->getRaw();
$this->sendGraphqlRequest();
}
/**
* @When I send the GraphQL request with operationName :operationName
*/
public function ISendTheGraphqlRequestWithOperation(string $operationName): void
{
$this->graphqlRequest['operationName'] = $operationName;
$this->sendGraphqlRequest();
}
/**
* @Given I have the following file(s) for a GraphQL request:
*/
public function iHaveTheFollowingFilesForAGraphqlRequest(TableNode $table): void
{
$files = [];
foreach ($table->getHash() as $row) {
if (!isset($row['name'], $row['file'])) {
throw new \InvalidArgumentException('You must provide a "name" and "file" column in your table node.');
}
$files[$row['name']] = $this->restContext->getMinkParameter('files_path').\DIRECTORY_SEPARATOR.$row['file'];
}
$this->graphqlRequest['files'] = $files;
}
/**
* @Given I have the following GraphQL multipart request map:
*/
public function iHaveTheFollowingGraphqlMultipartRequestMap(PyStringNode $string): void
{
$this->graphqlRequest['map'] = $string->getRaw();
}
/**
* @When I send the following GraphQL multipart request operations:
*/
public function iSendTheFollowingGraphqlMultipartRequestOperations(PyStringNode $string): void
{
$params = [];
$params['operations'] = $string->getRaw();
$params['map'] = $this->graphqlRequest['map'];
$this->request->setHttpHeader('Content-type', 'multipart/form-data');
$this->request->send('POST', '/graphql', $params, $this->graphqlRequest['files']);
}
/**
* @When I send the query to introspect the schema
*/
public function ISendTheQueryToIntrospectTheSchema(): void
{
$this->graphqlRequest = ['query' => Introspection::getIntrospectionQuery()];
$this->sendGraphqlRequest();
}
/**
* @Then the GraphQL field :fieldName is deprecated for the reason :reason
*/
public function theGraphQLFieldIsDeprecatedForTheReason(string $fieldName, string $reason): void
{
foreach (json_decode($this->request->getContent(), true, 512, \JSON_THROW_ON_ERROR)['data']['__type']['fields'] as $field) {
if ($fieldName === $field['name'] && $field['isDeprecated'] && $reason === $field['deprecationReason']) {
return;
}
}
throw new ExpectationFailedException(sprintf('The field "%s" is not deprecated.', $fieldName));
}
/**
* @Then the GraphQL debug message should be equal to :expectedDebugMessage
*/
public function theGraphQLDebugMessageShouldBeEqualTo(string $expectedDebugMessage): void
{
$jsonNode = 'errors[0].extensions.debugMessage';
// graphql-php < 15
if (\defined(Error::class.'::CATEGORY_INTERNAL')) {
$jsonNode = 'errors[0].debugMessage';
}
$this->jsonContext->theJsonNodeShouldBeEqualTo($jsonNode, $expectedDebugMessage);
}
private function sendGraphqlRequest(): void
{
$this->restContext->iSendARequestTo('GET', '/graphql?'.http_build_query($this->graphqlRequest));
}
}