Skip to content

Instantly share code, notes, and snippets.

@andybeak
Last active February 24, 2020 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybeak/eabba7e121802b1cf36e5f0ad3db8a31 to your computer and use it in GitHub Desktop.
Save andybeak/eabba7e121802b1cf36e5f0ad3db8a31 to your computer and use it in GitHub Desktop.
Test protected and private methods in PHPUnit
// -----------------------------------------------------------------------------------------------------------------
protected function getMethod($methodName, $object)
{
try {
$class = new \ReflectionClass(get_class($object));
$method = $class->getMethod($methodName);
$method->setAccessible(true);
return $method;
} catch (\ReflectionException $e) {
die("Reflection exception [{$e->getMessage()}]");
}
}
// -----------------------------------------------------------------------------------------------------------------
public function test_exampleMethod()
{
$objectUnderTest = $this->app->make(objectUnderTest::class);
$method = $this->getMethod('exampleMethod', $objectUnderTest);
$body = $method->invokeArgs($objectUnderTest, ['parameter1', $parameter2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment