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.
16 trait GuzzleTestTrait
{
18 * @var \GuzzleHttp\Client
20 protected $guzzleClient;
23 * Array containing guzzle history of requests and responses.
30 * Mockhandler to simulate guzzle requests.
32 * @var \GuzzleHttp\Handler\MockHandler
34 protected $mockHandler;
37 * The url to mock-interact with.
44 * @return \GuzzleHttp\Client
46 public function getGuzzleClient() {
47 return $this->guzzleClient
;
51 * @param \GuzzleHttp\Client $guzzleClient
53 public function setGuzzleClient($guzzleClient) {
54 $this->guzzleClient
= $guzzleClient;
60 public function getContainer() {
61 return $this->container
;
65 * @param array $container
67 public function setContainer($container) {
68 $this->container
= $container;
74 public function getBaseUri() {
75 return $this->baseUri
;
79 * @param mixed $baseUri
81 public function setBaseUri($baseUri) {
82 $this->baseUri
= $baseUri;
86 * @return \GuzzleHttp\Handler\MockHandler
88 public function getMockHandler() {
89 return $this->mockHandler
;
93 * @param \GuzzleHttp\Handler\MockHandler $mockHandler
95 public function setMockHandler($mockHandler) {
96 $this->mockHandler
= $mockHandler;
102 protected function createMockHandler($responses) {
104 foreach ($responses as $response) {
105 $mocks[] = new Response(200, [], $response);
107 $this->setMockHandler(new MockHandler($mocks));
113 protected function createMockHandlerForFiles($files) {
115 foreach ($files as $file) {
116 $body[] = trim(file_get_contents(__DIR__
. $file));
118 $this->createMockHandler($body);
122 * Set up a guzzle client with a history container.
124 * After you have run the requests you can inspect $this->container
125 * for the outgoing requests and incoming responses.
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.
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]);
140 * Get the bodies of the requests sent via Guzzle.
144 protected function getRequestBodies() {
146 foreach ($this->getContainer() as $guzzle) {
147 $requests[] = (string) $guzzle['request']->getBody();
153 * Get the bodies of the requests sent via Guzzle.
157 protected function getRequestHeaders() {
159 foreach ($this->getContainer() as $guzzle) {
160 $requests[] = $guzzle['request']->getHeaders();
166 * Get the bodies of the requests sent via Guzzle.
170 protected function getRequestUrls() {
172 foreach ($this->getContainer() as $guzzle) {
173 $requests[] = (string) $guzzle['request']->getUri();
179 * Get the bodies of the responses returned via Guzzle.
183 protected function getResponseBodies() {
185 foreach ($this->getContainer() as $guzzle) {
186 $responses[] = (string) $guzzle['response']->getBody();