Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix instance property reflection error in tests.
Resolves the `ReflectionException: Class [name of test class] does not have a property named instance`.

Error occurred as `getProperty()` is not available on `private` or `protected` properties.

This commit uses `ReflectionProperty` to change the `instance` property to `public` for the test.
The property is restored to `private` when done.
  • Loading branch information
hellofromtonya committed Apr 7, 2022
commit a3d71fbdce6a9f499d9732663a4fad8a817d0dec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class Tests_REST_WpRestBlockPatternCategoriesController extends WP_Test_REST_Con
*/
protected static $orig_registry;

/**
* Instance of the reflected `instance` property.
*
* @since 6.0.0
*
* @var ReflectionProperty
*/
private static $registry_instance_property;

/**
* The REST API route.
*
Expand All @@ -56,11 +65,11 @@ public static function wpSetupBeforeClass( $factory ) {
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 );
self::$orig_registry = WP_Block_Pattern_Categories_Registry::get_instance();
self::$registry_instance_property = new ReflectionProperty( 'WP_Block_Pattern_Categories_Registry', 'instance' );
self::$registry_instance_property->setAccessible( true );
$test_registry = new WP_Block_Pattern_Categories_Registry();
self::$registry_instance_property->setValue( $test_registry );

// Register some categories in the test registry.
$test_registry->register( 'test', array( 'label' => 'Test' ) );
Expand All @@ -71,8 +80,10 @@ public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );

// Restore the original registry instance.
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$reflection->setStaticPropertyValue( 'instance', self::$orig_registry );
self::$registry_instance_property->setValue( self::$orig_registry );
self::$registry_instance_property->setAccessible( false );
self::$registry_instance_property = null;
self::$orig_registry = null;
}

public function set_up() {
Expand Down
25 changes: 18 additions & 7 deletions tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_T
*/
protected static $orig_registry;

/**
* Instance of the reflected `instance` property.
*
* @since 6.0.0
*
* @var ReflectionProperty
*/
private static $registry_instance_property;

/**
* The REST API route.
*
Expand All @@ -56,11 +65,11 @@ public static function wpSetUpBeforeClass( $factory ) {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );

// 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 );
self::$orig_registry = WP_Block_Patterns_Registry::get_instance();
self::$registry_instance_property = new ReflectionProperty( 'WP_Block_Patterns_Registry', 'instance' );
self::$registry_instance_property->setAccessible( true );
$test_registry = new WP_Block_Pattern_Categories_Registry();
self::$registry_instance_property->setValue( $test_registry );

// Register some patterns in the test registry.
$test_registry->register(
Expand All @@ -87,8 +96,10 @@ public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );

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

public function set_up() {
Expand Down