From 4451811d68db3ddc846121ba024b63527dadc848 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Tue, 19 Sep 2023 21:49:27 +0200 Subject: [PATCH] fix(WPTestCase) call _after method correctly --- CHANGELOG.md | 4 +++ src/Codeception/TestCase/WPTestCase.php | 5 ++++ src/tad/WPBrowser/Compat/Compatibility.php | 14 +++++++++ .../wploadersuite/BeforeAfterMethodsTest.php | 30 +++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 tests/wploadersuite/BeforeAfterMethodsTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 74a5f52c7..8fd527e22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] Unreleased +### Fixed + +- Ensure `_before` and `_after` methods are called correctly by `WPTestCase`. + ## [3.2.0] 2023-09-15; ### Breaking change diff --git a/src/Codeception/TestCase/WPTestCase.php b/src/Codeception/TestCase/WPTestCase.php index c64cba188..54e6397cf 100644 --- a/src/Codeception/TestCase/WPTestCase.php +++ b/src/Codeception/TestCase/WPTestCase.php @@ -512,6 +512,11 @@ public function _tearDown() $this->_restore_hooks(); wp_set_current_user(0); $this->requestTimeTearDown(); + + $unitTearDownMethod = Compatibility::tearDownMethodFor(Unit::class); + if (method_exists(Unit::class, $unitTearDownMethod)) { + Unit::{$unitTearDownMethod}(); + } } /** diff --git a/src/tad/WPBrowser/Compat/Compatibility.php b/src/tad/WPBrowser/Compat/Compatibility.php index e00456529..a59683aa5 100644 --- a/src/tad/WPBrowser/Compat/Compatibility.php +++ b/src/tad/WPBrowser/Compat/Compatibility.php @@ -32,6 +32,20 @@ public static function setupMethodFor($class) return method_exists($class, '_setUp') ? '_setUp' : 'setUp'; } + /** + * Returns the first existing tearDown method for a base test case class. + * + * @since TBD + * + * @param string $class The fully-qualified name of the class to return the tear down method for. + * + * @return string The class tear down method name; default to the PHPUnit default `tearDown` if not found. + */ + public static function tearDownMethodFor($class) + { + return method_exists($class, '_tearDown') ? '_tearDown' : 'tearDown'; + } + /** * Returns the PHPUnit version currently installed. * diff --git a/tests/wploadersuite/BeforeAfterMethodsTest.php b/tests/wploadersuite/BeforeAfterMethodsTest.php new file mode 100644 index 000000000..27d1b5a58 --- /dev/null +++ b/tests/wploadersuite/BeforeAfterMethodsTest.php @@ -0,0 +1,30 @@ +canary = true; + } + + public function _after() + { + self::$staticCanary = false; + } + + public static function tearDownAfterClass() + { + parent::tearDownAfterClass(); + Assert::assertFalse(self::$staticCanary); + } + + public function test_before_method_is_loaded() + { + $this->assertTrue($this->canary); + } +}