|
| 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 | +namespace Magento\Framework\GraphQl\Config; |
| 9 | + |
| 10 | +use Magento\Framework\App\Cache; |
| 11 | +use Magento\Framework\App\Request\Http; |
| 12 | +use Magento\Framework\GraphQl\Config; |
| 13 | +use Magento\Framework\GraphQl\Schema\SchemaGenerator; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\Framework\Serialize\SerializerInterface; |
| 16 | +use Magento\GraphQl\Controller\GraphQl; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests the entire process of generating a schema from a given SDL and processing a request/query |
| 20 | + * |
| 21 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 22 | + * @magentoAppArea graphql |
| 23 | + */ |
| 24 | +class GraphQlDeprecatedAnnotationReaderTest extends \PHPUnit\Framework\TestCase |
| 25 | +{ |
| 26 | + /** @var Config */ |
| 27 | + private $configModel; |
| 28 | + |
| 29 | + /** @var GraphQl */ |
| 30 | + private $graphQlController; |
| 31 | + |
| 32 | + /** @var ObjectManagerInterface */ |
| 33 | + private $objectManager; |
| 34 | + |
| 35 | + /** @var SerializerInterface */ |
| 36 | + private $jsonSerializer; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp() |
| 42 | + { |
| 43 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 44 | + /** @var Cache $cache */ |
| 45 | + $cache = $this->objectManager->get(Cache::class); |
| 46 | + $cache->clean(); |
| 47 | + $fileResolverMock = $this->getMockBuilder( |
| 48 | + \Magento\Framework\Config\FileResolverInterface::class |
| 49 | + )->disableOriginalConstructor()->getMock(); |
| 50 | + $fileList = [ |
| 51 | + file_get_contents(__DIR__ . '/../_files/schemaE.graphqls') |
| 52 | + ]; |
| 53 | + $fileResolverMock->expects($this->any())->method('get')->will($this->returnValue($fileList)); |
| 54 | + $graphQlReader = $this->objectManager->create( |
| 55 | + \Magento\Framework\GraphQlSchemaStitching\GraphQlReader::class, |
| 56 | + ['fileResolver' => $fileResolverMock] |
| 57 | + ); |
| 58 | + $reader = $this->objectManager->create( |
| 59 | + \Magento\Framework\GraphQlSchemaStitching\Reader::class, |
| 60 | + ['readers' => ['graphql_reader' => $graphQlReader]] |
| 61 | + ); |
| 62 | + $data = $this->objectManager->create( |
| 63 | + \Magento\Framework\GraphQl\Config\Data ::class, |
| 64 | + ['reader' => $reader] |
| 65 | + ); |
| 66 | + $this->configModel = $this->objectManager->create( |
| 67 | + \Magento\Framework\GraphQl\Config::class, |
| 68 | + ['data' => $data] |
| 69 | + ); |
| 70 | + $outputMapper = $this->objectManager->create( |
| 71 | + \Magento\Framework\GraphQl\Schema\Type\Output\OutputMapper::class, |
| 72 | + ['config' => $this->configModel] |
| 73 | + ); |
| 74 | + $schemaGenerator = $this->objectManager->create( |
| 75 | + SchemaGenerator::class, |
| 76 | + ['outputMapper' => $outputMapper] |
| 77 | + ); |
| 78 | + $this->graphQlController = $this->objectManager->create( |
| 79 | + GraphQl::class, |
| 80 | + ['schemaGenerator' => $schemaGenerator] |
| 81 | + ); |
| 82 | + $this->jsonSerializer = $this->objectManager->get(SerializerInterface::class); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 87 | + */ |
| 88 | + public function testDispatchIntrospectionWithCustomSDL() |
| 89 | + { |
| 90 | + $query |
| 91 | + = <<<QUERY |
| 92 | + query IntrospectionQuery { |
| 93 | + __schema { |
| 94 | + queryType { name } |
| 95 | + types { |
| 96 | + ...FullType |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +fragment FullType on __Type { |
| 102 | + kind |
| 103 | + name |
| 104 | + description |
| 105 | + fields(includeDeprecated: true) { |
| 106 | + name |
| 107 | + description |
| 108 | + args { |
| 109 | + ...InputValue |
| 110 | + } |
| 111 | + type { |
| 112 | + ...TypeRef |
| 113 | + } |
| 114 | + isDeprecated |
| 115 | + deprecationReason |
| 116 | + } |
| 117 | + inputFields { |
| 118 | + ...InputValue |
| 119 | + } |
| 120 | + interfaces { |
| 121 | + ...TypeRef |
| 122 | + } |
| 123 | + enumValues(includeDeprecated: true) { |
| 124 | + name |
| 125 | + description |
| 126 | + isDeprecated |
| 127 | + deprecationReason |
| 128 | + } |
| 129 | + possibleTypes { |
| 130 | + ...TypeRef |
| 131 | + } |
| 132 | +} |
| 133 | +
|
| 134 | +fragment InputValue on __InputValue { |
| 135 | + name |
| 136 | + description |
| 137 | + type { ...TypeRef } |
| 138 | + defaultValue |
| 139 | +} |
| 140 | +
|
| 141 | +fragment TypeRef on __Type { |
| 142 | + kind |
| 143 | + name |
| 144 | + ofType { |
| 145 | + kind |
| 146 | + name |
| 147 | + ofType { |
| 148 | + kind |
| 149 | + name |
| 150 | + ofType { |
| 151 | + kind |
| 152 | + name |
| 153 | + ofType { |
| 154 | + kind |
| 155 | + name |
| 156 | + ofType { |
| 157 | + kind |
| 158 | + name |
| 159 | + ofType { |
| 160 | + kind |
| 161 | + name |
| 162 | + ofType { |
| 163 | + kind |
| 164 | + name |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | +
|
| 174 | +
|
| 175 | +QUERY; |
| 176 | + $postData = [ |
| 177 | + 'query' => $query, |
| 178 | + 'variables' => null, |
| 179 | + 'operationName' => 'IntrospectionQuery' |
| 180 | + ]; |
| 181 | + /** @var Http $request */ |
| 182 | + $request = $this->objectManager->get(Http::class); |
| 183 | + $request->setPathInfo('/graphql'); |
| 184 | + $request->setMethod('POST'); |
| 185 | + $request->setContent(json_encode($postData)); |
| 186 | + $headers = $this->objectManager->create(\Zend\Http\Headers::class) |
| 187 | + ->addHeaders(['Content-Type' => 'application/json']); |
| 188 | + $request->setHeaders($headers); |
| 189 | + |
| 190 | + $response = $this->graphQlController->dispatch($request); |
| 191 | + $this->jsonSerializer->unserialize($response->getContent()); |
| 192 | + $expectedOutput = require __DIR__ . '/../_files/schema_response_sdl_deprecated_annotation.php'; |
| 193 | + |
| 194 | + //Checks to make sure that the given description exists in the expectedOutput array |
| 195 | + |
| 196 | + $this->assertTrue( |
| 197 | + array_key_exists( |
| 198 | + array_search( |
| 199 | + 'Comment for SortEnum', |
| 200 | + array_column($expectedOutput, 'description') |
| 201 | + ), |
| 202 | + $expectedOutput |
| 203 | + ) |
| 204 | + ); |
| 205 | + |
| 206 | + //Checks to make sure that the given deprecatedReason exists in the expectedOutput array for enumValues, fields. |
| 207 | + $fieldsArray = $expectedOutput[0]['fields']; |
| 208 | + $enumValuesArray = $expectedOutput[1]['enumValues']; |
| 209 | + |
| 210 | + foreach($fieldsArray as $field){ |
| 211 | + if ( $field['isDeprecated'] === true){ |
| 212 | + $typeDeprecatedReason = $field['deprecationReason']; |
| 213 | + } |
| 214 | + } |
| 215 | + $this->assertNotEmpty($typeDeprecatedReason); |
| 216 | + $this->assertEquals('Deprecated url_path test', $typeDeprecatedReason); |
| 217 | + |
| 218 | + foreach($enumValuesArray as $enumValue){ |
| 219 | + if ( $enumValue['isDeprecated'] === true){ |
| 220 | + $enumValueDeprecatedReason = $enumValue['deprecationReason']; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + $this->assertNotEmpty($enumValueDeprecatedReason); |
| 225 | + $this->assertEquals('Deprecated SortEnum Value test',$enumValueDeprecatedReason); |
| 226 | + |
| 227 | + } |
| 228 | +} |
0 commit comments