From 04dc41ad488a44c3c341d19f9155085390fe2bf2 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Wed, 5 Jun 2024 17:55:00 +0300 Subject: [PATCH] Making sure every const set is cleared after tests --- tests/wpunit/AssetsTest.php | 62 ++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 4c9bfa2..83f436e 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -3,8 +3,16 @@ namespace StellarWP\Assets; use StellarWP\Assets\Tests\AssetTestCase; +use PHPUnit\Framework\Assert; class AssetsTest extends AssetTestCase { + /** + * Store the value of SCRIPT_DEBUG before modifying it. + * + * @var mixed + */ + protected static $uopz_redefines = []; + public function setUp() { // before parent::setUp(); @@ -19,6 +27,19 @@ public function tearDown() { Config::reset(); } + /** + * @after + */ + public function unset_uopz_redefines() { + if ( function_exists( 'uopz_redefine' ) ) { + foreach ( self::$uopz_redefines as $restore_callback ) { + $restore_callback(); + } + } + + self::$uopz_redefines = []; + } + /** * @test */ @@ -33,11 +54,6 @@ public function it_should_register_multiple_assets() { * @test */ public function it_should_locate_minified_versions_of_external_assets() { - if ( defined( SCRIPT_DEBUG ) ) { - $old_script_debug = SCRIPT_DEBUG; - uopz_undefine( 'SCRIPT_DEBUG' ); - } - Asset::add( 'fake-script', 'fake.js' )->register(); Asset::add( 'fake-style', 'fake.css' )->register(); Asset::add( 'fake2-script', 'fake2.js' )->register(); @@ -59,10 +75,6 @@ public function it_should_locate_minified_versions_of_external_assets() { $this->assert_minified_found( $slug, true, ...$data ); $this->assert_minified_found( $slug, false, ...$data ); } - - if ( isset( $old_script_debug ) ) { - uopz_redefine( 'SCRIPT_DEBUG', $old_script_debug ); - } } /** @@ -391,7 +403,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min $urls = []; - uopz_redefine( 'SCRIPT_DEBUG', false ); + $this->set_const_value( 'SCRIPT_DEBUG', false ); $this->assertFalse( SCRIPT_DEBUG ); @@ -411,7 +423,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min $asset->get_url() ); - uopz_redefine( 'SCRIPT_DEBUG', true ); + $this->set_const_value( 'SCRIPT_DEBUG', true ); $this->assertTrue( SCRIPT_DEBUG ); @@ -425,7 +437,33 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min $urls['1'], $asset->get_url() ); + } - uopz_undefine( 'SCRIPT_DEBUG' ); + /** + * Set a constant value using uopz. + * + * @param string $const + * @param mixed $value + */ + private function set_const_value( $const, $value ) { + if ( ! function_exists( 'uopz_redefine' ) ) { + $this->markTestSkipped( 'uopz extension is not installed' ); + } + + // Normal const redefinition. + $previous_value = defined( $const ) ? constant( $const ) : null; + if ( null === $previous_value ) { + $restore_callback = static function () use ( $const ) { + uopz_undefine( $const ); + Assert::assertFalse( defined( $const ) ); + }; + } else { + $restore_callback = static function () use ( $previous_value, $const ) { + uopz_redefine( $const, $previous_value ); + Assert::assertEquals( $previous_value, constant( $const ) ); + }; + } + uopz_redefine( $const, $value ); + self::$uopz_redefines[] = $restore_callback; } }