GraphQlReaderTest.php 6.8 KB

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