PHPackages                             badasswp/wp-mock-tc - 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. badasswp/wp-mock-tc

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

badasswp/wp-mock-tc
===================

WP Mock Test Case Library

v1.1.0(4mo ago)41.9k↑56.1%MITPHPCI passing

Since Sep 25Pushed 4mo agoCompare

[ Source](https://github.com/badasswp/wp-mock-tc)[ Packagist](https://packagist.org/packages/badasswp/wp-mock-tc)[ RSS](/packages/badasswp-wp-mock-tc/feed)WikiDiscussions master Synced yesterday

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

WPMockTestCase
==============

[](#wpmocktestcase)

**WPMockTestCase** provides a **solid base test case class** that centralizes common WordPress mocks so you don’t have to repeat them in every test.

Getting Started
---------------

[](#getting-started)

Install directly from **Packagist** using Composer like so:

```
composer require badasswp/wp-mock-tc
```

Setup &amp; TearDown Methods
----------------------------

[](#setup--teardown-methods)

Load `parent` methods correctly by running `setUp` and `tearDown` methods like so:

```
use Badasswp\WPMockTC\WPMockTestCase;

class SampleTest extends WPMockTestCase {
    public function setUp(): void {
        parent::setUp();
    }

    public function tearDown(): void {
        parent::tearDown();
    }
}
```

Demo Sample
-----------

[](#demo-sample)

The example below shows a class called `SampleClass` which contains a public method that appends the list of names to the `post_id` param.

We can simply write our test case and allow **WPMockTestCase** take care of the mock definitions under the hood like so:

```
use Badasswp\WPMockTC\WPMockTestCase;

class SampleClass {
    public function get_user_names_appended_to_post_id( $post_id ) {
        // Safely type-cast Post ID.
        $id = absint( $post_id );

        // Get list of users.
        $users = [ 'John Doe', 'Cathryn Washington', 'Jack Foley' ];

        // Get user names appended to Post ID.
        return array_map(
            function( $arg ) use( $id ) {
                return sprintf( '%s - %d', esc_html( $arg ), $id );
            },
            $users
        );
    }
}

class SampleTest extends WPMockTestCase {
    private $sample_class;

    public function setUp(): void {
        parent::setUp();
        $this->sample_class = new SampleClass();
    }

    public function tearDown(): void {
        parent::tearDown();
    }

    public function test_get_user_names_appended_to_post_id() {
        $this->assertSame(
            [
                'John Doe - 1',
                'Cathryn Washington - 1',
                'Jack Foley - 1'
            ],
            $this->sample_class->get_user_names_appended_to_post_id( 1 )
        );
    }
}
```

Notice that in our test, we are not mocking the WP functions `absint` and `esc_html`, **WPMockTestCase** handles that for us under the hood, so we have one less thing to worry about.

Override Mocks
--------------

[](#override-mocks)

You can override any of the existing `WPMockTestCase` mocks by simply running the `parent::override` method like so:

```
use Badasswp\WPMockTC\WPMockTestCase;

class OverrideTest extends WPMockTestCase {
    public function setUp(): void {
        parent::override( [ 'absint' ] );
        parent::setUp();
    }

    public function tearDown(): void {
        parent::tearDown();
    }

    public function test_get_user_names_appended_to_post_id() {
        // Now you can define your custom mock.
        WP_Mock::userFunction( 'absint' )
            ->andReturn( 1 );

        $this->assertSame(
            [
                'John Doe - 1',
                'Cathryn Washington - 1',
                'Jack Foley - 1'
            ],
            $this->sample_class->get_user_names_appended_to_post_id( 1 )
        );
    }
}
```

Caveat
------

[](#caveat)

At the moment, **WPMockTestCase** DOES NOT mock all of WP functions. So you may have to do some of your own mocking in your test cases.

Here are a list of mocked functions in WPMockTestCase:

- `__`
- `_e`
- `_x`
- `_n`
- `esc_html`
- `esc_attr`
- `esc_url`
- `esc_html__`
- `esc_attr__`
- `esc_html_e`
- `absint`
- `is_wp_error`
- `wp_json_encode`
- `wp_parse_args`
- `wp_strip_all_tags`

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance74

Regular maintenance activity

Popularity25

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

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

Total

2

Last Release

144d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c9b2b05af7ad2bce065ec66a489bc9f3d0e481e281a2f1482e0bf66ea0eae65?d=identicon)[badasswp](/maintainers/badasswp)

---

Top Contributors

[![badasswp](https://avatars.githubusercontent.com/u/149586343?v=4)](https://github.com/badasswp "badasswp (35 commits)")

---

Tags

librarymockunit-testingwordpresswp-mock

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/badasswp-wp-mock-tc/health.svg)

```
[![Health](https://phpackages.com/badges/badasswp-wp-mock-tc/health.svg)](https://phpackages.com/packages/badasswp-wp-mock-tc)
```

###  Alternatives

[dms/phpunit-arraysubset-asserts

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

14429.2M360](/packages/dms-phpunit-arraysubset-asserts)

PHPackages © 2026

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