Git Product home page Git Product logo

phpunit-require-wordpress-version's Introduction

@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

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

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

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 <= 4.2.0
	 */
	function testOnlyRunsFor420AndEarlier() {
		// test will only run for WordPress version 4.2.0 and earlier
	}

}

Alternative syntax

You use @requires WordPress with just a version number, and no operator:

/**
 * @requires WordPress 4.2.0
 */

which means the test requires WordPress 4.2.0 or higher to run, and is otherwise skipped. Or you can specify an operator. E.g:

/**
 * @requires WordPress < 4.2.0
 */

which means the test requires a WordPress version strictly before 4.2.0.

The various operators supported are listed below. Also note, that you do not need to include the patch version number if it is 0.

  • WordPress greater than or equal to 4.4.0

    • WordPress 4.4
    • WordPress 4.4.0
    • WordPress >= 4.4.0
    • WordPress ge 4.4.0
  • WordPress greater than 4.4.0

    • WordPress > 4.4
    • WordPress > 4.4.0
    • WordPress gt 4.4.0
  • WordPress equal to 4.4.0

    • WordPress == 4.4
    • WordPress == 4.4.0
    • WordPress = 4.4.0
    • WordPress eq 4.4.0
  • WordPress not equal to 4.4.0

    • WordPress != 4.4
    • WordPress != 4.4.0
    • WordPress ne 4.4.0
    • WordPress <> 4.4.0
  • WordPress less than 4.4.0

    • WordPress < 4.4
    • WordPress < 4.4.0
    • WordPress lt 4.4.0
  • WordPress less than or equal to 4.4.0

    • WordPress <= 4.4
    • WordPress <= 4.4.0
    • WordPress le 4.4.0

License

This package is open source and released under MIT license. See LICENSE file for more info.

Questions? Problems?

Please open an issue at https://github.com/stephenharris/phpunit-require-wordpress-version/issues

phpunit-require-wordpress-version's People

Contributors

stephenharris avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.