Build/Test Tools: Use the PHPUnit Polyfill TestCase as void workaround.

> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit:
* Lets the `PHPUnit_Adapter_TestCase` extend the `Yoast\PHPUnitPolyfills\TestCases\TestCase`, which makes this solution for the `void` return type available to the WordPress test suite.
* Removes the individual import and trait `use` statements for the Polyfill traits. These are no longer necessary as the `Yoast\PHPUnitPolyfills\TestCases\TestCase` already includes those.

Follow-up to [51559-51566].

Props jrf, hellofromTonya, johnbillion, netweb, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51567 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2021-08-07 10:20:05 +00:00
parent ee2770bda5
commit cb6bf02638
2 changed files with 10 additions and 34 deletions

View File

@ -235,6 +235,7 @@
<properties>
<property name="custom_test_class_whitelist" type="array">
<!-- Test case parent classes. -->
<element value="PHPUnit_Adapter_TestCase"/>
<element value="WP_UnitTestCase"/>
<element value="WP_Ajax_UnitTestCase"/>
<element value="WP_Canonical_UnitTestCase"/>
@ -267,6 +268,7 @@
<element value="WPProfiler"/>
<element value="SpeedTrapListener"/>
<element value="PHPUnit_Framework_Exception"/>
<element value="Polyfill_TestCase"/>
</property>
</properties>
</rule>

View File

@ -1,20 +1,6 @@
<?php
use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
use Yoast\PHPUnitPolyfills\TestCases\TestCase as Polyfill_TestCase;
/**
* PHPUnit adapter layer.
@ -22,24 +8,12 @@ use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
* This class enhances the PHPUnit native `TestCase` with polyfills
* for assertions and expectation methods added between PHPUnit 4.8 - 9.5.
*
* Additionally, the Polyfill TestCase offers a workaround for the addition
* of the `void` return type to PHPUnit fixture methods by providing
* overloadable snake_case versions of the typical fixture method names and
* ensuring that PHPUnit handles those correctly.
*
* See {@link https://github.com/Yoast/PHPUnit-Polyfills} for full
* documentation on the available polyfills.
* documentation on the available polyfills and other features.
*/
abstract class PHPUnit_Adapter_TestCase extends PHPUnit\Framework\TestCase {
use AssertAttributeHelper;
use AssertClosedResource;
use AssertEqualsSpecializations;
use AssertFileDirectory;
use AssertFileEqualsSpecializations;
use AssertionRenames;
use AssertIsType;
use AssertNumericType;
use AssertObjectEquals;
use AssertStringContains;
use EqualToSpecializations;
use ExpectException;
use ExpectExceptionMessageMatches;
use ExpectExceptionObject;
use ExpectPHPException;
}
abstract class PHPUnit_Adapter_TestCase extends Polyfill_TestCase {}