Skip to content

Commit

Permalink
Add invokeInaccessibleMethod and getInaccessiblePropertyValue trait m…
Browse files Browse the repository at this point in the history
…ethods (#220)

<!--
Filling out the required portions of this template is mandatory. 
Any PR that does not include enough information to be reviewed may be
closed at a maintainers' discretion.
All new code requires associated documentation and unit tests.
-->

# Summary <!-- Required -->

Improves the `AccessInaccessibleClassMembersTrait` used in `WP_Mock` own
`TestCase` to invoke an inaccessible class method or get an inaccessible
property value.

## Contributor checklist <!-- Required -->

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you are unsure about any of these, please ask for
clarification. We are here to help! -->

- [x] I agree to follow this project's [**Code of
Conduct**](https://github.com/10up/.github/blob/trunk/CODE_OF_CONDUCT.md).
- [x] I have updated the documentation accordingly 
- [x] I have added tests to cover changes introduced by this pull
request
- [x] All new and existing tests pass

## Testing <!-- Required -->

<!-- If applicable, add specific steps for the reviewer to perform as
part of their testing process prior to approving this pull request. -->

<!-- List any configuration requirements for testing. -->

### Reviewer checklist <!-- Required -->

<!-- The following checklist is for the reviewer: add any steps that may
be relevant while reviewing this pull request -->

- [x] Code changes review
- [ ] Documentation changes review
- [x] Unit tests pass
- [x] Static analysis passes

---------

Co-authored-by: Ashley Gibson <[email protected]>
  • Loading branch information
unfulvio-godaddy and agibson-godaddy authored Jul 3, 2023
1 parent ae0992a commit 4b56a8c
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 4b56a8c

Please sign in to comment.