Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created March 19, 2013 15:31
Show Gist options
  • Save JeffreyWay/5197083 to your computer and use it in GitHub Desktop.
Save JeffreyWay/5197083 to your computer and use it in GitHub Desktop.
Test PHP protected/private methods with ease using Reflection.
<?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