Move code hacker resetting from BeforeTestHook to setUp

The code hacker needs to be reset before each test. This was done via
a couple of classes implementeing BeforeTestHook, those were registered
in phpunit.xml.

The problem is that the PHPUnit version used for WooCommerce unit test
has recently been changed from 7.5 to 6.5 for compatibility with
PHP 7.0, and hook classes were introduced in PHPUnit 7. Thus no hooks
were ran, the code hacker wasn't reset, that caused some functions
to remain hacked between tests, and this made some tests to fail.

The solution is to move the code hacker reset to the setUp method
in the base unit test class.
This commit is contained in:
Nestor Soriano 2021-02-01 16:21:31 +01:00
parent 7440e95b72
commit 8a60e7e147
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
4 changed files with 6 additions and 69 deletions

View File

@ -49,8 +49,4 @@
<listeners>
<listener class="SpeedTrapListener" file="tests/legacy/includes/listener-loader.php" />
</listeners>
<extensions>
<extension class="\Automattic\WooCommerce\Testing\Tools\CodeHacking\CodeHackerTestHook" />
<extension class="\Automattic\WooCommerce\Testing\Tools\DependencyManagement\DependencyManagementTestHook" />
</extensions>
</phpunit>

View File

@ -1,32 +0,0 @@
<?php
/**
* CodeHackerTestHook class file.
*
* @package WooCommerce\Testing
*/
namespace Automattic\WooCommerce\Testing\Tools\CodeHacking;
use PHPUnit\Runner\BeforeTestHook;
/**
* Helper to use the CodeHacker class in PHPUnit. To use, add this to phpunit.xml:
*
* <extensions>
* <extension class="CodeHackerTestHook" />
* </extensions>
*/
final class CodeHackerTestHook implements BeforeTestHook {
/**
* Runs before each test.
*
* @param string $test "TestClass::TestMethod".
*
* @throws \ReflectionException Thrown by execute_before_methods.
*/
public function executeBeforeTest( string $test ): void {
CodeHacker::reset_hacks();
}
}

View File

@ -1,33 +0,0 @@
<?php
/**
* DependencyManagementTestHook class file.
*
* @package Automattic\WooCommerce\Testing\Tools\DependencyManagement
*/
namespace Automattic\WooCommerce\Testing\Tools\DependencyManagement;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use PHPUnit\Runner\BeforeTestHook;
/**
* Hook to perform dependency management related setup in PHPUnit. To use, add this to phpunit.xml:
*
* <extensions>
* <extension class="DependencyManagementTestHook" />
* </extensions>
*/
final class DependencyManagementTestHook implements BeforeTestHook {
/**
* Runs before each test.
*
* @param string $test "TestClass::TestMethod".
*/
public function executeBeforeTest( string $test ): void {
// Reset the instance of MockableLegacyProxy that was registered during bootstrap,
// in order to start the test in a clean state (without anything mocked).
wc_get_container()->get( LegacyProxy::class )->reset();
}
}

View File

@ -76,6 +76,12 @@ class WC_Unit_Test_Case extends WP_HTTP_TestCase {
// Register post types before each test.
WC_Post_types::register_post_types();
WC_Post_types::register_taxonomies();
CodeHacker::reset_hacks();
// Reset the instance of MockableLegacyProxy that was registered during bootstrap,
// in order to start the test in a clean state (without anything mocked).
wc_get_container()->get( LegacyProxy::class )->reset();
}
/**