PHPackages                             doppiogancio/mocked-client - 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. doppiogancio/mocked-client

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

doppiogancio/mocked-client
==========================

A simple way to mock a client

v4.1.4(2y ago)1975.9k↓55.8%3[1 issues](https://github.com/doppiogancio/mocked-client/issues)[1 PRs](https://github.com/doppiogancio/mocked-client/pulls)3MITPHPPHP ^8.1

Since Aug 19Pushed 7mo ago3 watchersCompare

[ Source](https://github.com/doppiogancio/mocked-client)[ Packagist](https://packagist.org/packages/doppiogancio/mocked-client)[ RSS](/packages/doppiogancio-mocked-client/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (11)Versions (38)Used By (3)

[![Packagist Version](https://camo.githubusercontent.com/6811b95c3c6357233678d9916f6ac3fb240e348bb1a9e2d25397be10ed07dfca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646f7070696f67616e63696f2f6d6f636b65642d636c69656e74)](https://packagist.org/packages/doppiogancio/mocked-client)[![Packagist Downloads](https://camo.githubusercontent.com/1dd3d7cdb8418c0d5ff66bba0c15de0051092181b6cd9db32459035042e90a97/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f646f7070696f67616e63696f2f6d6f636b65642d636c69656e74)](https://packagist.org/packages/doppiogancio/mocked-client)

Mocked Client
=============

[](#mocked-client)

This package will help test components that depend on clients for HTTP calls. At the moment only Guzzle Client is supported.

Install
-------

[](#install)

Via Composer

```
$ composer require doppiogancio/mocked-client guzzlehttp/guzzle php-http/discovery
```

Requirements
------------

[](#requirements)

This version requires a minimum PHP version 8.1

How to mock a client
--------------------

[](#how-to-mock-a-client)

```
use DoppioGancio\MockedClient\HandlerBuilder;
use DoppioGancio\MockedClient\ClientBuilder;
use DoppioGancio\MockedClient\Route\RouteBuilder;
use GuzzleHttp\Psr7\Response;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Log\NullLogger;

$handlerBuilder = new HandlerBuilder(
    Psr17FactoryDiscovery::findServerRequestFactory(),
    new NullLogger()
);

$route = new RouteBuilder(
    Psr17FactoryDiscovery::findResponseFactory(),
    Psr17FactoryDiscovery::findStreamFactory(),
);

// Route with Response
$handlerBuilder->addRoute(
    $route->new()
        ->withMethod('GET')
        ->withPath('/country/IT')
        ->withResponse(new Response(200, [], '{"id":"+39","code":"IT","name":"Italy"}'))
        ->build()
);

$clientBuilder = new ClientBuilder($handlerBuilder);
$client = $clientBuilder->build();
```

### Advanced examples

[](#advanced-examples)

1. [Route with a file](./docs/route-with-file-response.md)
2. [Route with a string](./docs/route-with-string-response.md)
3. [Route with consecutive calls](./docs/route-with-consecutive-calls.md)
4. [Route with callbacks](./docs/route-with-callbacks.md)
5. [Guzzle client with middlewares](./docs/route-with-consecutive-calls.md)

How to use the client
---------------------

[](#how-to-use-the-client)

```
$response = $client->request('GET', '/country/DE/json');
$body = (string) $response->getBody();
$country = json_decode($body, true);

print_r($country);

// will return
Array
(
    [id] => +49
    [code] => DE
    [name] => Germany
)
```

Some recommendations...
-----------------------

[](#some-recommendations)

### Fail Fast, Fail Often

[](#fail-fast-fail-often)

If you don't know in advance which routes are needed, don't worry, start with a client without routes, and let it suggests which routes to add.

```
$handlerBuilder = new HandlerBuilder(
    Psr17FactoryDiscovery::findServerRequestFactory(),
    new NullLogger()
);

// don't add any route for now...

$clientBuilder = new ClientBuilder($handlerBuilder);
$client = $clientBuilder->build();
```

Run the test: the test will fail, but it will suggest you the route that is missing. By doing this, it will only specify the needed routes.

An example:

```
DoppioGancio\MockedClient\Exception\RouteNotFound: Mocked route GET /admin/dashboard not found
```

### Inject the client in the service container

[](#inject-the-client-in-the-service-container)

If you have a service container, add the client to it, so that every service depending on it will be able to auto wire.

```
self::$container->set(Client::class, $client);

// In Symfony
self::$container->set('eight_points_guzzle.client.my_client', $client);
```

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance44

Moderate activity, may be stable

Popularity38

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 91.7% 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 ~30 days

Total

27

Last Release

984d ago

Major Versions

v2.0.2 → v3.1.02022-04-15

v2.0.7 → v3.1.32023-05-09

v3.1.4 → v4.0.02023-05-09

v3.1.10 → v4.1.02023-06-09

v3.x-dev → v4.1.12023-08-25

PHP version history (2 changes)v2.0.0PHP ^7.4|^8.0

v4.0.0PHP ^8.1

### Community

Maintainers

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

---

Top Contributors

[![doppiogancio](https://avatars.githubusercontent.com/u/7829649?v=4)](https://github.com/doppiogancio "doppiogancio (22 commits)")[![Sauerbrei](https://avatars.githubusercontent.com/u/17849159?v=4)](https://github.com/Sauerbrei "Sauerbrei (1 commits)")[![smoench](https://avatars.githubusercontent.com/u/183530?v=4)](https://github.com/smoench "smoench (1 commits)")

---

Tags

httpphptestingclientGuzzlemock

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/doppiogancio-mocked-client/health.svg)

```
[![Health](https://phpackages.com/badges/doppiogancio-mocked-client/health.svg)](https://phpackages.com/packages/doppiogancio-mocked-client)
```

###  Alternatives

[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[danog/madelineproto

Async PHP client API for the telegram MTProto protocol.

3.5k902.0k23](/packages/danog-madelineproto)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[blastcloud/guzzler

Supercharge your app or SDK with a testing library specifically for Guzzle.

272470.1k42](/packages/blastcloud-guzzler)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6942.5M421](/packages/drupal-core-recommended)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M574](/packages/shopware-core)

PHPackages © 2026

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