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