Created
March 19, 2013 15:31
-
-
Save JeffreyWay/5197083 to your computer and use it in GitHub Desktop.
Test PHP protected/private methods with ease using Reflection.
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
<?php | |
// ... | |
public function testProtected() | |
{ | |
$dateFormatter = new DateFormatter; | |
$class = new \ReflectionClass('DateFormatter'); | |
// Find the protected/private method and make it public | |
$getSentence = $class->getMethod('getSentence'); | |
$getSentence->setAccessible(true); | |
// Trigger the method, and pass in any applicable args | |
$sentence = $getSentence->invokeArgs($dateFormatter, [1, 'month']); | |
// Do your test as usual | |
$this->assertEquals('1 month from now.', $sentence); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment