alekseytupichenkov / guzzle_stub
This repository provides guzzle stubs with flexible fixtures
Installs: 12 048
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:guzzle-extension
Requires
- php: ^8.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Table of contents:
Introduction
This repository provides guzzle stubs with flexible fixtures.
Installation
-
Download
Run command in console
$ composer require --dev alekseytupichenkov/guzzle_stub
Basic usage
In case of you have guzzle client for which it is necessary to write stubs and exclude the possibility make requests to external services
For example:
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; class MySuperGuzzleClient extends Client { public function __construct(HandlerStack $handlerStack = null) { parent::__construct([ 'timeout' => 300, 'base_uri' => 'http://foo.bar', 'handler' => $handlerStack ?? HandlerStack::create(), ]); } }
Create Guzzle client stub class using existing guzzle client, add trait GuzzleClientTrait, implement method loadFixtures
and add fixtures to it
use Alekseytupichenkov\GuzzleStub\Model\Fixture; use Alekseytupichenkov\GuzzleStub\Traits\GuzzleClientTrait; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; class MySuperGuzzleClientStub extends MySuperGuzzleClient { use GuzzleClientTrait; function loadFixtures() { $this->append(new Fixture( new Request('GET', 'http://foo.bar/baz', ['token' => '.*']), new Response(200, [], '{"result":"ok"}') )); $this->append(new Fixture( new Request('POST', 'http://foo.bar/baz', ['token' => '.*'], '{"data":".*"}'), new Response(200, [], '{"result":"ok"}') )); } }
Now you can write tests checking history of requests/responses
use PHPUnit\Framework\TestCase; class MySuperGuzzleClientStubTest extends TestCase { /** @var MySuperGuzzleClientStub */ private $client; public function setUp() { $this->client = new MySuperGuzzleClientStub(); } public function testPost() { $response = $this->client->post('/baz', [ 'headers' => [ 'token' => '123' ], 'body' => '{"data":"test"}' ]); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('{"result":"ok"}', $response->getBody()->__toString()); $this->assertEquals(1, $this->client->getHistoryCount()); $this->assertEquals($response, $this->client->getLatestHistoryResponse()); } }
If stub haven't sutible fixture, you'll get exception like this
1) MySuperGuzzleClientStubTest::testException Alekseytupichenkov\GuzzleStub\Exception\GuzzleStubException: Can't find suitable response for request [array ( 'method' => 'POST', 'uri' => '/without/fixture', 'body' => '', 'protocol_version' => '1.1', 'headers' => array ( 'User-Agent' => array ( 0 => 'GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.18', ), 'Host' => array ( 0 => 'foo.bar', ), ), )]