GraphQlAbstract.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\TestCase;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * Test case for Web API functional tests for Graphql.
  10. *
  11. * @SuppressWarnings(PHPMD.NumberOfChildren)
  12. */
  13. abstract class GraphQlAbstract extends WebapiAbstract
  14. {
  15. /**
  16. * The instantiated GraphQL client.
  17. *
  18. * @var \Magento\TestFramework\TestCase\GraphQl\Client
  19. */
  20. private $graphQlClient;
  21. /**
  22. * @var \Magento\Framework\App\Cache
  23. */
  24. private $appCache;
  25. /**
  26. * Perform GraphQL call to the system under test.
  27. *
  28. * @see \Magento\TestFramework\TestCase\GraphQl\Client::call()
  29. * @param string $query
  30. * @param array $variables
  31. * @param string $operationName
  32. * @param array $headers
  33. * @return array|int|string|float|bool GraphQL call results
  34. * @throws \Exception
  35. */
  36. public function graphQlQuery(
  37. string $query,
  38. array $variables = [],
  39. string $operationName = '',
  40. array $headers = []
  41. ) {
  42. return $this->getGraphQlClient()->postQuery(
  43. $query,
  44. $variables,
  45. $operationName,
  46. $this->composeHeaders($headers)
  47. );
  48. }
  49. /**
  50. * Compose headers
  51. *
  52. * @param array $headers
  53. * @return string[]
  54. */
  55. private function composeHeaders(array $headers): array
  56. {
  57. $headersArray = [];
  58. foreach ($headers as $key => $value) {
  59. $headersArray[] = sprintf('%s: %s', $key, $value);
  60. }
  61. return $headersArray;
  62. }
  63. /**
  64. * Clear cache so integration test can alter cached GraphQL schema
  65. *
  66. * @return bool
  67. */
  68. protected function cleanCache()
  69. {
  70. return $this->getAppCache()->clean(\Magento\Framework\App\Config::CACHE_TAG);
  71. }
  72. /**
  73. * Return app cache setup.
  74. *
  75. * @return \Magento\Framework\App\Cache
  76. */
  77. private function getAppCache()
  78. {
  79. if (null === $this->appCache) {
  80. $this->appCache = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Cache::class);
  81. }
  82. return $this->appCache;
  83. }
  84. /**
  85. * Get GraphQL adapter (create if requested one does not exist).
  86. *
  87. * @return \Magento\TestFramework\TestCase\GraphQl\Client
  88. */
  89. private function getGraphQlClient()
  90. {
  91. if ($this->graphQlClient === null) {
  92. $this->graphQlClient = Bootstrap::getObjectManager()->get(
  93. \Magento\TestFramework\TestCase\GraphQl\Client::class
  94. );
  95. }
  96. return $this->graphQlClient;
  97. }
  98. /**
  99. * Compare actual response fields with expected
  100. *
  101. * @param array $actualResponse
  102. * @param array $assertionMap ['response_field_name' => 'response_field_value', ...]
  103. * OR [['response_field' => $field, 'expected_value' => $value], ...]
  104. */
  105. protected function assertResponseFields($actualResponse, $assertionMap)
  106. {
  107. foreach ($assertionMap as $key => $assertionData) {
  108. $expectedValue = isset($assertionData['expected_value'])
  109. ? $assertionData['expected_value']
  110. : $assertionData;
  111. $responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
  112. self::assertNotNull(
  113. $expectedValue,
  114. "Value of '{$responseField}' field must not be NULL"
  115. );
  116. self::assertEquals(
  117. $expectedValue,
  118. $actualResponse[$responseField],
  119. "Value of '{$responseField}' field in response does not match expected value: "
  120. . var_export($expectedValue, true)
  121. );
  122. }
  123. }
  124. }