Skip to content

Commit

Permalink
fix(WPTestCase) call _after method correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Sep 19, 2023
1 parent 0049ff9 commit 4451811
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Codeception/TestCase/WPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}();
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/tad/WPBrowser/Compat/Compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/wploadersuite/BeforeAfterMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use PHPUnit\Framework\Assert;

class BeforeAfterMethodsTest extends \Codeception\TestCase\WPTestCase
{
private static $staticCanary = true;
private $canary = false;

public function _before()
{
$this->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);
}
}

0 comments on commit 4451811

Please sign in to comment.