| 1 | <?php |
| 2 | |
| 3 | namespace Civi\Test; |
| 4 | |
| 5 | use GuzzleHttp\Handler\MockHandler; |
| 6 | use GuzzleHttp\HandlerStack; |
| 7 | use GuzzleHttp\Psr7\Response; |
| 8 | use GuzzleHttp\Middleware; |
| 9 | use GuzzleHttp\Client; |
| 10 | |
| 11 | /** |
| 12 | * Class GuzzleTestTrait |
| 13 | * |
| 14 | * This trait defines a number of helper functions for testing Guzzle-based logic, |
| 15 | * such as a payment-processing gateway. |
| 16 | * |
| 17 | * Use this in a headless test for which you need to mock outbound HTTP requests from Civi. |
| 18 | * Alternatively, to write an E2E test with inbound HTTP requests to Civi, see HttpTestTrait. |
| 19 | */ |
| 20 | trait GuzzleTestTrait { |
| 21 | /** |
| 22 | * @var \GuzzleHttp\Client |
| 23 | */ |
| 24 | protected $guzzleClient; |
| 25 | |
| 26 | /** |
| 27 | * Array containing guzzle history of requests and responses. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | protected $container; |
| 32 | |
| 33 | /** |
| 34 | * Mockhandler to simulate guzzle requests. |
| 35 | * |
| 36 | * @var \GuzzleHttp\Handler\MockHandler |
| 37 | */ |
| 38 | protected $mockHandler; |
| 39 | |
| 40 | /** |
| 41 | * The url to mock-interact with. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $baseUri; |
| 46 | |
| 47 | /** |
| 48 | * @return \GuzzleHttp\Client |
| 49 | */ |
| 50 | public function getGuzzleClient() { |
| 51 | return $this->guzzleClient; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param \GuzzleHttp\Client $guzzleClient |
| 56 | */ |
| 57 | public function setGuzzleClient($guzzleClient) { |
| 58 | $this->guzzleClient = $guzzleClient; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return array |
| 63 | */ |
| 64 | public function getContainer() { |
| 65 | return $this->container; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param array $container |
| 70 | */ |
| 71 | public function setContainer($container) { |
| 72 | $this->container = $container; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return mixed |
| 77 | */ |
| 78 | public function getBaseUri() { |
| 79 | return $this->baseUri; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param mixed $baseUri |
| 84 | */ |
| 85 | public function setBaseUri($baseUri) { |
| 86 | $this->baseUri = $baseUri; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return \GuzzleHttp\Handler\MockHandler |
| 91 | */ |
| 92 | public function getMockHandler() { |
| 93 | return $this->mockHandler; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param \GuzzleHttp\Handler\MockHandler $mockHandler |
| 98 | */ |
| 99 | public function setMockHandler($mockHandler) { |
| 100 | $this->mockHandler = $mockHandler; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param $responses |
| 105 | */ |
| 106 | protected function createMockHandler($responses) { |
| 107 | $mocks = []; |
| 108 | foreach ($responses as $response) { |
| 109 | $mocks[] = new Response(200, [], $response); |
| 110 | } |
| 111 | $this->setMockHandler(new MockHandler($mocks)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param $files |
| 116 | */ |
| 117 | protected function createMockHandlerForFiles($files) { |
| 118 | $body = []; |
| 119 | foreach ($files as $file) { |
| 120 | $body[] = trim(file_get_contents(__DIR__ . $file)); |
| 121 | } |
| 122 | $this->createMockHandler($body); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set up a guzzle client with a history container. |
| 127 | * |
| 128 | * After you have run the requests you can inspect $this->container |
| 129 | * for the outgoing requests and incoming responses. |
| 130 | * |
| 131 | * If $this->mock is defined then no outgoing http calls will be made |
| 132 | * and the responses configured on the handler will be returned instead |
| 133 | * of replies from a remote provider. |
| 134 | */ |
| 135 | protected function setUpClientWithHistoryContainer() { |
| 136 | $this->container = []; |
| 137 | $history = Middleware::history($this->container); |
| 138 | $handler = HandlerStack::create($this->getMockHandler()); |
| 139 | $handler->push($history); |
| 140 | $this->guzzleClient = new Client(['base_uri' => $this->baseUri, 'handler' => $handler]); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the bodies of the requests sent via Guzzle. |
| 145 | * |
| 146 | * @return array |
| 147 | */ |
| 148 | protected function getRequestBodies() { |
| 149 | $requests = []; |
| 150 | foreach ($this->getContainer() as $guzzle) { |
| 151 | $requests[] = (string) $guzzle['request']->getBody(); |
| 152 | } |
| 153 | return $requests; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the bodies of the requests sent via Guzzle. |
| 158 | * |
| 159 | * @return array |
| 160 | */ |
| 161 | protected function getRequestHeaders() { |
| 162 | $requests = []; |
| 163 | foreach ($this->getContainer() as $guzzle) { |
| 164 | $requests[] = $guzzle['request']->getHeaders(); |
| 165 | } |
| 166 | return $requests; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the bodies of the requests sent via Guzzle. |
| 171 | * |
| 172 | * @return array |
| 173 | */ |
| 174 | protected function getRequestUrls() { |
| 175 | $requests = []; |
| 176 | foreach ($this->getContainer() as $guzzle) { |
| 177 | $requests[] = (string) $guzzle['request']->getUri(); |
| 178 | } |
| 179 | return $requests; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the bodies of the responses returned via Guzzle. |
| 184 | * |
| 185 | * @return array |
| 186 | */ |
| 187 | protected function getResponseBodies() { |
| 188 | $responses = []; |
| 189 | foreach ($this->getContainer() as $guzzle) { |
| 190 | $responses[] = (string) $guzzle['response']->getBody(); |
| 191 | } |
| 192 | return $responses; |
| 193 | } |
| 194 | |
| 195 | } |