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(2y ago)0274[2 issues](https://github.com/ideasonpurpose/wp-test-stubs/issues)5MITPHPPHP &gt;=8.0.0

Since Nov 30Pushed 9mo 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 3w ago

READMEChangelogDependenciesVersions (3)Used By (5)

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

29

—

LowBetter than 57% of packages

Maintenance41

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity46

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

757d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8320?v=4)[Joe Maller](/maintainers/joemaller)[@joemaller](https://github.com/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

[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14228.7M340](/packages/dms-phpunit-arraysubset-asserts)[orchestra/workbench

Workbench Companion for Laravel Packages Development

8219.1M69](/packages/orchestra-workbench)[phpbenchmark/phpbenchmark

Easy to use benchmark toolkit for your PHP-application. This library contains classes for comparing algorithms as well as benchmarking application responses

8011.5k2](/packages/phpbenchmark-phpbenchmark)[kunicmarko/graphql-test

GraphQL Test Cases

1359.0k](/packages/kunicmarko-graphql-test)

PHPackages © 2026

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