GraphQlControllerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\GraphQl\Controller;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Framework\App\Request\Http;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. use Magento\Framework\Serialize\SerializerInterface;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. /**
  15. * Tests the dispatch method in the GraphQl Controller class using a simple product query
  16. *
  17. * @magentoAppArea graphql
  18. * @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
  19. * @magentoDbIsolation disabled
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class GraphQlControllerTest extends \Magento\TestFramework\Indexer\TestCase
  23. {
  24. const CONTENT_TYPE = 'application/json';
  25. /** @var \Magento\Framework\ObjectManagerInterface */
  26. private $objectManager;
  27. /** @var GraphQl */
  28. private $graphql;
  29. /** @var SerializerInterface */
  30. private $jsonSerializer;
  31. /** @var MetadataPool */
  32. private $metadataPool;
  33. public static function setUpBeforeClass()
  34. {
  35. $db = Bootstrap::getInstance()->getBootstrap()
  36. ->getApplication()
  37. ->getDbInstance();
  38. if (!$db->isDbDumpExists()) {
  39. throw new \LogicException('DB dump does not exist.');
  40. }
  41. $db->restoreFromDbDump();
  42. parent::setUpBeforeClass();
  43. }
  44. protected function setUp() : void
  45. {
  46. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  47. $this->graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class);
  48. $this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
  49. $this->metadataPool = $this->objectManager->get(MetadataPool::class);
  50. }
  51. /**
  52. * Test if a graphql schema is generated and request is dispatched and response generated
  53. *
  54. * @return void
  55. */
  56. public function testDispatch() : void
  57. {
  58. /** @var ProductRepositoryInterface $productRepository */
  59. $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  60. /** @var ProductInterface $product */
  61. $product = $productRepository->get('simple1');
  62. $query
  63. = <<<QUERY
  64. {
  65. products(filter: {sku: {eq: "simple1"}})
  66. {
  67. items {
  68. id
  69. name
  70. sku
  71. }
  72. }
  73. }
  74. QUERY;
  75. $postData = [
  76. 'query' => $query,
  77. 'variables' => null,
  78. 'operationName' => null
  79. ];
  80. /** @var Http $request */
  81. $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class);
  82. $request->setPathInfo('/graphql');
  83. $request->setContent(json_encode($postData));
  84. $headers = $this->objectManager->create(\Zend\Http\Headers::class)
  85. ->addHeaders(['Content-Type' => 'application/json']);
  86. $request->setHeaders($headers);
  87. $response = $this->graphql->dispatch($request);
  88. $output = $this->jsonSerializer->unserialize($response->getContent());
  89. $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
  90. $this->assertArrayNotHasKey('errors', $output, 'Response has errors');
  91. $this->assertTrue(!empty($output['data']['products']['items']), 'Products array has items');
  92. $this->assertTrue(!empty($output['data']['products']['items'][0]), 'Products array has items');
  93. $this->assertEquals($output['data']['products']['items'][0]['id'], $product->getData($linkField));
  94. $this->assertEquals($output['data']['products']['items'][0]['sku'], $product->getSku());
  95. $this->assertEquals($output['data']['products']['items'][0]['name'], $product->getName());
  96. }
  97. /**
  98. * Test the errors on graphql output
  99. *
  100. * @return void
  101. */
  102. public function testError() : void
  103. {
  104. $query
  105. = <<<QUERY
  106. {
  107. customAttributeMetadata(attributes:[
  108. {
  109. attribute_code:"sku"
  110. entity_type:"invalid"
  111. }
  112. ])
  113. {
  114. items{
  115. attribute_code
  116. attribute_type
  117. entity_type
  118. }
  119. }
  120. }
  121. QUERY;
  122. $postData = [
  123. 'query' => $query,
  124. 'variables' => null,
  125. 'operationName' => null
  126. ];
  127. /** @var Http $request */
  128. $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class);
  129. $request->setPathInfo('/graphql');
  130. $request->setContent(json_encode($postData));
  131. $headers = $this->objectManager->create(\Zend\Http\Headers::class)
  132. ->addHeaders(['Content-Type' => 'application/json']);
  133. $request->setHeaders($headers);
  134. $response = $this->graphql->dispatch($request);
  135. $outputResponse = $this->jsonSerializer->unserialize($response->getContent());
  136. if (isset($outputResponse['errors'][0])) {
  137. if (is_array($outputResponse['errors'][0])) {
  138. foreach ($outputResponse['errors'] as $error) {
  139. $this->assertEquals(
  140. $error['category'],
  141. \Magento\Framework\GraphQl\Exception\GraphQlInputException::EXCEPTION_CATEGORY
  142. );
  143. if (isset($error['message'])) {
  144. $this->assertEquals($error['message'], 'Invalid entity_type specified: invalid');
  145. }
  146. if (isset($error['trace'])) {
  147. if (is_array($error['trace'])) {
  148. $this->assertNotEmpty($error['trace']);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * teardown
  157. */
  158. public function tearDown()
  159. {
  160. parent::tearDown();
  161. }
  162. }