PHPackages                             stephenharris/phpunit-require-wordpress-version - 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. stephenharris/phpunit-require-wordpress-version

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

stephenharris/phpunit-require-wordpress-version
===============================================

Provides a trait to be used in your PHPUnit test cases to conditionally run tests based on the WordPress version

1.0.0(9y ago)110MITPHPPHP &gt;=5.4.0

Since Oct 1Pushed 9y ago1 watchersCompare

[ Source](https://github.com/stephenharris/phpunit-require-wordpress-version)[ Packagist](https://packagist.org/packages/stephenharris/phpunit-require-wordpress-version)[ RSS](/packages/stephenharris-phpunit-require-wordpress-version/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

@requires WordPress
===================

[](#requires-wordpress)

This package provides a trait (to use in your test cases) so that you can run your phpunit tests for specific WordPress versions:

```
class My_Test_Case extends WP_UnitTestCase {
	use StephenHarris\PHPUnit\RequiresWordPressVersion;

}
```

Then in your tests:

```
class My_Test extends My_Test_Case {

	/**
	 * @requires WordPress 4.4.0
	 */
	 function testSomethingThatRequiresTermMeta() {
		 	// test will be skipped unless WordPress >= 4.4.0
	 }

}-
```

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

[](#getting-started)

### Installation

[](#installation)

To install you need:

- PHP 5.4+
- Composer

You can install by running following command in your project folder:

```
composer require stephenharris/phpunit-require-wordpress-version:1.* --dev

```

Alternativevely you can directly edit your composer.json by adding:

```
{
  "require-dev": {
    "stephenharris/phpunit-require-wordpress-version": "~1.0"
  }
}

```

### Setting up your test cases

[](#setting-up-your-test-cases)

To use, simply add the `use` statement for the provided trait to your test case:

```
class My_Test_Case extends WP_UnitTestCase {
	use StephenHarris\PHPUnit\RequiresWordPressVersion;

}
```

This trait overloads the `PHPUnit_Framework_TestCase::checkRequirements()`, if you already overloading `checkRequirements()` in your test case class then you can alias the method:

```
class My_Test_Case extends WP_UnitTestCase {
	use StephenHarris\PHPUnit\RequiresWordPressVersion {
		checkRequirements as checkWordPressVersionRequirements;
	}

	function checkRequirements() {
		$this->checkWordPressVersionRequirements();

		//... your checkRequirements() method here
	}

}
```

If you are using multiple traits with the `checkRequirements()` method, then you will need to resolve the conflicts using aliases:

```
class My_Test_Case extends WP_UnitTestCase {
	use StephenHarris\PHPUnit\RequiresWordPressVersion {
		checkRequirements as checkWordPressVersionRequirements;
	}
	use Some\Other\checkRequirementsTrait {
		checkRequirements as checkSomeOtherRequirements;
	}

	function checkRequirements() {
		$this->checkWordPressVersionRequirements();
		$this->checkSomeOtherRequirements();
	}

}
```

Examples
--------

[](#examples)

```
class My_Test extends My_Test_Case {

	/**
	 * @requires WordPress 4.4.0
	 */
	 function testSomethingRequiresAtLeast440() {
		 	// test will be skipped unless WordPress >= 4.4.0
	 }

	/**
	 * @requires WordPress >= 4.4.0-alpha-123
	 */
	 function testSomethingRequiresAtLeast440alpha123() {
		 	// test will be skipped unless WordPress >= 4.4.0-alpha-123
		 	// also works if you specify 4.4-alpha-123 instead
	 }

	/**
	 * @requires WordPress > 4.6.2-rc-1
	 */
	 function testSomethingRequiresGreaterThan462ReleaseCandidate() {
	 	 // test will be skipped unless WordPress version is greater than 4.6.2-rc-1
	 }

	/**
	 * @requires WordPress == 4.6.0
	 */
	 function testOnlyRunsForWordPress460() {
	 	 // test will only run with version 4.6.0
	 }

	/**
	 * @requires WordPress != 4.6
	 */
	function testSkippedIf460() {
		// test will be skipped if WordPress version is 4.6.0
	}

	/**
	 * @requires WordPress < 4.2-alpha-1234
	 */
	function testOnlyRunsForVersionsBefore42Alpha1234() {
		// test will only run for WordPress versions strictly less than4.2-alpha-1234
	}

	/**
	 * @requires WordPress
