PHPackages                             webwhales/laravel-guzzle-conditional-mock-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. webwhales/laravel-guzzle-conditional-mock-handler

ActiveLibrary[Testing &amp; Quality](/categories/testing)

webwhales/laravel-guzzle-conditional-mock-handler
=================================================

Offers a way to load mocked responses conditionally with Guzzle instead of a fixed queue in Laravel

2.1.4(11mo ago)730.5k1MITPHPPHP ^7.2|^8.0CI failing

Since Apr 27Pushed 11mo agoCompare

[ Source](https://github.com/WebWhales/laravel-guzzle-conditional-mock-handler)[ Packagist](https://packagist.org/packages/webwhales/laravel-guzzle-conditional-mock-handler)[ Docs](https://github.com/WebWhales/laravel-guzzle-conditional-mock-handler)[ RSS](/packages/webwhales-laravel-guzzle-conditional-mock-handler/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (9)Dependencies (4)Versions (10)Used By (0)

Laravel Conditional Mock Handler for Guzzle
===========================================

[](#laravel-conditional-mock-handler-for-guzzle)

This package offers an easy way to use the [Conditional Mock Handler for Guzzle](https://github.com/WebWhales/guzzle-conditional-mock-handler) package while writing tests in a Laravel application. The Conditional Mock Handler can be used to load mocked responses conditionally based on the URL, instead of a fixed queue.

Installation
------------

[](#installation)

Install this package using composer:

```
composer require --dev webwhales/laravel-guzzle-conditional-mock-handler

```

Simple Example
--------------

[](#simple-example)

To use the Conditional Mock Handler, you'll have to use the `TestWithGuzzleMockHandler` trait in your test. Furthermore, the Guzzle client has to instantiated by either the Laravel container or Laravel's dependency injection.

See the following example:

```
use Illuminate\Foundation\Testing\TestCase;
use WebWhales\LaravelGuzzleConditionalMockHandler\TestWithGuzzleMockHandler;

class ASimpleTest extends TestCase
{
    use TestWithGuzzleMockHandler;

    public function testSomething()
    {
        /**
         * @var \GuzzleHttp\Client $client
         */
        $client = $this->app->make(Client::class);

        // Add a mocked response
        $this->loadMockedResponseData('https://example.com', 'This is a test');

        // Get the response
        $response = $client->get('https://example.com');

		// Test the response content
        $this->assertEquals('This is a test', $response->getBody()->getContents());
    }
```

You can also use a Guzzle `Response` object (or any object implementing the `\Psr\Http\Message\ResponseInterface`interface), to give you more control about the response object itself:

```
// Add a mocked response
$this->loadMockedResponse('https://example.com', new Response(400, ['X-Test' => 'Test'], 'This is a test'));

// Get the response
$response = $client->get('https://example.com');

// Test the response content
$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals('This is a test', $response->getBody()->getContents());
```

Regex example
-------------

[](#regex-example)

The [Conditional Mock Handler for Guzzle](https://github.com/WebWhales/guzzle-conditional-mock-handler) package also supports regex patterns:

```
// Add mocked responses
$this->loadMockedResponseData('^http(s)?://example\.', 'This is a test');

// Make a request to a matching URL
$response = $client->request('GET', 'https://example.com');

// Test the response content
$this->assertEquals('This is a test', $response->getBody()->getContents());

// Make a request to a non matching URL
// This will retrieve the actual content of https://www.example.com
$response = $client->request('GET', 'https://www.example.com');

// Test the response content
$this->assertNotEquals('This is a test', $response->getBody()->getContents());
```

License
-------

[](#license)

The this package is open source software licensed under the [MIT license](https://opensource.org/licenses/MIT)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance52

Moderate activity, may be stable

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 76.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~234 days

Recently: every ~289 days

Total

9

Last Release

339d ago

Major Versions

1.0 → 2.02021-08-16

PHP version history (3 changes)1.0PHP ^7.2

2.0PHP ^7.2|^8.0

2.1.1PHP ^7.2|^8.0|^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/a8b6bb09e85274734d6040f0e6b3fcb393db59c20d984f5e217be3b272550089?d=identicon)[redelschaap](/maintainers/redelschaap)

---

Top Contributors

[![redelschaap](https://avatars.githubusercontent.com/u/6915990?v=4)](https://github.com/redelschaap "redelschaap (16 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")

---

Tags

httptestingmockinglaravelGuzzle

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webwhales-laravel-guzzle-conditional-mock-handler/health.svg)

```
[![Health](https://phpackages.com/badges/webwhales-laravel-guzzle-conditional-mock-handler/health.svg)](https://phpackages.com/packages/webwhales-laravel-guzzle-conditional-mock-handler)
```

###  Alternatives

[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[hotmeteor/spectator

Testing helpers for your OpenAPI spec

3021.4M1](/packages/hotmeteor-spectator)[orchestra/workbench

Workbench Companion for Laravel Packages Development

8017.0M43](/packages/orchestra-workbench)[osteel/openapi-httpfoundation-testing

Validate HttpFoundation requests and responses against OpenAPI (3+) definitions

1201.9M6](/packages/osteel-openapi-httpfoundation-testing)[aeris/guzzle-http-mock

A mock library for verifying requests made with the Guzzle Http Client, and mocking responses.

2613.1k1](/packages/aeris-guzzle-http-mock)[guanguans/laravel-soar

SQL optimizer and rewriter for laravel. - laravel 的 SQL 优化器和重写器。

2227.8k](/packages/guanguans-laravel-soar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
