5 use GuzzleHttp\Handler\MockHandler
;
6 use GuzzleHttp\HandlerStack
;
7 use GuzzleHttp\Psr7\Response
;
8 use GuzzleHttp\Middleware
;
12 * Class GuzzleTestTrait
14 * This trait defines a number of helper functions for testing Guzzle-based logic,
15 * such as a payment-processing gateway.
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.
20 trait GuzzleTestTrait
{
22 * @var \GuzzleHttp\Client
24 protected $guzzleClient;
27 * Array containing guzzle history of requests and responses.
34 * Mockhandler to simulate guzzle requests.
36 * @var \GuzzleHttp\Handler\MockHandler
38 protected $mockHandler;
41 * The url to mock-interact with.
48 * @return \GuzzleHttp\Client
50 public function getGuzzleClient() {
51 return $this->guzzleClient
;
55 * @param \GuzzleHttp\Client $guzzleClient
57 public function setGuzzleClient($guzzleClient) {
58 $this->guzzleClient
= $guzzleClient;
64 public function getContainer() {
65 return $this->container
;
69 * @param array $container
71 public function setContainer($container) {
72 $this->container
= $container;
78 public function getBaseUri() {
79 return $this->baseUri
;
83 * @param mixed $baseUri
85 public function setBaseUri($baseUri) {
86 $this->baseUri
= $baseUri;
90 * @return \GuzzleHttp\Handler\MockHandler
92 public function getMockHandler() {
93 return $this->mockHandler
;
97 * @param \GuzzleHttp\Handler\MockHandler $mockHandler
99 public function setMockHandler($mockHandler) {
100 $this->mockHandler
= $mockHandler;
106 protected function createMockHandler($responses) {
108 foreach ($responses as $response) {
109 $mocks[] = new Response(200, [], $response);
111 $this->setMockHandler(new MockHandler($mocks));
117 protected function createMockHandlerForFiles($files) {
119 foreach ($files as $file) {
120 $body[] = trim(file_get_contents(__DIR__
. $file));
122 $this->createMockHandler($body);
126 * Set up a guzzle client with a history container.
128 * After you have run the requests you can inspect $this->container
129 * for the outgoing requests and incoming responses.
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.
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]);
144 * Get the bodies of the requests sent via Guzzle.
148 protected function getRequestBodies() {
150 foreach ($this->getContainer() as $guzzle) {
151 $requests[] = (string) $guzzle['request']->getBody();
157 * Get the bodies of the requests sent via Guzzle.
161 protected function getRequestHeaders() {
163 foreach ($this->getContainer() as $guzzle) {
164 $requests[] = $guzzle['request']->getHeaders();
170 * Get the bodies of the requests sent via Guzzle.
174 protected function getRequestUrls() {
176 foreach ($this->getContainer() as $guzzle) {
177 $requests[] = (string) $guzzle['request']->getUri();
183 * Get the bodies of the responses returned via Guzzle.
187 protected function getResponseBodies() {
189 foreach ($this->getContainer() as $guzzle) {
190 $responses[] = (string) $guzzle['response']->getBody();