Skip to content

Commit ddf16cd

Browse files
committed
MC-18945: Reading deprecated annotation in schema
- Fixed the review comments on PR
1 parent 185d07a commit ddf16cd

File tree

7 files changed

+248
-1044
lines changed

7 files changed

+248
-1044
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/IntrospectionQueryTest.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,139 @@ public function testIntrospectionQuery()
5656

5757
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
5858
}
59+
60+
/**
61+
* Tests that Introspection Query with deprecated annotations on enum values, fields are read.
62+
*/
63+
public function testIntrospectionQueryWithDeprecatedAnnotationOnEnumAndFieldValues()
64+
{
65+
$query
66+
= <<<QUERY
67+
query IntrospectionQuery {
68+
__schema {
69+
queryType { name }
70+
mutationType { name }
71+
types {
72+
...FullType
73+
}
74+
}
75+
}
76+
fragment FullType on __Type {
77+
kind
78+
name
79+
description
80+
fields(includeDeprecated: true) {
81+
name
82+
description
83+
isDeprecated
84+
deprecationReason
85+
}
86+
enumValues(includeDeprecated: true) {
87+
name
88+
description
89+
isDeprecated
90+
deprecationReason
91+
}
92+
}
93+
94+
QUERY;
95+
96+
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
97+
$response = $this->graphQlQuery($query);
98+
$schemaResponseFields = $response['__schema']['types'];
99+
$enumValueReasonArray = $this->getEnumValueDeprecatedReason($schemaResponseFields);
100+
$fieldsValueReasonArray = $this->getFieldsValueDeprecatedReason($schemaResponseFields);
101+
$expectedOutput = require __DIR__ . '/_files/schema_response_sdl_deprecated_annotation.php';
102+
103+
// checking field values deprecated reason
104+
if (!empty($fieldsValueReasonArray)) {
105+
$fieldDeprecatedReason = [];
106+
$fieldsArray = $expectedOutput[0]['fields'];
107+
foreach ($fieldsArray as $field) {
108+
if ($field['isDeprecated'] === true) {
109+
$fieldDeprecatedReason [] = $field['deprecationReason'];
110+
}
111+
}
112+
$this->assertNotEmpty($fieldDeprecatedReason);
113+
$this->assertContains(
114+
'Symbol was missed. Use `default_display_currency_code`.',
115+
$fieldDeprecatedReason
116+
);
117+
118+
$this->assertContains(
119+
'Symbol was missed. Use `default_display_currency_code`.',
120+
$fieldsValueReasonArray
121+
);
122+
123+
$this->assertNotEmpty(
124+
array_intersect($fieldDeprecatedReason, $fieldsValueReasonArray)
125+
);
126+
127+
}
128+
129+
// checking enum values deprecated reason
130+
if (!empty($enumValueReasonArray)) {
131+
$enumValueDeprecatedReason = [];
132+
$enumValuesArray = $expectedOutput[1]['enumValues'];
133+
foreach ($enumValuesArray as $enumValue) {
134+
if ($enumValue['isDeprecated'] === true) {
135+
$enumValueDeprecatedReason [] = $enumValue['deprecationReason'];
136+
}
137+
}
138+
$this->assertNotEmpty($enumValueDeprecatedReason);
139+
$this->assertContains(
140+
'`sample_url` serves to get the downloadable sample',
141+
$enumValueDeprecatedReason
142+
);
143+
$this->assertContains(
144+
'`sample_url` serves to get the downloadable sample',
145+
$enumValueReasonArray
146+
);
147+
$this->assertNotEmpty(
148+
array_intersect($enumValueDeprecatedReason, $enumValueReasonArray)
149+
);
150+
}
151+
}
152+
153+
/**
154+
* Get the enum values deprecated reasons from the schema
155+
*
156+
* @param array $schemaResponseFields
157+
* @return array
158+
*/
159+
private function getEnumValueDeprecatedReason($schemaResponseFields): array
160+
{
161+
$enumValueReasonArray = [];
162+
foreach ($schemaResponseFields as $schemaResponseField) {
163+
if (!empty($schemaResponseField['enumValues'])) {
164+
foreach ($schemaResponseField['enumValues'] as $enumValueDeprecationReasonArray) {
165+
if (!empty($enumValueDeprecationReasonArray['deprecationReason'])) {
166+
$enumValueReasonArray[] = $enumValueDeprecationReasonArray['deprecationReason'];
167+
}
168+
}
169+
}
170+
}
171+
return $enumValueReasonArray;
172+
}
173+
174+
/**
175+
* Get the fields values deprecated reasons from the schema
176+
*
177+
* @param array $schemaResponseFields
178+
* @return array
179+
*/
180+
private function getFieldsValueDeprecatedReason($schemaResponseFields): array
181+
{
182+
$fieldsValueReasonArray = [];
183+
foreach ($schemaResponseFields as $schemaResponseField) {
184+
if (!empty($schemaResponseField['fields'])) {
185+
foreach ($schemaResponseField['fields'] as $fieldsValueDeprecatedReasonArray) {
186+
if (!empty($fieldsValueDeprecatedReasonArray['deprecationReason'])) {
187+
$fieldsValueReasonArray[] = $fieldsValueDeprecatedReasonArray['deprecationReason'];
188+
}
189+
}
190+
}
191+
}
192+
return $fieldsValueReasonArray;
193+
}
59194
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
return [
9+
[
10+
'kind'=> 'OBJECT',
11+
'name'=> 'Currency',
12+
'description'=> '',
13+
'fields'=> [
14+
[
15+
'name'=> 'available_currency_codes',
16+
'description'=> null,
17+
'isDeprecated'=> false,
18+
'deprecationReason'=> null
19+
],
20+
[
21+
'name'=> 'base_currency_code',
22+
'description'=> null,
23+
'isDeprecated'=> false,
24+
'deprecationReason'=> null
25+
],
26+
27+
[
28+
'name'=> 'base_currency_symbol',
29+
'description'=> null,
30+
'isDeprecated'=> false,
31+
'deprecationReason'=> null
32+
],
33+
[
34+
'name'=> 'default_display_currecy_code',
35+
'description'=> null,
36+
'isDeprecated'=> true,
37+
'deprecationReason'=> 'Symbol was missed. Use `default_display_currency_code`.'
38+
],
39+
[
40+
'name'=> 'default_display_currecy_symbol',
41+
'description'=> null,
42+
'isDeprecated'=> true,
43+
'deprecationReason'=> 'Symbol was missed. Use `default_display_currency_code`.'
44+
],
45+
[
46+
'name'=> 'default_display_currency_code',
47+
'description'=> null,
48+
'isDeprecated'=> false,
49+
'deprecationReason'=> null
50+
],
51+
[
52+
'name'=> 'default_display_currency_symbol',
53+
'description'=> null,
54+
'isDeprecated'=> false,
55+
'deprecationReason'=> null
56+
],
57+
[
58+
'name'=> 'exchange_rates',
59+
'description'=> null,
60+
'isDeprecated'=> false,
61+
'deprecationReason'=> null
62+
],
63+
64+
],
65+
'enumValues'=> null
66+
],
67+
[
68+
'kind' => 'ENUM',
69+
'name' => 'DownloadableFileTypeEnum',
70+
'description' => '',
71+
'fields' => null,
72+
'enumValues' => [
73+
[
74+
'name' => 'FILE',
75+
'description' => '',
76+
'isDeprecated' => true,
77+
'deprecationReason' => 'sample_url` serves to get the downloadable sample'
78+
],
79+
[
80+
'name' => 'URL',
81+
'description' => '',
82+
'isDeprecated' => true,
83+
'deprecationReason' => '`sample_url` serves to get the downloadable sample'
84+
]
85+
],
86+
'possibleTypes' => null
87+
],
88+
];

dev/tests/integration/testsuite/Magento/Framework/GraphQl/Config/GraphQlReaderTest.php

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -244,158 +244,4 @@ function ($a, $b) {
244244
)
245245
);
246246
}
247-
248-
/**
249-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
250-
*/
251-
public function testDispatchIntrospectionWithDeprecatedSDL()
252-
{
253-
$query
254-
= <<<QUERY
255-
query IntrospectionQuery {
256-
__schema {
257-
queryType { name }
258-
types {
259-
...FullType
260-
}
261-
}
262-
}
263-
264-
fragment FullType on __Type {
265-
kind
266-
name
267-
description
268-
fields(includeDeprecated: true) {
269-
name
270-
description
271-
args {
272-
...InputValue
273-
}
274-
type {
275-
...TypeRef
276-
}
277-
isDeprecated
278-
deprecationReason
279-
}
280-
inputFields {
281-
...InputValue
282-
}
283-
interfaces {
284-
...TypeRef
285-
}
286-
enumValues(includeDeprecated: true) {
287-
name
288-
description
289-
isDeprecated
290-
deprecationReason
291-
}
292-
possibleTypes {
293-
...TypeRef
294-
}
295-
}
296-
297-
fragment InputValue on __InputValue {
298-
name
299-
description
300-
type { ...TypeRef }
301-
defaultValue
302-
}
303-
304-
fragment TypeRef on __Type {
305-
kind
306-
name
307-
ofType {
308-
kind
309-
name
310-
ofType {
311-
kind
312-
name
313-
ofType {
314-
kind
315-
name
316-
ofType {
317-
kind
318-
name
319-
ofType {
320-
kind
321-
name
322-
ofType {
323-
kind
324-
name
325-
ofType {
326-
kind
327-
name
328-
}
329-
}
330-
}
331-
}
332-
}
333-
}
334-
}
335-
}
336-
337-
338-
QUERY;
339-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
340-
/** @var Cache $cache */
341-
$cache = $this->objectManager->get(Cache::class);
342-
$cache->clean();
343-
$fileResolverMock = $this->getMockBuilder(
344-
\Magento\Framework\Config\FileResolverInterface::class
345-
)->disableOriginalConstructor()->getMock();
346-
$fileList = [
347-
file_get_contents(__DIR__ . '/../_files/schemaE.graphqls')
348-
];
349-
$fileResolverMock->expects($this->any())->method('get')->will($this->returnValue($fileList));
350-
351-
$postData = [
352-
'query' => $query,
353-
'variables' => null,
354-
'operationName' => 'IntrospectionQuery'
355-
];
356-
/** @var Http $request */
357-
$request = $this->objectManager->get(Http::class);
358-
$request->setPathInfo('/graphql');
359-
$request->setMethod('POST');
360-
$request->setContent(json_encode($postData));
361-
$headers = $this->objectManager->create(\Zend\Http\Headers::class)
362-
->addHeaders(['Content-Type' => 'application/json']);
363-
$request->setHeaders($headers);
364-
365-
$response = $this->graphQlController->dispatch($request);
366-
$this->jsonSerializer->unserialize($response->getContent());
367-
$expectedOutput = require __DIR__ . '/../_files/schema_response_sdl_deprecated_annotation.php';
368-
369-
//Checks to make sure that the given description exists in the expectedOutput array
370-
371-
$this->assertTrue(
372-
array_key_exists(
373-
array_search(
374-
'Comment for SortEnum',
375-
array_column($expectedOutput, 'description')
376-
),
377-
$expectedOutput
378-
)
379-
);
380-
381-
//Checks to make sure that the given deprecatedReason exists in the expectedOutput array for enumValues, fields.
382-
$fieldsArray = $expectedOutput[0]['fields'];
383-
$enumValuesArray = $expectedOutput[1]['enumValues'];
384-
385-
foreach ($fieldsArray as $field) {
386-
if ($field['isDeprecated'] === true) {
387-
$typeDeprecatedReason [] = $field['deprecationReason'];
388-
}
389-
}
390-
$this->assertNotEmpty($typeDeprecatedReason);
391-
$this->assertContains('Deprecated url_path test', $typeDeprecatedReason);
392-
393-
foreach ($enumValuesArray as $enumValue) {
394-
if ($enumValue['isDeprecated'] === true) {
395-
$enumValueDeprecatedReason [] = $enumValue['deprecationReason'];
396-
}
397-
}
398-
$this->assertNotEmpty($enumValueDeprecatedReason);
399-
$this->assertContains('Deprecated SortEnum Value test', $enumValueDeprecatedReason);
400-
}
401247
}

0 commit comments

Comments
 (0)