Skip to content

verdet23/guzzle-mock

Repository files navigation

Guzzle Mock Handler

Packagist Version CI Coverage Status PHPStan Coding Style License Packagist PHP Version Support

Description

Inspired by guzzle/guzzle MockHandler and alekseytupichenkov/GuzzleStub library.

Mock Handler functional same as default GuzzleHttp\Handler\MockHandler except filling queue. Argument $queue expected to be array of Request and Response objects. When you pass Request to MockHanlder it will try to find suitable Request in queue and return paired Response.

Prerequisite

php >= 8.0
guzzlehttp/guzzle >= 7.0

Installation

Use the package manager composer to install.

composer require --dev verdet/guzzle-mock

Basic usage

// Create a mock and queue three pairs of request and responses.
$mock = new MockHandler([
            [
                new Request('GET', 'https://example.com'),
                new Response(200, ['X-Foo' => 'Bar'], 'Hello, World')
            ],
            [
                new Request('GET', 'https://example.com/latest'),
                new Response(202, ['Content-Length' => '0'])],
            [
                new Request('POST', 'https://example.com/foo'),
                new RequestException('Error Communicating with Server', new Request('POST', 'https://example.com/foo'))
            ]
        ]);

Rest of usage same as https://docs.guzzlephp.org/en/stable/testing.html