Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin Dependencies: Store reflected members instead of creating new ones in tests. #17

Merged
Changes from all commits
Commits
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
51 changes: 39 additions & 12 deletions tests/phpunit/tests/admin/plugin-dependencies/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ abstract class WP_PluginDependencies_UnitTestCase extends WP_UnitTestCase {
'dependency_filepaths' => array(),
);

/**
* An array of reflected class members.
*
* @var ReflectionMethod[]|ReflectionProperty[]
*/
protected static $reflected_members = array();

/**
* Sets up the WP_Plugin_Dependencies instance before any tests run.
*/
Expand All @@ -43,6 +50,15 @@ public static function set_up_before_class() {
self::$instance = new WP_Plugin_Dependencies();
}

/**
* Empties the '$reflected_members' property after all tests run.
*/
public static function tear_down_after_class() {
self::$reflected_members = array();

parent::tear_down_after_class();
}

/**
* Resets all static properties to a default value after each test.
*/
Expand All @@ -61,10 +77,13 @@ public function set_up() {
* @param mixed $value The new value.
*/
public function set_property_value( $property, $value ) {
$reflection_property = new ReflectionProperty( self::$instance, $property );
$reflection_property->setAccessible( true );
$reflection_property->setValue( self::$instance, $value );
$reflection_property->setAccessible( false );
if ( ! isset( self::$reflected_members[ $property ] ) ) {
self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property );
}

self::$reflected_members[ $property ]->setAccessible( true );
self::$reflected_members[ $property ]->setValue( self::$instance, $value );
self::$reflected_members[ $property ]->setAccessible( false );
}

/**
Expand All @@ -74,10 +93,14 @@ public function set_property_value( $property, $value ) {
* @return mixed The value of the property.
*/
public function get_property_value( $property ) {
$reflection_property = new ReflectionProperty( self::$instance, $property );
$reflection_property->setAccessible( true );
$value = $reflection_property->getValue( self::$instance );
$reflection_property->setAccessible( false );
if ( ! isset( self::$reflected_members[ $property ] ) ) {
self::$reflected_members[ $property ] = new ReflectionProperty( self::$instance, $property );
}

self::$reflected_members[ $property ]->setAccessible( true );
$value = self::$reflected_members[ $property ]->getValue( self::$instance );
self::$reflected_members[ $property ]->setAccessible( false );

return $value;
}

Expand All @@ -90,10 +113,14 @@ public function get_property_value( $property ) {
* @return mixed The result of the method call.
*/
protected function call_method( $method, ...$args ) {
$reflection_method = new ReflectionMethod( self::$instance, $method );
$reflection_method->setAccessible( true );
$value = $reflection_method->invokeArgs( self::$instance, $args );
$reflection_method->setAccessible( false );
if ( ! isset( self::$reflected_members[ $method ] ) ) {
self::$reflected_members[ $method ] = new ReflectionMethod( self::$instance, $method );
}

self::$reflected_members[ $method ]->setAccessible( true );
$value = self::$reflected_members[ $method ]->invokeArgs( self::$instance, $args );
self::$reflected_members[ $method ]->setAccessible( false );

return $value;
}
}