Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GraphQlControllerTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
26 {
27  const CONTENT_TYPE = 'application/json';
28 
30  private $objectManager;
31 
33  private $graphql;
34 
36  private $jsonSerializer;
37 
39  private $metadataPool;
40 
41  public static function setUpBeforeClass()
42  {
43  $db = Bootstrap::getInstance()->getBootstrap()
44  ->getApplication()
45  ->getDbInstance();
46  if (!$db->isDbDumpExists()) {
47  throw new \LogicException('DB dump does not exist.');
48  }
49  $db->restoreFromDbDump();
50 
51  parent::setUpBeforeClass();
52  }
53 
54  protected function setUp() : void
55  {
57  $this->graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class);
58  $this->jsonSerializer = $this->objectManager->get(SerializerInterface::class);
59  $this->metadataPool = $this->objectManager->get(MetadataPool::class);
60  }
61 
67  public function testDispatch() : void
68  {
70  $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
71 
73  $product = $productRepository->get('simple1');
74 
75  $query
76  = <<<QUERY
77  {
78  products(filter: {sku: {eq: "simple1"}})
79  {
80  items {
81  id
82  name
83  sku
84  }
85  }
86  }
87 QUERY;
88  $postData = [
89  'query' => $query,
90  'variables' => null,
91  'operationName' => null
92  ];
94  $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class);
95  $request->setPathInfo('/graphql');
96  $request->setContent(json_encode($postData));
97  $headers = $this->objectManager->create(\Zend\Http\Headers::class)
98  ->addHeaders(['Content-Type' => 'application/json']);
99  $request->setHeaders($headers);
100  $response = $this->graphql->dispatch($request);
101  $output = $this->jsonSerializer->unserialize($response->getContent());
102  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
103 
104  $this->assertArrayNotHasKey('errors', $output, 'Response has errors');
105  $this->assertTrue(!empty($output['data']['products']['items']), 'Products array has items');
106  $this->assertTrue(!empty($output['data']['products']['items'][0]), 'Products array has items');
107  $this->assertEquals($output['data']['products']['items'][0]['id'], $product->getData($linkField));
108  $this->assertEquals($output['data']['products']['items'][0]['sku'], $product->getSku());
109  $this->assertEquals($output['data']['products']['items'][0]['name'], $product->getName());
110  }
111 
117  public function testError() : void
118  {
119  $this->markTestSkipped('Causes failiure with php unit and php 7.2');
120  $query
121  = <<<QUERY
122  {
123  customAttributeMetadata(attributes:[
124  {
125  attribute_code:"sku"
126  entity_type:"invalid"
127  }
128  ])
129  {
130  items{
131  attribute_code
132  attribute_type
133  entity_type
134  }
135  }
136  }
137 QUERY;
138 
139  $postData = [
140  'query' => $query,
141  'variables' => null,
142  'operationName' => null
143  ];
145  $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class);
146  $request->setPathInfo('/graphql');
147  $request->setContent(json_encode($postData));
148  $headers = $this->objectManager->create(\Zend\Http\Headers::class)
149  ->addHeaders(['Content-Type' => 'application/json']);
150  $request->setHeaders($headers);
151  $response = $this->graphql->dispatch($request);
152  $outputResponse = $this->jsonSerializer->unserialize($response->getContent());
153  if (isset($outputResponse['errors'][0])) {
154  if (is_array($outputResponse['errors'][0])) {
155  foreach ($outputResponse['errors'] as $error) {
156  $this->assertEquals(
157  $error['category'],
158  \Magento\Framework\GraphQl\Exception\GraphQlInputException::EXCEPTION_CATEGORY
159  );
160  if (isset($error['message'])) {
161  $this->assertEquals($error['message'], 'Invalid entity_type specified: invalid');
162  }
163  if (isset($error['trace'])) {
164  if (is_array($error['trace'])) {
165  $this->assertNotEmpty($error['trace']);
166  }
167  }
168  }
169  }
170  }
171  }
172 
176  public function tearDown()
177  {
178  parent::tearDown();
179  }
180 }
$response
Definition: 404.php:11