getBootstrap() ->getApplication() ->getDbInstance(); if (!$db->isDbDumpExists()) { throw new \LogicException('DB dump does not exist.'); } $db->restoreFromDbDump(); parent::setUpBeforeClass(); } protected function setUp() : void { $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->graphql = $this->objectManager->get(\Magento\GraphQl\Controller\GraphQl::class); $this->jsonSerializer = $this->objectManager->get(SerializerInterface::class); $this->metadataPool = $this->objectManager->get(MetadataPool::class); } /** * Test if a graphql schema is generated and request is dispatched and response generated * * @return void */ public function testDispatch() : void { /** @var ProductRepositoryInterface $productRepository */ $productRepository = $this->objectManager->get(ProductRepositoryInterface::class); /** @var ProductInterface $product */ $product = $productRepository->get('simple1'); $query = << $query, 'variables' => null, 'operationName' => null ]; /** @var Http $request */ $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class); $request->setPathInfo('/graphql'); $request->setContent(json_encode($postData)); $headers = $this->objectManager->create(\Zend\Http\Headers::class) ->addHeaders(['Content-Type' => 'application/json']); $request->setHeaders($headers); $response = $this->graphql->dispatch($request); $output = $this->jsonSerializer->unserialize($response->getContent()); $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); $this->assertArrayNotHasKey('errors', $output, 'Response has errors'); $this->assertTrue(!empty($output['data']['products']['items']), 'Products array has items'); $this->assertTrue(!empty($output['data']['products']['items'][0]), 'Products array has items'); $this->assertEquals($output['data']['products']['items'][0]['id'], $product->getData($linkField)); $this->assertEquals($output['data']['products']['items'][0]['sku'], $product->getSku()); $this->assertEquals($output['data']['products']['items'][0]['name'], $product->getName()); } /** * Test the errors on graphql output * * @return void */ public function testError() : void { $query = << $query, 'variables' => null, 'operationName' => null ]; /** @var Http $request */ $request = $this->objectManager->get(\Magento\Framework\App\Request\Http::class); $request->setPathInfo('/graphql'); $request->setContent(json_encode($postData)); $headers = $this->objectManager->create(\Zend\Http\Headers::class) ->addHeaders(['Content-Type' => 'application/json']); $request->setHeaders($headers); $response = $this->graphql->dispatch($request); $outputResponse = $this->jsonSerializer->unserialize($response->getContent()); if (isset($outputResponse['errors'][0])) { if (is_array($outputResponse['errors'][0])) { foreach ($outputResponse['errors'] as $error) { $this->assertEquals( $error['category'], \Magento\Framework\GraphQl\Exception\GraphQlInputException::EXCEPTION_CATEGORY ); if (isset($error['message'])) { $this->assertEquals($error['message'], 'Invalid entity_type specified: invalid'); } if (isset($error['trace'])) { if (is_array($error['trace'])) { $this->assertNotEmpty($error['trace']); } } } } } } /** * teardown */ public function tearDown() { parent::tearDown(); } }