PHPackages                             aksoyih/http-mock - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. aksoyih/http-mock

ActiveLibrary[HTTP &amp; Networking](/categories/http)

aksoyih/http-mock
=================

A PHP library for mocking HTTP clients with customizable responses and behaviors

v2.0.0(3mo ago)24051MITPHPPHP ^8.4CI passing

Since Jan 20Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/aksoyih/http-mock)[ Packagist](https://packagist.org/packages/aksoyih/http-mock)[ RSS](/packages/aksoyih-http-mock/feed)WikiDiscussions master Synced today

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

HTTP Mock
=========

[](#http-mock)

[![CI](https://github.com/aksoyih/http-mock/actions/workflows/ci.yml/badge.svg)](https://github.com/aksoyih/http-mock/actions/workflows/ci.yml)

A PHP library for mocking HTTP clients with customizable responses, flexible request matching, and full verification support. Works with PSR-18, Guzzle, and Symfony HttpClient.

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

[](#installation)

```
composer require aksoyih/http-mock --dev
```

Quick Start
-----------

[](#quick-start)

```
use Aksoyih\HttpMock\HttpMock;

// Define a mock
HttpMock::when('GET', '/users')
    ->willReturn(['id' => 1, 'name' => 'John'])
    ->withStatus(200)
    ->register();

// Create a PSR-18 client
$client = HttpMock::createClient();
$response = $client->sendRequest($request);

// Verify it was called
HttpMock::assertCalled('GET', '/users');
```

Request Matching
----------------

[](#request-matching)

### Exact Match

[](#exact-match)

```
HttpMock::when('GET', '/users/123')
    ->willReturn(['id' => 123])
    ->register();
```

### Wildcard Patterns

[](#wildcard-patterns)

```
// * matches a single path segment
HttpMock::when('GET', '/users/*/posts')
    ->willReturn([])
    ->register();

// ** matches multiple segments
HttpMock::when('GET', '/api/**')
    ->willReturn([])
    ->register();
```

### Regex Patterns

[](#regex-patterns)

```
HttpMock::when('GET', '#^/users/\d+$#')
    ->willReturn(['found' => true])
    ->register();
```

### Callable Matchers

[](#callable-matchers)

```
HttpMock::whenMatching(fn($method, $url, $headers, $body) =>
    $method === 'POST' && str_contains($url, '/admin')
)->willReturn(['access' => 'granted'])
    ->register();
```

Shorthand
---------

[](#shorthand)

```
HttpMock::fake([
    'GET /users/*' => HttpMock::response(['id' => 1], 200),
    'POST /users'  => HttpMock::response(['created' => true], 201),
]);
```

Verification
------------

[](#verification)

```
HttpMock::assertCalled('GET', '/users');
HttpMock::assertCalledTimes('POST', '/users', 3);
HttpMock::assertNotCalled('DELETE', '/users');

// Or use expect() constraints
HttpMock::when('GET', '/users')
    ->willReturn([])
    ->expect(times: 2)
    ->register();

// ... make requests ...

HttpMock::verify(); // throws if expectations not met
```

HTTP Client Adapters
--------------------

[](#http-client-adapters)

### PSR-18

[](#psr-18)

```
$client = HttpMock::createClient();
```

### Guzzle

[](#guzzle)

```
$handler = HttpMock::createGuzzleHandler();
$stack = HandlerStack::create($handler);
$client = new \GuzzleHttp\Client(['handler' => $stack]);
```

### Symfony HttpClient

[](#symfony-httpclient)

```
$client = HttpMock::createSymfonyClient();
```

Unmatched Request Handling
--------------------------

[](#unmatched-request-handling)

```
// Strict mode (default) — throws on unmatched requests
HttpMock::onUnmatched(HttpMock::THROW);

// Return 404 for unmatched requests
HttpMock::onUnmatched(HttpMock::DEFAULT_404);

// Custom handler
HttpMock::onUnmatched(fn($method, $url) => [
    'status' => 503,
    'headers' => [],
    'body' => 'Service unavailable',
]);
```

Test Isolation
--------------

[](#test-isolation)

### PHPUnit Trait (recommended)

[](#phpunit-trait-recommended)

```
use Aksoyih\HttpMock\Testing\MocksHttp;

class MyTest extends TestCase
{
    use MocksHttp; // auto-resets after each test
}
```

### Scoped

[](#scoped)

```
HttpMock::scoped(function () {
    HttpMock::when('GET', '/users')->willReturn([])->register();
    // ... test code ...
}); // auto-resets, even on exception
```

### Manual

[](#manual)

```
HttpMock::reset();
```

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

[](#requirements)

- PHP 8.4+
- `psr/http-client` ^1.0
- `nyholm/psr7` ^1.0

Optional:

- `guzzlehttp/guzzle` ^7.0 (for Guzzle adapter)
- `symfony/http-client` ^7.0 (for Symfony adapter)

License
-------

[](#license)

MIT

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance80

Actively maintained with recent releases

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% 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 ~210 days

Total

3

Last Release

108d ago

Major Versions

1.0.0 → v2.0.02026-03-17

PHP version history (2 changes)1.0.0PHP ^8.1

v2.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/5e60d1e2913a8bed8c8f2d8d5227360633a65cb625960406d964835b8b96e23b?d=identicon)[aksoyih](/maintainers/aksoyih)

---

Top Contributors

[![aksoyih](https://avatars.githubusercontent.com/u/54416061?v=4)](https://github.com/aksoyih "aksoyih (3 commits)")[![raphaelstolt](https://avatars.githubusercontent.com/u/48225?v=4)](https://github.com/raphaelstolt "raphaelstolt (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aksoyih-http-mock/health.svg)

```
[![Health](https://phpackages.com/badges/aksoyih-http-mock/health.svg)](https://phpackages.com/packages/aksoyih-http-mock)
```

###  Alternatives

[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[mollie/mollie-api-php

Mollie API client library for PHP. Mollie is a European Payment Service provider and offers international payment methods such as Mastercard, VISA, American Express and PayPal, and local payment methods such as iDEAL, Bancontact, SOFORT Banking, SEPA direct debit, Belfius Direct Net, KBC Payment Button and various gift cards such as Podiumcadeaukaart and fashioncheque.

60216.0M85](/packages/mollie-mollie-api-php)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[drupal/core-recommended

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

6942.5M421](/packages/drupal-core-recommended)[laudis/neo4j-php-client

Neo4j-PHP-Client is the most advanced PHP Client for Neo4j

185702.8k44](/packages/laudis-neo4j-php-client)[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)
