-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect any test cases which don't call the parent setup/teardown method
- Prevent side effects. - Prompt users to fix issues before code reviews.
- Loading branch information
Showing
7 changed files
with
254 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Lipe\WP_Unit\Helpers; | ||
|
||
|
||
/** | ||
* @author Mat Lipe | ||
* @since April 2024 | ||
* | ||
*/ | ||
class SetUpBeforeClassTest extends \WP_UnitTestCase { | ||
public static function set_up_before_class() { | ||
} | ||
|
||
public static function tear_down_after_class() { | ||
set_private_property( Setup_Teardown_State::class, 'setup_before', true ); | ||
parent::tear_down_after_class(); | ||
} | ||
|
||
|
||
public function setUp(): void { | ||
try { | ||
parent::setUp(); | ||
} catch ( \LogicException $e ) { | ||
$this->assertSame( 'Test case did not set up properly. Did you forget to call the `parent::set_up_before_class` or `parent::setUpBeforeClass` method?', $e->getMessage() ); | ||
} finally { | ||
$this->assertInstanceOf( \LogicException::class, $e ); | ||
} | ||
} | ||
|
||
public function test_missing_setup_call(): void { | ||
$this->expectException( \LogicException::class ); | ||
$this->expectExceptionMessage( 'Test case did not set up properly. Did you forget to call the `parent::set_up_before_class` or `parent::setUpBeforeClass` method?' ); | ||
|
||
self::set_up(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Lipe\WP_Unit\Helpers; | ||
|
||
/** | ||
* @author Mat Lipe | ||
* @since April 2024 | ||
* | ||
*/ | ||
class SetUpTest extends \WP_UnitTestCase { | ||
public function set_up() { | ||
// Did not call set_up(). | ||
} | ||
|
||
|
||
public function tear_down() { | ||
try { | ||
parent::tear_down(); | ||
} catch ( \LogicException $e ) { | ||
$this->assertSame( 'Test case did not set up properly. Did you forget to call the `parent::set_up` or `parent::setUp` method?', $e->getMessage() ); | ||
} finally { | ||
$this->assertInstanceOf( \LogicException::class, $e ); | ||
} | ||
|
||
set_private_property( Setup_Teardown_State::class, 'setup', true ); | ||
} | ||
|
||
|
||
public function test_missing_setup_call(): void { | ||
$this->expectException( \LogicException::class ); | ||
$this->expectExceptionMessage( 'Test case did not set up properly. Did you forget to call the `parent::set_up` or `parent::setUp` method?' ); | ||
|
||
parent::tear_down(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Lipe\WP_Unit\Helpers; | ||
|
||
/** | ||
* @author Mat Lipe | ||
* @since April 2024 | ||
* | ||
*/ | ||
class TearDownAfterClassTest extends \WP_UnitTestCase { | ||
public static function tear_down_after_class() { | ||
} | ||
|
||
|
||
public function tear_down(): void { | ||
set_private_property( Setup_Teardown_State::class, 'setup', true ); | ||
parent::tear_down(); | ||
} | ||
|
||
|
||
public function test_missing_tear_down_after_class_call(): void { | ||
$this->expectException( \LogicException::class ); | ||
$this->expectExceptionMessage( 'Lipe\WP_Unit\Helpers\TearDownAfterClassTest did not tear down after class? Did you forget to call the `parent::tearDownAfterClass` or `parent::tear_down_after_class` method?' ); | ||
|
||
try { | ||
self::tear_down_after_class(); | ||
self::set_up_before_class(); | ||
} finally { | ||
set_private_property( Setup_Teardown_State::class, 'tear_down_after_classes', [] ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Lipe\WP_Unit\Helpers; | ||
|
||
/** | ||
* @author Mat Lipe | ||
* @since April 2024 | ||
* | ||
*/ | ||
class TearDownTest extends \WP_UnitTestCase { | ||
public function tear_down() { | ||
// Did not call tear_down(). | ||
} | ||
|
||
|
||
public static function set_up_before_class() { | ||
parent::set_up_before_class(); | ||
set_error_handler( function( $errno, $errstr, $errfile, $errline ) { | ||
throw new \ErrorException( $errstr, $errno, 0, $errfile, $errline ); | ||
} ); | ||
} | ||
|
||
public static function tear_down_after_class() { | ||
try { | ||
parent::tear_down_after_class(); | ||
} catch ( \ErrorException $e ) { | ||
self::assertSame( 'Test case did not tear down properly. Did you forget to call the `parent::tear_down` or `parent::tearDown` method?', $e->getMessage() ); | ||
} finally { | ||
restore_error_handler(); | ||
self::assertInstanceOf( \ErrorException::class, $e ); | ||
} | ||
} | ||
|
||
|
||
public function test_missing_tear_down_call(): void { | ||
$this->expectException( \ErrorException::class ); | ||
$this->expectExceptionMessage( 'Test case did not tear down properly. Did you forget to call the `parent::tear_down` or `parent::tearDown` method?' ); | ||
|
||
parent::tear_down_after_class(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Lipe\WP_Unit\Helpers; | ||
|
||
/** | ||
* Track the state of the setup and tear doan method to | ||
* insure the parent methods are called and state is reset. | ||
* | ||
* | ||
* | ||
* @author Mat Lipe | ||
* @since 3.8.0 | ||
* | ||
*/ | ||
class Setup_Teardown_State { | ||
/** | ||
* @var bool | ||
*/ | ||
protected static $setup_before = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected static $setup = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected static $tear_down = false; | ||
|
||
/** | ||
* @var array<string, bool> | ||
*/ | ||
protected static $tear_down_after_classes = []; | ||
|
||
|
||
public static function set_up_before_class( string $class ): void { | ||
try { | ||
if ( \count( self::$tear_down_after_classes ) > 0 ) { | ||
$classes = \implode( ', ', \array_keys( self::$tear_down_after_classes ) ); | ||
throw new \LogicException( $classes . ' did not tear down after class? Did you forget to call the `parent::tearDownAfterClass` or `parent::tear_down_after_class` method?' ); | ||
} | ||
} finally { | ||
self::reset(); | ||
self::$setup_before = true; | ||
self::$tear_down_after_classes[ $class ] = true; | ||
} | ||
} | ||
|
||
|
||
public static function set_up(): void { | ||
self::$setup = true; | ||
|
||
if ( ! self::$setup_before ) { | ||
throw new \LogicException( 'Test case did not set up properly. Did you forget to call the `parent::set_up_before_class` or `parent::setUpBeforeClass` method?' ); | ||
} | ||
} | ||
|
||
|
||
public static function tear_down(): void { | ||
self::$tear_down = true; | ||
|
||
if ( ! self::$setup ) { | ||
throw new \LogicException( 'Test case did not set up properly. Did you forget to call the `parent::set_up` or `parent::setUp` method?' ); | ||
} | ||
} | ||
|
||
|
||
public static function tear_down_after_class( string $class ): void { | ||
unset( self::$tear_down_after_classes[ $class ] ); | ||
|
||
try { | ||
if ( ! self::$tear_down ) { | ||
trigger_error( 'Test case did not tear down properly. Did you forget to call the `parent::tear_down` or `parent::tearDown` method?', E_USER_ERROR ); | ||
} elseif ( ! self::$setup_before ) { | ||
trigger_error( 'Test case did not set up before properly?. Did you forget to call the `parent::set_up_before_class` or `parent::setUpBeforeClass` method?', E_USER_ERROR ); | ||
} elseif ( ! self::$setup ) { | ||
trigger_error( 'Test case did not set up properly. Did you forget to call the `parent::set_up` or `parent::setUp` method?', E_USER_ERROR ); | ||
} | ||
} finally { | ||
self::reset(); | ||
} | ||
} | ||
|
||
|
||
protected static function reset(): void { | ||
self::$setup_before = false; | ||
self::$setup = false; | ||
self::$tear_down = false; | ||
} | ||
} |