PHPackages                             ideasonpurpose/wp-test-stubs - 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. ideasonpurpose/wp-test-stubs

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

ideasonpurpose/wp-test-stubs
============================

A simple collection of stubs and doubles for testing WordPress code.

v0.2.0(1y ago)0272↓100%[2 issues](https://github.com/ideasonpurpose/wp-test-stubs/issues)4MITPHPPHP &gt;=8.0.0

Since Nov 30Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/ideasonpurpose/wp-test-stubs)[ Packagist](https://packagist.org/packages/ideasonpurpose/wp-test-stubs)[ RSS](/packages/ideasonpurpose-wp-test-stubs/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (3)Used By (4)

wp-test-stubs
=============

[](#wp-test-stubs)

WP Test Stubs
=============

[](#wp-test-stubs-1)

#### Version 0.2.0

[](#version-020)

[![Packagist](https://camo.githubusercontent.com/4ee760bdd84c99373abbb47112041dcba564bd25d53b5162bcef85706a350268/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f69646561736f6e707572706f73652f77702d746573742d7374756273)](https://packagist.org/packages/ideasonpurpose/wp-test-stubs)[![styled with prettier](https://camo.githubusercontent.com/7c31269a4fab27bd4b327813e7a459cf7a76da0b0b009254bf949a20889d2b6c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c65645f776974682d70726574746965722d6666363962342e737667)](https://github.com/prettier/prettier)

A simple collection of stubs and doubles for testing WordPress code.

Unlike [Brain Monkey](https://brain-wp.github.io/BrainMonkey/) or [WP\_Mock](https://github.com/10up/wp_mock), this is a very dumb library.

Most functions are just empty stubs or return a matching global variable.

`add_action` and `add_filter`
-----------------------------

[](#add_action-and-add_filter)

The `add_action`, `remove_action`, `add_filter` and `remove_filter` functions record calls in global `$actions` or `$filters` arrays. Each call pushes an associative array onto the stack containing the `hook`, `action`, `priority` and `args`. Check those global arrays to see whether the action/filter was called correctly.

Any functions which need testing should be public and capable of being tested independently.

### `all_added_actions` and `all_added_filters` helper functions

[](#all_added_actions-and-all_added_filters-helper-functions)

These two helper functions return a simplified view of the global `$actions` and `$filters` arrays with each added hook represented as two-item, hook/action array: `['hook_name', 'method_name']`. The two parallel functions, `all_removed_actions` and `all_removed_filters` can be used with PHPUnit like this:

```
$this->assertContains(['hook_name', 'method_name'], all_added_filters());
```

For short-arrow and anonymous functions (Closures), test for their returned values. For example, `fn() => 5` can be validated with `assertContains(['hook_name', 5]`.

is\_{$something} functions
--------------------------

[](#is_something-functions)

All of these functions are mocked from the same pattern: Each will return the value of a global with the same name as the function. Since these functions are often used for control flow, being able to easily toggle their values makes it simple to test alternate pathways through System Under Test code without having to refactor.

To toggle any `is_` function, set a value like this:

```
global $is_admin;
$is_admin = true;
```

i18n functions
--------------

[](#i18n-functions)

Several [basic WordPress i18n functions](https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#basic-functions) are stubbed. These all work the same way. If `$i18n` global array is defined, the first string argument will be used as a key and any assigned value will be returned. If no key is defined, the original string is returned.

Example:

```
$i18n = [
  'Bird' => 'pájaro',
  'Birds' => 'pájaros',
]

__('bird', 'ignored'); // pájaro
_x('Bird', 'ignored', 'also-ignored'); // pájaro
_n('Bird', 'Birds', 3, 'ignored'); // pájaros
```

Note that `_n()` will return through sprintf, templates should appear as keys the same as they would in a **\*.pot** file:

```
$i18n = [
  '%s bird' => '%s pájaro',
  '%s birds' => '%s pájaros',
]
```

Replacing error\_log
--------------------

[](#replacing-error_log)

A simple stub of PHP's built-in [`error_log`](https://www.php.net/manual/en/function.error-log.php) function is provided. Use namespaces to override the native function, add something like this to the top of your test files:

```
Test\Stubs::init();

if (!function_exists(__NAMESPACE__ . '\error_log')) {
    function error_log($err)
    {
        Test\Stubs::error_log($err);
    }
}
```

Every call to error\_log will append the logged content as a string to the global variable. To test calls to error\_log, expose the global and test for strings like this:

```
      public function testError_log()
    {
        global $error_log;
        log_and_error();  // logs "needle"
        $this->assertStringContainsString("needle", $error_log);
    }
```

Local Development
-----------------

[](#local-development)

To have Composer check out a live clone of this repo instead of downloading an archive from Packagist, add a repositories key to the root of your project's **composer.json** file:

```
{
    "repositories": [
    {
      "type": "git",
      "url": "https://github.com/ideasonpurpose/wp-test-stubs"
    }
  ]
}
```

-

[](#)

#### Brought to you by IOP

[](#brought-to-you-by-iop)

[![IOP Logo](https://raw.githubusercontent.com/ideasonpurpose/ideasonpurpose/master/iop-logo-white-on-black-88px.png)](https://www.ideasonpurpose.com)[![](https://raw.githubusercontent.com/ideasonpurpose/ideasonpurpose/master/spacer.png)](https://raw.githubusercontent.com/ideasonpurpose/ideasonpurpose/master/spacer.png) This project is actively developed and used in production at [Ideas On Purpose](https://www.ideasonpurpose.com).

###  Health Score

33

—

LowBetter than 77% of packages

Maintenance52

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 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 ~908 days

Total

2

Last Release

665d ago

### Community

Maintainers

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

---

Top Contributors

[![joemaller](https://avatars.githubusercontent.com/u/8320?v=4)](https://github.com/joemaller "joemaller (92 commits)")

### Embed Badge

![Health badge](/badges/ideasonpurpose-wp-test-stubs/health.svg)

```
[![Health](https://phpackages.com/badges/ideasonpurpose-wp-test-stubs/health.svg)](https://phpackages.com/packages/ideasonpurpose-wp-test-stubs)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k488.3M21.1k](/packages/mockery-mockery)[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k548.7M621](/packages/phpspec-prophecy)[sebastian/global-state

Snapshotting of global state

6.6k848.0M40](/packages/sebastian-global-state)[hamcrest/hamcrest-php

This is the PHP port of Hamcrest Matchers

7.1k475.4M96](/packages/hamcrest-hamcrest-php)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k76.1M6.1k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k113.9M637](/packages/brianium-paratest)

PHPackages © 2026

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