Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GraphQlAbstract.php
Go to the documentation of this file.
1 <?php
7 
9 
15 abstract class GraphQlAbstract extends WebapiAbstract
16 {
22  private $graphQlClient;
23 
27  private $appCache;
28 
39  public function graphQlQuery(
40  string $query,
41  array $variables = [],
42  string $operationName = '',
43  array $headers = []
44  ) {
45  return $this->getGraphQlClient()->postQuery(
46  $query,
47  $variables,
48  $operationName,
49  $this->composeHeaders($headers)
50  );
51  }
52 
56  private function composeHeaders($headers)
57  {
58  $headersArray =[];
59  foreach ($headers as $key => $value) {
60  $headersArray[] = sprintf('%s: %s', $key, $value);
61  }
62  return $headersArray;
63  }
64 
70  protected function cleanCache()
71  {
72  return $this->getAppCache()->clean(\Magento\Framework\App\Config::CACHE_TAG);
73  }
74 
80  private function getAppCache()
81  {
82  if (null === $this->appCache) {
83  $this->appCache = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Cache::class);
84  }
85  return $this->appCache;
86  }
87 
93  private function getGraphQlClient()
94  {
95  if ($this->graphQlClient === null) {
96  $this->graphQlClient = Bootstrap::getObjectManager()->get(
97  \Magento\TestFramework\TestCase\GraphQl\Client::class
98  );
99  }
100  return $this->graphQlClient;
101  }
102 
110  protected function assertResponseFields($actualResponse, $assertionMap)
111  {
112  foreach ($assertionMap as $key => $assertionData) {
113  $expectedValue = isset($assertionData['expected_value'])
114  ? $assertionData['expected_value']
115  : $assertionData;
116  $responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
117  self::assertNotNull(
118  $expectedValue,
119  "Value of '{$responseField}' field must not be NULL"
120  );
121  self::assertEquals(
122  $expectedValue,
123  $actualResponse[$responseField],
124  "Value of '{$responseField}' field in response does not match expected value: "
125  . var_export($expectedValue, true)
126  );
127  }
128  }
129 }
assertResponseFields($actualResponse, $assertionMap)
$value
Definition: gender.phtml:16
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])