Make WordPress Core


Ignore:
Timestamp:
09/24/2021 02:20:53 PM (3 years ago)
Author:
hellofromTonya
Message:

Build/Test Tools: Fix test forward-compatibility layer.

In [51844], the test wrapper methods were not being called due to the names not being recognized as supported PHPUnit "hook" names for fixtures.

This commit:

  • Fixes the problem by adding extra camelCase wrappers to the WP_UnitTestCase to call the methods in the right order.
  • Adds wrappers for the assertPreConditions() and assertPostConditions() fixture methods to make the backport feature complete for the fixture wrappers.

Test wrapper methods call fix:

By adding method overloads for the PHPUnit native camelCase fixture methods and letting those call the (camelCase) parent method first and only calling the snake_case fixture methods after, the snake_case methods can be supported and the typical run order safeguarded.

As not all test classes will have declared snake_case fixture methods, the snake_case fixture methods are also declared in the WP_UnitTestCase. Why? This prevents having to wrap these method calls in method_exists() conditions checking for the existence of the snake_case methods in an unknown Test child class. And with the normal inheritance rules in combination with calling the method using static, the right method will be called anyway without fatal "calling undeclared method" errors.

Note: While it will be rare, there may be cases where a test class does not adhere to the normal execution order for fixtures, i.e. for the setup methods, parent first, own code second; and for the teardown methods, own code first, parent second. For example a test class which has "some code - parent::setUp() call - some more code" in their setUp() method. In those (rare) cases, the execution order of the code will now be changed, which may have side-effects. This rare case will be identified in the dev note.

Follow-up to [51844].

Props bjorsch, swissspidy, jrf, hellofromTonya.
See #53911.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.4/tests/phpunit/includes/phpunit7/testcase.php

    r51844 r51865  
    4747
    4848    /**
    49      * Wrapper method for the `setUpBeforeClass()` method for forward-compatibility with WP 5.9.
     49     * Wrapper method for the `set_up_before_class()` method for forward-compatibility with WP 5.9.
    5050     */
    51     public static function set_up_before_class() {
    52         static::setUpBeforeClass();
     51    public static function setUpBeforeClass() {
     52        parent::setUpBeforeClass();
     53        static::set_up_before_class();
    5354    }
    5455
    5556    /**
    56      * Wrapper method for the `tearDownAfterClass()` method for forward-compatibility with WP 5.9.
     57     * Wrapper method for the `tear_down_after_class()` method for forward-compatibility with WP 5.9.
    5758     */
    58     public static function tear_down_after_class() {
    59         static::tearDownAfterClass();
     59    public static function tearDownAfterClass() {
     60        static::tear_down_after_class();
     61        parent::tearDownAfterClass();
    6062    }
    6163
    6264    /**
    63      * Wrapper method for the `setUp()` method for forward-compatibility with WP 5.9.
     65     * Wrapper method for the `set_up()` method for forward-compatibility with WP 5.9.
    6466     */
    65     public function set_up() {
    66         static::setUp();
     67    public function setUp() {
     68        parent::setUp();
     69        $this->set_up();
    6770    }
    6871
    6972    /**
    70      * Wrapper method for the `tearDown()` method for forward-compatibility with WP 5.9.
     73     * Wrapper method for the `tear_down()` method for forward-compatibility with WP 5.9.
    7174     */
    72     public function tear_down() {
    73         static::tearDown();
     75    public function tearDown() {
     76        $this->tear_down();
     77        parent::tearDown();
    7478    }
     79
     80    /**
     81     * Wrapper method for the `assert_pre_conditions()` method for forward-compatibility with WP 5.9.
     82     */
     83    protected function assertPreConditions() {
     84        parent::assertPreConditions();
     85        $this->assert_pre_conditions();
     86    }
     87
     88    /**
     89     * Wrapper method for the `assert_post_conditions()` method for forward-compatibility with WP 5.9.
     90     */
     91    protected function assertPostConditions() {
     92        parent::assertPostConditions();
     93        $this->assert_post_conditions();
     94    }
     95
     96    /**
     97     * Placeholder method for forward-compatibility with WP 5.9.
     98     */
     99    public static function set_up_before_class() {}
     100
     101    /**
     102     * Placeholder method for forward-compatibility with WP 5.9.
     103     */
     104    public static function tear_down_after_class() {}
     105
     106    /**
     107     * Placeholder method for forward-compatibility with WP 5.9.
     108     */
     109    protected function set_up() {}
     110
     111    /**
     112     * Placeholder method for forward-compatibility with WP 5.9.
     113     */
     114    protected function tear_down() {}
     115
     116    /**
     117     * Placeholder method for forward-compatibility with WP 5.9.
     118     */
     119    protected function assert_pre_conditions() {}
     120
     121    /**
     122     * Placeholder method for forward-compatibility with WP 5.9.
     123     */
     124    protected function assert_post_conditions() {}
    75125}
Note: See TracChangeset for help on using the changeset viewer.