Skip to content

Commit

Permalink
Making sure every const set is cleared after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Jun 5, 2024
1 parent 9dd6f86 commit 04dc41a
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
*/
Expand All @@ -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();
Expand All @@ -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 );
}
}

/**
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );

Expand All @@ -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;
}
}

0 comments on commit 04dc41a

Please sign in to comment.