PHPackages                             otgs/unit-tests-framework - 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. otgs/unit-tests-framework

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

otgs/unit-tests-framework
=========================

A framework to build unit tests with OTGS products

5.0.0(5mo ago)1482.9k—1.3%7GPL-2.0PHPCI failing

Since Aug 4Pushed 5mo ago19 watchersCompare

[ Source](https://github.com/OnTheGoSystems/otgs-unit-tests-framework)[ Packagist](https://packagist.org/packages/otgs/unit-tests-framework)[ Docs](https://git.onthegosystems.com/shared/otgs-unit-tests-framework)[ RSS](/packages/otgs-unit-tests-framework/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (6)Dependencies (7)Versions (55)Used By (7)

PhpUnit
=======

[](#phpunit)

- Create a `tests` directory in your project
- Create a `phpunit` subdirectory in `tests`[1](#user-content-fn-1-f001e40bd0a4477967e9c54777cc7ac5)
- Copy `samples/phpunit.xml` file in the root of your project[2](#user-content-fn-2-f001e40bd0a4477967e9c54777cc7ac5)
    - Change `test-suite-name` into something appropriated
    - Most likely, you won't need to change anything else in that file
- Copy `samples/bootstrap.php` into `tests/phpunit`
    - Read the comments in this file and make the appropriated changes
- Run `composer require --dev otgs/unit-tests-framework`[3](#user-content-fn-3-f001e40bd0a4477967e9c54777cc7ac5)
- Create another `tests` subdirectory in `tests/phpunit`
    - Write your tests there
    - Unless you have customized the `phpunit.xml` file, you don't need to name the test files and classes in any particular way
- Run `phpunit` from your project's root to start your tests[4](#user-content-fn-4-f001e40bd0a4477967e9c54777cc7ac5)

How to use the OTGS\_TestCase class
-----------------------------------

[](#how-to-use-the-otgs_testcase-class)

In order to take the most of this library, all your tests classes should extext `OTGS_TestCase`.

Once you do that, this will happen:

1. `\OTGS_TestCase::setupBeforeClass`:

- `$_GET` and `$_POST` are set to an empty array
- An instance of `FactoryMuffin` is provided: you can refer to it as `self::$fm` (see "Resources and dependencies" for more details)

2. `\OTGS_TestCase::setUp`

- `FunctionMocker` is initialized (see "Resources and dependencies" for more details)
- `WP_Mock` is initialized (see "Resources and dependencies" for more details)

3. `\OTGS_TestCase::tearDown`

- `WP_Mock` is destroyed
- `FunctionMocker ` is destroyed
- `Mockery` is destroyed (just in case it has been used)

4. `\OTGS_TestCase::tearDownAfterClass`

- Deletes all models created with `FactoryMuffin`

### Mock WP Core functions

[](#mock-wp-core-functions)

This class also provide an helper method to quickly mock the functions defined by WordPress by using the `\OTGS_TestCase::get_mocked_wp_core_functions` which returns an instance of `OTGS_Mocked_WP_Core_Functions`.

`OTGS_Mocked_WP_Core_Functions` organize mocks in methods named using the same name of the file where the function is defined in WordPress codebase.

For instance, to mock of all functions defined in `post.php` like `get_post`, in your test you should simply call `$this->get_mocked_wp_core_functions()->post()`.

To mock `add_query_arg` yo call `$this->get_mocked_wp_core_functions()->functions()` because `add_query_arg` is defined in `functions.php`.

`OTGS_Mocked_WP_Core_Functions` tries to handle dependencies.

So, if you call `$this->get_mocked_wp_core_functions()->post()` to mock `wp_insert_post`, you automatically call `$this->get_mocked_wp_core_functions()->post()`, so to get all the meta related functions mocked as well.
Finally, there is a "mock all" method you could use (though is discouraged) with `$this->mock_all_core_functions()`.

### Stub WP common classes

[](#stub-wp-common-classes)

`\OTGS_TestCase` provides a helpful way to quickly get a stub of some of the most commonly used classes in WordPress.

By calling `$this->stubs->wpdb()` you will get a stub you can pass as a dependency of the classes you are testing. If you need to control the behavior of this stub, you just use the standard PHPUnit mock helpers.

E.g. 1:

```
$wpdb = $this->stubs->wpdb();
$wpdb->method( 'get_var' )->willReturn( 1 );
```

E.g. 2:

```
$results = array(
	array( 'translation_id' => 1, 'element_id' => 1, 'language_code' => 'en', 'source_language_code' => null, 'trid' => 1, 'element_type' => 'post_page' ),
	array( 'translation_id' => 2, 'element_id' => 2, 'language_code' => 'fr', 'source_language_code' => 'en', 'trid' => 1, 'element_type' => 'post_page' ),
);
$wpdb = $this->this->wpdb();
$wpdb->expects( $this->exactly( 2 ) )->method( 'get_results' )->willReturn( $results );
```

Other stubs you can get:

- `WP_Widget` with `$this->stubs->WP_Widget()`
- `WP_Theme` with `$this->stubs->WP_Theme()`
- `WP_Filesystem_Direct` with `$this->stubs->WP_Filesystem_Direct()`
- `WP_Query` with `$this->stubs->WP_Query()`

It is important to know that, if you only need the class to be defined (e.g. hard-dependency, or sub-classing), you don't need to assig the stub to a variable: just call the method.

A good example is with WordPress' widgets, where you may have your own widget which is supposed to extend `WP_Widget`.

In this case, unless you want to mock some of the `WP_Widget` methods, you simply call `$this->stubs->WP_Widget()`, then write your tests.
The class which extends `WP_Widget` will find a definition of this class, with all the methods (doing nothing).

Resources and dependencies
--------------------------

[](#resources-and-dependencies)

Below are some resources on writing unit tests which lead to the creation of this library and links to the libraries included here:

- Start from here for a general explanation:
- 10up's WP\_Mock`: [https://github.com/10up/wp\_mock](https://github.com/10up/wp_mock)
- Mockery:
- Function mocker:
- Factory Muffin:
- Factory Muffin Faker:
- The DomCrawler Component: [http://symfony.com/doc/current/components/dom\_crawler.html](http://symfony.com/doc/current/components/dom_crawler.html)
- The CssSelector Component: [http://symfony.com/doc/current/components/css\_selector.html](http://symfony.com/doc/current/components/css_selector.html)
- php-loremipsum:

Footnotes
---------

1. it is recommended to keep your PHPUnit tests into a subfolder, because there are good changes you want to add other kind of tests (e.g. QUnit, PHPSpec, etc.) [↩](#user-content-fnref-1-f001e40bd0a4477967e9c54777cc7ac5)
2. in case, for whatever reason, you can't do that, copy this file in `tests/phpunit` or wherever you find it more convenient [↩](#user-content-fnref-2-f001e40bd0a4477967e9c54777cc7ac5)
3. In some cases, you may want to use `composer require --dev otgs/unit-tests-framework:dev-develop`. When you do that, you may also need to run this command first `composer config minimum-stability dev` [↩](#user-content-fnref-3-f001e40bd0a4477967e9c54777cc7ac5)
4. If you've placed the `phpunit.xml` you need to either move to that directory first, or tell phpunit where the file is with `phpunit --configuration path/to/phpunit.xml` [↩](#user-content-fnref-4-f001e40bd0a4477967e9c54777cc7ac5)

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance73

Regular maintenance activity

Popularity35

Limited adoption so far

Community26

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 59.6% 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 ~166 days

Recently: every ~240 days

Total

21

Last Release

152d ago

Major Versions

1.2.8 → 2.0.02019-12-17

2.0.0 → 3.0.02023-05-02

3.0.0 → 4.0.02024-12-13

4.1.1 → 5.0.02025-12-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/9b2504f162bfb82c278563083bbc6e1ebe23f79e41099096a17e6879f3f7183a?d=identicon)[andreasciamanna](/maintainers/andreasciamanna)

![](https://www.gravatar.com/avatar/38f6b9ea4ac5094cb4d6a7eda9e3acd33ae87f5a89977def2957d5c9d31a6722?d=identicon)[otgs-libraries](/maintainers/otgs-libraries)

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

![](https://www.gravatar.com/avatar/76d7a340b0b4d3ddf4c80241a7aebe9b98d6bb2b8f26cb6540008308f80ebde3?d=identicon)[brucepearson](/maintainers/brucepearson)

---

Top Contributors

[![andreasciamanna](https://avatars.githubusercontent.com/u/181780?v=4)](https://github.com/andreasciamanna "andreasciamanna (31 commits)")[![Igor-WPML](https://avatars.githubusercontent.com/u/43881443?v=4)](https://github.com/Igor-WPML "Igor-WPML (10 commits)")[![strategio](https://avatars.githubusercontent.com/u/1941656?v=4)](https://github.com/strategio "strategio (9 commits)")[![BruceOnTheGo](https://avatars.githubusercontent.com/u/10436459?v=4)](https://github.com/BruceOnTheGo "BruceOnTheGo (1 commits)")[![ivanmaricic89](https://avatars.githubusercontent.com/u/128781279?v=4)](https://github.com/ivanmaricic89 "ivanmaricic89 (1 commits)")

---

Tags

phpunit

### Embed Badge

![Health badge](/badges/otgs-unit-tests-framework/health.svg)

```
[![Health](https://phpackages.com/badges/otgs-unit-tests-framework/health.svg)](https://phpackages.com/packages/otgs-unit-tests-framework)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[drupal/core-dev

require-dev dependencies from drupal/drupal; use in addition to drupal/core-recommended to run tests from drupal/core.

2021.0M277](/packages/drupal-core-dev)[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[juampi92/test-seo

Easy way to test your SEO

26341.0k](/packages/juampi92-test-seo)[robiningelbrecht/phpunit-pretty-print

Prettify PHPUnit output

76460.0k15](/packages/robiningelbrecht-phpunit-pretty-print)[phpunit/phpunit-dom-assertions

DOM assertions for PHPUnit

29343.5k11](/packages/phpunit-phpunit-dom-assertions)

PHPackages © 2026

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