PHPackages                             chillerlan/phpunit-http - 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. chillerlan/phpunit-http

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

chillerlan/phpunit-http
=======================

Add PSR-17 factories and a PSR-18 client to your tests

2.0.0(3mo ago)01.1k4MITPHPPHP ^8.4CI passing

Since Mar 15Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/chillerlan/phpunit-http)[ Packagist](https://packagist.org/packages/chillerlan/phpunit-http)[ Docs](https://github.com/chillerlan/phpunit-http)[ Fund](https://ko-fi.com/codemasher)[ RSS](/packages/chillerlan-phpunit-http/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (4)Dependencies (16)Versions (6)Used By (4)

chillerlan/phpunit-http
=======================

[](#chillerlanphpunit-http)

Add [PSR-17](https://www.php-fig.org/psr/psr-17/) factories and a [PSR-18](https://www.php-fig.org/psr/psr-18/) client for use in your [PHPUnit](https://phpunit.de/) tests.

[![PHP Version Support](https://camo.githubusercontent.com/659eb95296242c5f3c3de9fba814c19e7462f8f1015023e66a6d92ecb157df8a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6368696c6c65726c616e2f706870756e69742d687474703f6c6f676f3d70687026636f6c6f723d383839324246266c6f676f436f6c6f723d666666)](https://www.php.net/supported-versions.php)[![Packagist version](https://camo.githubusercontent.com/73e9cd58ce782b6441678f5297a44ddcae92c58f6eeb978b1ed0f29a827f955e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368696c6c65726c616e2f706870756e69742d687474702e7376673f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d666666)](https://packagist.org/packages/chillerlan/phpunit-http)[![License](https://camo.githubusercontent.com/06a3bc0633f41c398d59c69fb6dff216d7fb8156cb7d323a958224d122caf777/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368696c6c65726c616e2f706870756e69742d68747470)](https://github.com/chillerlan/phpunit-http/blob/main/LICENSE)[![Continuous Integration](https://camo.githubusercontent.com/48a6a3e66dbe815b25e19bd42fcfa8291aeb4c260b5875c4a073f04c97c63811/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6368696c6c65726c616e2f706870756e69742d687474702f63692e796d6c3f6272616e63683d6d61696e266c6f676f3d676974687562266c6f676f436f6c6f723d666666)](https://github.com/chillerlan/phpunit-http/actions/workflows/ci.yml?query=branch%3Amain)[![Packagist downloads](https://camo.githubusercontent.com/248350ce39f2963283d92baccbd80f53d5ddf82ff2c22bd0a35d13365b869013/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368696c6c65726c616e2f706870756e69742d687474702e7376673f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d666666)](https://packagist.org/packages/chillerlan/phpunit-http/stats)

Overview
--------

[](#overview)

### Features

[](#features)

A PSR-18 HTTP client and PSR-17 factories for your unit tets. That's it.

### Requirements

[](#requirements)

- PHP 8.4+
    - required extensions may vary depending on the used HTTP client

Documentation
-------------

[](#documentation)

### Installation with [composer](https://getcomposer.org)

[](#installation-with-composer)

Add this library along with PHPUnit and an optional HTTP client to the `require-dev` section of your `composer.json`:

```
{
	"require": {
		"php": "^8.4"
	},
	"require-dev": {
		"chillerlan/phpunit-http": "^2.0",
		"guzzlehttp/guzzle": "^7.10",
		"phpunit/phpunit": "^13.0"
	}
}
```

### Usage

[](#usage)

Include the `HttpFactoryTrait` and call `initFactories()` from within PHPUnit's `TestCase::setUp()`:

```
class MyUnitTest extends \PHPUnit\Framework\TestCase{
	// include the factory trait
	use \chillerlan\PHPUnitHttp\HttpFactoryTrait;

	// you can define the factories either as properties in your test class or in phpunit.xml
	protected string $REQUEST_FACTORY  = MyRequestFactory::class;
	protected string $RESPONSE_FACTORY = MyResponseFactory::class;
	protected string $STREAM_FACTORY   = MyStreamFactory::class;
	protected string $URI_FACTORY      = MyUriFactory::class;

	// these three factories may not always be needed and/or implemented,
	// you can just unset or simply omit the properties
	protected string $HTTP_CLIENT_FACTORY = \chillerlan\PHPUnitHttp\GuzzleHttpClientFactory::class;
	protected string $SERVER_REQUEST_FACTORY;
	protected string $UPLOADED_FILE_FACTORY;

	// a CA bundle is required when using a http client
	protected const CACERT = __DIR__.'/cacert.pem';

	// in PHPUnit's setUp, call the factory initializer
	protected function setUp():void{
		try{
			$this->initFactories(realpath($this::CACERT));
		}
		catch(\Throwable $e){
			$this->markTestSkipped('unable to init http factories: '.$e->getMessage());
		}
	}

	// use the factories
	public function testSomething():void{
		$uri      = $this->uriFactory->createUri('https://example.com');
		$request  = $this->requestFactory->createRequest('GET', $uri);
		$response = $this->httpClient->sendRequest($request);

		// do stuff
		$this::assertSame(200, $response->getStatusCode());
	}

}
```

Instead of setting the properties in the test classes, you can define them as constants in your `phpunit.xml`:

```

			tests

			src

```

Profit!

### Custom HTTP client

[](#custom-http-client)

You can implement the `HttpClientFactoryInterface` to create your own HTTP client factory:

```
final class MyHttpClientFactory implements HttpClientFactoryInterface{

	public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{
		return new MyHttpClient(['cacert' => $cacert, /* ... */]);
	}

}
```

Disclaimer
----------

[](#disclaimer)

Use at your own risk!

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance82

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~184 days

Total

5

Last Release

93d ago

Major Versions

v1.x-dev → 2.0.02026-03-21

PHP version history (2 changes)1.0.0PHP ^8.1

2.0.0PHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/592497?v=4)[smiley](/maintainers/codemasher)[@codemasher](https://github.com/codemasher)

---

Top Contributors

[![codemasher](https://avatars.githubusercontent.com/u/592497?v=4)](https://github.com/codemasher "codemasher (13 commits)")

---

Tags

httpphpunitpsr-17psr-18factories

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/chillerlan-phpunit-http/health.svg)

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

###  Alternatives

[phpunit/phpunit

The PHP Unit Testing framework.

20.0k940.0M149.9k](/packages/phpunit-phpunit)[dg/bypass-finals

Removes final keyword from source code on-the-fly and allows mocking of final methods and classes

57028.2M568](/packages/dg-bypass-finals)[donatj/mock-webserver

Simple mock web server for unit testing

1392.6M92](/packages/donatj-mock-webserver)[elastic/transport

HTTP transport PHP library for Elastic products

2022.9M9](/packages/elastic-transport)[http-interop/http-factory-tests

Unit tests for HTTP factories

11238.2k39](/packages/http-interop-http-factory-tests)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28146.3k](/packages/phpro-http-tools)

PHPackages © 2026

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