verdet/guzzle-mock

Guzzle Mock Handler

Installs: 59

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 1

Type:guzzle-extension

1.0.0 2022-08-15 11:11 UTC

This package is auto-updated.

Last update: 2024-04-04 17:26:45 UTC


README

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