stephenharris/phpunit-require-wordpress-version

提供了一组特性,可以在您的PHPUnit测试用例中使用,以根据WordPress版本有条件地运行测试

1.0.0 2016-10-01 18:17 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:32:44 UTC


README

此软件包提供了一组特性(用于测试用例),以便您可以针对特定的WordPress版本运行phpunit测试

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

}

然后在您的测试中

class My_Test extends My_Test_Case {

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

}-

入门

安装

要安装,您需要

  • PHP 5.4+
  • Composer

您可以在项目目录中运行以下命令进行安装

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

或者,您可以直接编辑您的composer.json文件,添加以下内容

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

设置测试用例

使用时,只需将提供的特性中的use语句添加到您的测试用例中

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

}

此特性会覆盖PHPUnit_Framework_TestCase::checkRequirements()方法,如果您已经在测试用例类中覆盖了checkRequirements()方法,则可以给方法起别名

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

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

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

}

如果您正在使用多个具有checkRequirements()方法的特性,则需要使用别名解决冲突

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();
	}

}

示例

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
	}

}

替代语法

您可以使用@requires WordPress加上版本号,不使用运算符

/**
 * @requires WordPress 4.2.0
 */

这意味着测试需要WordPress 4.2.0或更高版本才能运行,否则将被跳过。或者,您可以指定一个运算符。例如

/**
 * @requires WordPress < 4.2.0
 */

这意味着测试需要WordPress版本严格小于4.2.0。

以下是支持的各种运算符。请注意,如果补丁版本号为0,则不需要包含补丁版本号。

  • WordPress大于或等于4.4.0

    • WordPress 4.4
    • WordPress 4.4.0
    • WordPress >= 4.4.0
    • WordPress ge 4.4.0
  • WordPress大于4.4.0

    • WordPress > 4.4
    • WordPress > 4.4.0
    • WordPress gt 4.4.0
  • WordPress等于4.4.0

    • WordPress == 4.4
    • WordPress == 4.4.0
    • WordPress = 4.4.0
    • WordPress eq 4.4.0
  • WordPress不等于4.4.0

    • WordPress != 4.4
    • WordPress != 4.4.0
    • WordPress ne 4.4.0
    • WordPress <> 4.4.0
  • WordPress小于4.4.0

    • WordPress < 4.4
    • WordPress < 4.4.0
    • WordPress lt 4.4.0
  • WordPress小于或等于4.4.0

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

许可证

此软件包是开源的,并按照MIT许可证发布。有关更多信息,请参阅LICENSE文件。

有问题?

请在https://github.com/stephenharris/phpunit-require-wordpress-version/issues上打开一个问题