Skip to content

Commit

Permalink
Restore original registry instance after test is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Mar 28, 2022
1 parent bbe130c commit e8c1a9c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
34 changes: 20 additions & 14 deletions phpunit/class-wp-rest-block-pattern-categories-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@

class WP_REST_Block_Pattern_Categories_Controller_Test extends WP_Test_REST_Controller_Testcase {
protected static $admin_id;
protected static $orig_registry;

public function set_up() {
parent::set_up();
switch_theme( 'emptytheme' );
}

public static function wpSetupBeforeClass( $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
// Create a test user.
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );

// Setup an empty testing instance of `WP_Block_Pattern_Categories_Registry` and save the original.
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$reflection->getProperty( 'instance' )->setAccessible( true );
self::$orig_registry = $reflection->getStaticPropertyValue( 'instance' );
$test_registry = new WP_Block_Pattern_Categories_Registry();
$reflection->setStaticPropertyValue( 'instance', $test_registry );

// Register some categories in the test registry.
$test_registry->register( 'test', array( 'label' => 'Test' ) );
$test_registry->register( 'query', array( 'label' => 'Query' ) );
}

public function setup_mock_registry() {
$categories_reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$categories_instance_property = $categories_reflection->getProperty( 'instance' );
$categories_instance_property->setAccessible( true );
$categories_instance = new WP_Block_Pattern_Categories_Registry();
$categories_reflection->setStaticPropertyValue( 'instance', $categories_instance );
public static function wpTearDownAfterClass() {
// Delete the test user.
self::delete_user( self::$admin_id );

$categories_instance->register( 'test', array( 'label' => 'Test' ) );
$categories_instance->register( 'query', array( 'label' => 'Query' ) );
// Restore the original registry instance.
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$reflection->setStaticPropertyValue( 'instance', self::$orig_registry );
}

public function test_register_routes() {
Expand All @@ -37,7 +44,6 @@ public function test_register_routes() {
}

public function test_get_items() {
$this->setup_mock_registry();
wp_set_current_user( self::$admin_id );

$expected_names = array( 'test', 'query' );
Expand Down
39 changes: 23 additions & 16 deletions phpunit/class-wp-rest-block-patterns-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

class WP_REST_Block_Patterns_Controller_Test extends WP_Test_REST_Controller_Testcase {
protected static $admin_id;
protected static $orig_registry;

public function set_up() {
parent::set_up();
switch_theme( 'emptytheme' );
}

public static function wpSetupBeforeClass( $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
}
public static function wpSetUpBeforeClass( $factory ) {
// Create a test user.
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );

public function setup_mock_registry() {
$block_patterns_reflection = new ReflectionClass( 'WP_Block_Patterns_Registry' );
$block_patterns_instance_property = $block_patterns_reflection->getProperty( 'instance' );
$block_patterns_instance_property->setAccessible( true );
$block_patterns_instance = new WP_Block_Patterns_Registry();
$block_patterns_reflection->setStaticPropertyValue( 'instance', $block_patterns_instance );
// Setup an empty testing instance of `WP_Block_Patterns_Registry` and save the original.
$reflection = new ReflectionClass( 'WP_Block_Patterns_Registry' );
$reflection->getProperty( 'instance' )->setAccessible( true );
self::$orig_registry = $reflection->getStaticPropertyValue( 'instance' );
$test_registry = new WP_Block_Patterns_Registry();
$reflection->setStaticPropertyValue( 'instance', $test_registry );

$block_patterns_instance->register(
// Register some patterns in the test registry.
$test_registry->register(
'test/one',
array(
'title' => 'Pattern One',
Expand All @@ -32,7 +30,8 @@ public function setup_mock_registry() {
'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
)
);
$block_patterns_instance->register(

$test_registry->register(
'test/two',
array(
'title' => 'Pattern Two',
Expand All @@ -42,6 +41,15 @@ public function setup_mock_registry() {
);
}

public static function wpTearDownAfterClass() {
// Delete the test user.
self::delete_user( self::$admin_id );

// Restore the original registry instance.
$reflection = new ReflectionClass( 'WP_Block_Patterns_Registry' );
$reflection->setStaticPropertyValue( 'instance', self::$orig_registry );
}

public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey(
Expand All @@ -52,7 +60,6 @@ public function test_register_routes() {
}

public function test_get_items() {
$this->setup_mock_registry();
wp_set_current_user( self::$admin_id );

$expected_names = array( 'test/one', 'test/two' );
Expand Down

0 comments on commit e8c1a9c

Please sign in to comment.