Skip to content

Commit

Permalink
Merge branch 'trunk' into chore/update-wp-mock-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
agibson-godaddy authored Jul 3, 2023
2 parents 0ac386a + 4b56a8c commit 1638cb2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
29 changes: 29 additions & 0 deletions php/WP_Mock/Traits/AccessInaccessibleClassMembersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public function getInaccessibleMethod($class, string $methodName): ReflectionMet
return $method;
}

/**
* Invokes the given inaccessible method on the given class.
*
* @param object $class the class name or instance to call against
* @param string $methodName the method name to call
* @param mixed ...$args arguments to pass to the invoked method
* @return mixed
* @throws ReflectionException
*/
public function invokeInaccessibleMethod(object $class, string $methodName, ...$args)
{
return $this->getInaccessibleMethod($class, $methodName)->invoke($class, ...$args);
}

/**
* Gets the given inaccessible property for the given class.
*
Expand All @@ -52,6 +66,21 @@ public function getInaccessibleProperty($class, string $propertyName): Reflectio
return $property;
}

/**
* Gets the given inaccessible property value for the given class.
*
* Allows for calling protected and private properties on a class.
*
* @param object $class the class name or instance
* @param string $property the property name
* @return mixed the property value
* @throws ReflectionException
*/
public function getInaccessiblePropertyValue(object $class, string $property)
{
return $this->getInaccessibleProperty($class, $property)->getValue($class);
}

/**
* Allows for setting private or protected properties in a class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ final class AccessInaccessibleClassMembersTraitTest extends TestCase
public function testCanGetInaccessibleProperty(): void
{
$instance = $this->getInstance('test');
/* @phpstan-ignore-next-line */
$property = $instance->getInaccessibleProperty($instance, 'property');

$this->assertEquals('property', $property->getName());
Expand All @@ -42,11 +41,24 @@ public function testCanSetInaccessibleProperty(): void
$this->assertEquals('bar', $property->getValue($instance));
}

/**
* @covers \WP_Mock\Traits\AccessInaccessibleClassMembersTrait::getInaccessiblePropertyValue()
*
* @return void
* @throws ReflectionException|Exception
*/
public function testCanGetInaccessiblePropertyValue(): void
{
$instance = $this->getInstance('test');

$this->assertEquals('test', $instance->getInaccessiblePropertyValue($instance, 'property'));
}

/**
* @covers \WP_Mock\Traits\AccessInaccessibleClassMembersTrait::getInaccessibleMethod()
*
* @return void
* @throws Exception
* @throws ReflectionException|Exception
*/
public function testCanGetInaccessibleMethod(): void
{
Expand All @@ -57,6 +69,19 @@ public function testCanGetInaccessibleMethod(): void
$this->assertEquals('test', $method->invoke($instance));
}

/**
* @covers \WP_Mock\Traits\AccessInaccessibleClassMembersTrait::invokeInaccessibleMethod()
*
* @return void
* @throws ReflectionException|Exception
*/
public function testCanInvokeInaccessibleMethod(): void
{
$instance = $this->getInstance('test');

$this->assertEquals('test', $instance->invokeInaccessibleMethod($instance, 'method'));
}

/**
* Gets the instance of an object implementing the trait.
*
Expand Down

0 comments on commit 1638cb2

Please sign in to comment.