Skip to content

Commit

Permalink
Refactor the tests set_up
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz authored and sirreal committed Jan 2, 2024
1 parent b48d811 commit a4a1828
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions tests/phpunit/tests/modules/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,13 @@
* @covers WP_Modules::print_module_preloads
*/
class Tests_Modules_Functions extends WP_UnitTestCase {
/**
* Stores a reference to the ReflectionProperty instance of the
* WP_Modules::$registered property.
*
* @var ReflectionProperty
*/
protected $registered;

/**
* Stores the original value of WP_Modules::$registered to restore it later.
*
* @var array
*/
protected $old_registered;

/**
* Stores a reference to the ReflectionProperty instance of the
* WP_Modules::$enqueued_before_registered property.
*
* @var ReflectionProperty
*/
protected $enqueued_before_registered;

/**
* Stores the original value of WP_Modules::$enqueued_before_registered to
* restore it later.
Expand All @@ -43,20 +27,25 @@ class Tests_Modules_Functions extends WP_UnitTestCase {
public function set_up() {
parent::set_up();

$wp_modules = new ReflectionClass( 'WP_Modules' );

$this->old_registered = $wp_modules->getStaticPropertyValue( 'registered' );
$this->old_enqueued_before_registered = $wp_modules->getStaticPropertyValue( 'enqueued_before_registered' );
$registered = new ReflectionProperty( 'WP_Modules', 'registered' );
$registered->setAccessible( true );
$this->old_registered = $registered->getValue();
$registered->setValue( null, array() );

$wp_modules->setStaticPropertyValue( 'registered', array() );
$wp_modules->setStaticPropertyValue( 'enqueued_before_registered', array() );
$enqueued_before_registered = new ReflectionProperty( 'WP_Modules', 'enqueued_before_registered' );
$enqueued_before_registered->setAccessible( true );
$this->old_enqueued_before_registered = $enqueued_before_registered->getValue();
$enqueued_before_registered->setValue( null, array() );
}

public function tear_down() {
$wp_modules = new ReflectionClass( 'WP_Modules' );
$registered = new ReflectionProperty( 'WP_Modules', 'registered' );
$registered->setAccessible( true );
$registered->setValue( null, $this->old_registered );

$wp_modules->setStaticPropertyValue( 'registered', $this->old_registered );
$wp_modules->setStaticPropertyValue( 'enqueued_before_registered', $this->old_enqueued_before_registered );
$enqueued_before_registered = new ReflectionProperty( 'WP_Modules', 'enqueued_before_registered' );
$enqueued_before_registered->setAccessible( true );
$enqueued_before_registered->setValue( null, $this->old_enqueued_before_registered );

parent::tear_down();
}
Expand Down

0 comments on commit a4a1828

Please sign in to comment.