PHPackages                             jdgrimes/wp-http-testcase - 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. jdgrimes/wp-http-testcase

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

jdgrimes/wp-http-testcase
=========================

PHPUnit testcase for WordPress code involving HTTP requests

1.3.1(8y ago)820.3k3MITPHPPHP &gt;=5.2.0

Since Feb 3Pushed 7y ago2 watchersCompare

[ Source](https://github.com/JDGrimes/wp-http-testcase)[ Packagist](https://packagist.org/packages/jdgrimes/wp-http-testcase)[ Docs](https://github.com/JDGrimes/wp-http-testcase)[ RSS](/packages/jdgrimes-wp-http-testcase/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)DependenciesVersions (8)Used By (0)

WP HTTP Testcase
================

[](#wp-http-testcase)

PHPUnit testcase for testing code that uses WordPress's `WP_Http` class.

If you use `wp_remote_request()` or other wrappers for `WP_Http` methods in your code, this makes it difficult to test, especially if the remote server may not be reachable from your testing environment. This testcase solves this by letting you route your requests to a different host address, use a cached set of responses, or just mock the remote responses by supplying artificial ones.

Installation
============

[](#installation)

You can install this package using composer:

```
composer require --dev jdgrimes/wp-http-testcase
```

Usage
=====

[](#usage)

To use it in your code, you need to first include the `wp-http-testcase.php` file in your PHPUnit bootstrap file. (Or, to take advantage of Composer's autoloading, you can just include `vendor/autoload.php`.)

Then, in your tests that involve `WP_Http`, you need to extend `WP_HTTP_TestCase`instead of `WP_UnitTestCase` as you normally would.

Mocking Responses
-----------------

[](#mocking-responses)

### Using Response Caching

[](#using-response-caching)

The best way of testing, when possible, it to set up a mock host to handle the requests. In some cases, you may want or need to actually send the requests through to the real server, and that can be done as well. Which of these you do will depend on the nature of the requests, and what side-effects they produce on the recipient host.

#### Setting Up a Test Host

[](#setting-up-a-test-host)

For example, if you are testing a plugin that makes requests to an API provided by another plugin or other software, you probably don't want or need to test this on a live site. Instead, you can set up a test site, or use a local server that is part of your development environment. There you can install the software that handles the requests. Once this is done, you can run your tests against that test site like this:

```
WP_HTTP_TC_HOST=localhost phpunit
```

Just replace `localhost` with the hostname of the local server. Note that the `WP_HTTP_TC_*` flags can be defined as PHP constants, or as bash environment variables as above. The latter will take precedence.

#### Enabling Caching

[](#enabling-caching)

Of course this will be much slower than most other unit tests, because the requests are bound to take a bit of time. That is where caching comes in. When caching is enabled, the response to each request is cached the first time it is run, and the cached version is used in the future. This means that your tests can remain lightning fast.

To enable caching, just add this to your bootstrap:

```
define( 'WP_HTTP_TC_USE_CACHING', true );
```

You'll probably also want to specify the directory to save the cache in, via `WP_HTTP_TC_CACHE_DIR`. You can utilize multiple cache groups and switch between them using `WP_HTTP_TC_CACHE_GROUP`.

#### Using the Live Host

[](#using-the-live-host)

There is the second case though, where you are unable to set up a test server. An example where this would be the case would be if your plugin makes requests to the API provided by GitHub. Depending on the situation, it may be feasible to actually make the requests to the "live" recipient. The main issue again is that the requests will make the tests take a long time to complete. There is also the possibility that the API isn't always accessible from your testing environment, or that your tests will end up pounding the API too hard and you'll get blocked. This is where caching can help you. You only need to run your tests against the "live" API once in a while, and the rest of the time you can test using the cached responses.

### Supplying Artificial Responses

[](#supplying-artificial-responses)

Of course, there may be times when it isn't possible to create a test server, and it isn't feasible to run against the live server either. In this case, you may want to hard-code artificial responses into your tests. Here is how you can do that:

Before calling the code that will invoke the HTTP request, you need to set the function to mock the responses like so:

```
$this->http_responder = array( $this, 'mock_server_response' );
```

The HTTP responder function will be passed two arguments, the request arguments and the URL the request was intended for.

```
protected function mock_server_response( $request, $url ) {

	return array( 'body' => 'Test response.' );
}
```

For a full list of the `$request` and response arguments, see [`WP_Http::request()`](http://developer.wordpress.org/reference/classes/wp_http/request/#source-code)

Testing Requests
----------------

[](#testing-requests)

You may also wish to test that your code is making requests as expected. You can do this by checking the value of `$this->http_requests`, which is an array of requests. Each entry in the array stores the request arguments (`'request'` key) and URL (`'url'` key).

To check that a request was made, you could do something like this:

```
$this->assertCount( 1, $this->http_requests );
$this->assertEquals( 'http://example.com/', $this->http_requests[0]['url'] );
```

When you just want to test the request and don't care about the response, you can short-circuit the request before it is made, by setting the response mocker to be `__return_true()`:

```
$this->http_responder = '__return_true';

my_prefix_make_request();

$this->assertCount( 1, $this->http_requests );
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 94% 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 ~141 days

Recently: every ~211 days

Total

7

Last Release

3262d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4005415?v=4)[J.D. Grimes](/maintainers/jdgrimes)[@JDGrimes](https://github.com/JDGrimes)

---

Top Contributors

[![JDGrimes](https://avatars.githubusercontent.com/u/4005415?v=4)](https://github.com/JDGrimes "JDGrimes (47 commits)")[![jamesgol](https://avatars.githubusercontent.com/u/6788517?v=4)](https://github.com/jamesgol "jamesgol (1 commits)")[![mAAdhaTTah](https://avatars.githubusercontent.com/u/4371429?v=4)](https://github.com/mAAdhaTTah "mAAdhaTTah (1 commits)")[![rodrigoprimo](https://avatars.githubusercontent.com/u/77215?v=4)](https://github.com/rodrigoprimo "rodrigoprimo (1 commits)")

---

Tags

phpunitphpunit-testcasetestingwordpress

### Embed Badge

![Health badge](/badges/jdgrimes-wp-http-testcase/health.svg)

```
[![Health](https://phpackages.com/badges/jdgrimes-wp-http-testcase/health.svg)](https://phpackages.com/packages/jdgrimes-wp-http-testcase)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M678](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M753](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M571](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.0k](/packages/orchestra-testbench)

PHPackages © 2026

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