|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Service; |
| 9 | + |
| 10 | +use Magento\Framework\App\Request\Http; |
| 11 | +use Magento\Framework\Serialize\SerializerInterface; |
| 12 | +use Magento\GraphQl\Controller\GraphQl; |
| 13 | +use Magento\Framework\App\Response\Http as HttpResponse; |
| 14 | +use Magento\TestFramework\ObjectManager; |
| 15 | +use Magento\Framework\Webapi\Request; |
| 16 | + |
| 17 | +/** |
| 18 | + * Service class to simplify GraphQl requests for integration tests |
| 19 | + */ |
| 20 | +class GraphQlRequest |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private $controllerPath = '/graphql'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Http |
| 29 | + */ |
| 30 | + private $httpRequest; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + private $defaultHeaders = ['Content-Type' => 'application/json']; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var SerializerInterface |
| 39 | + */ |
| 40 | + private $json; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var GraphQl |
| 44 | + */ |
| 45 | + private $controller; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param Http $httpRequest |
| 49 | + * @param SerializerInterface $json |
| 50 | + * @param GraphQl $controller |
| 51 | + */ |
| 52 | + public function __construct( |
| 53 | + Http $httpRequest, |
| 54 | + SerializerInterface $json, |
| 55 | + GraphQl $controller |
| 56 | + ) { |
| 57 | + $this->httpRequest = $httpRequest; |
| 58 | + $this->json = $json; |
| 59 | + $this->controller = $controller; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Send request and return response |
| 64 | + * |
| 65 | + * @param string $query |
| 66 | + * @param array $variables |
| 67 | + * @param string $operation |
| 68 | + * @param array $headers |
| 69 | + * @return HttpResponse |
| 70 | + */ |
| 71 | + public function send( |
| 72 | + string $query, |
| 73 | + array $variables = [], |
| 74 | + string $operation = '', |
| 75 | + array $headers = [] |
| 76 | + ) { |
| 77 | + $this->httpRequest->setPathInfo($this->controllerPath); |
| 78 | + $this->setQuery($query, $variables, $operation) |
| 79 | + ->setHeaders($headers); |
| 80 | + |
| 81 | + /** @var HttpResponse $response */ |
| 82 | + $response = $this->controller->dispatch($this->httpRequest); |
| 83 | + return $response; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Set query data on request |
| 88 | + * |
| 89 | + * @param string $query |
| 90 | + * @param array $variables |
| 91 | + * @param string $operation |
| 92 | + * @return GraphQlRequest |
| 93 | + */ |
| 94 | + private function setQuery(string $query, array $variables = [], string $operation = ''): self |
| 95 | + { |
| 96 | + if (strpos(trim($query), 'mutation') === 0) { |
| 97 | + $this->httpRequest->setMethod('POST'); |
| 98 | + $this->setPostContent($query, $variables, $operation); |
| 99 | + } else { |
| 100 | + $this->httpRequest->setMethod('GET'); |
| 101 | + $this->setGetContent($query, $variables, $operation); |
| 102 | + } |
| 103 | + |
| 104 | + return $this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Set headers on request |
| 109 | + * |
| 110 | + * @param array $headers |
| 111 | + * @return GraphQlRequest |
| 112 | + */ |
| 113 | + private function setHeaders(array $headers): self |
| 114 | + { |
| 115 | + $allHeaders = array_merge($this->defaultHeaders, $headers); |
| 116 | + |
| 117 | + $webApiRequest = ObjectManager::getInstance()->get(Request::class); |
| 118 | + $requestHeaders = $webApiRequest->getHeaders(); |
| 119 | + foreach ($allHeaders as $key => $value) { |
| 120 | + $requestHeaders->addHeaderLine($key, $value); |
| 121 | + } |
| 122 | + |
| 123 | + $this->httpRequest->setHeaders($webApiRequest->getHeaders()); |
| 124 | + |
| 125 | + return $this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Set POST body for request |
| 130 | + * |
| 131 | + * @param string $query |
| 132 | + * @param array $variables |
| 133 | + * @param string $operation |
| 134 | + * @return GraphQlRequest |
| 135 | + */ |
| 136 | + private function setPostContent(string $query, array $variables = [], string $operation = ''): self |
| 137 | + { |
| 138 | + $content = [ |
| 139 | + 'query' => $query, |
| 140 | + 'variables' => !empty($variables) ? $this->json->serialize($variables) : null, |
| 141 | + 'operationName' => !empty($operation) ? $operation : null |
| 142 | + ]; |
| 143 | + $this->httpRequest->setContent($this->json->serialize($content)); |
| 144 | + |
| 145 | + return $this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Set GET parameters for request |
| 150 | + * |
| 151 | + * @param string $query |
| 152 | + * @param array $variables |
| 153 | + * @param string $operation |
| 154 | + * @return GraphQlRequest |
| 155 | + */ |
| 156 | + private function setGetContent(string $query, array $variables = [], string $operation = ''): self |
| 157 | + { |
| 158 | + $this->httpRequest->setQueryValue('query', $query); |
| 159 | + |
| 160 | + if (!empty($variables)) { |
| 161 | + $this->httpRequest->setQueryValue('variables', $variables); |
| 162 | + } |
| 163 | + if (!empty($operation)) { |
| 164 | + $this->httpRequest->setQueryValue('operationName', $operation); |
| 165 | + } |
| 166 | + |
| 167 | + return $this; |
| 168 | + } |
| 169 | +} |
0 commit comments