Last active
February 24, 2020 16:24
-
-
Save andybeak/eabba7e121802b1cf36e5f0ad3db8a31 to your computer and use it in GitHub Desktop.
Test protected and private methods in PHPUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------------------------------------------------------- | |
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