Page MenuHomeDevCentral

WithMockHttpClient.php
No OneTemporary

WithMockHttpClient.php

<?php
namespace Keruald\Mailgun\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
trait WithMockHttpClient {
/**
* @return \GuzzleHttp\
*/
public function mockHttpClient () {
$body = $this->mockHttpClientResponseBody();
return self::mockHttpClientWithCustomResponse(200, $body);
}
/**
* @return \GuzzleHttp\Client
*/
public function mockHttpClientWithCustomResponse ($code, $body) {
$handler = self::getCustomMockHttpClientHandler($code, $body);
return new Client(['handler' => $handler]);
}
/**
* @return stdClass
*/
public function mockEventPayload () {
return json_decode(file_get_contents(__DIR__ . '/payload.json'));
}
///
/// Mock helper methods
///
/**
* @return \GuzzleHttp\HandlerStack
*/
protected static function getCustomMockHttpClientHandler ($code, $body, $headers = []) {
return HandlerStack::create(new MockHandler([
new Response($code, $headers, $body),
]));
}
/**
* @return string
*/
protected static function mockHttpClientResponseBody () {
return file_get_contents(__DIR__ . '/response.json');
}
}

File Metadata

Mime Type
text/x-php
Expires
Wed, Jul 8, 15:03 (23 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3903895
Default Alt Text
WithMockHttpClient.php (1 KB)

Event Timeline