Skip to content

Instantly share code, notes, and snippets.

@alpody
Last active October 24, 2015 16:41
Show Gist options
  • Save alpody/54fef7a246eb349528e4 to your computer and use it in GitHub Desktop.
Save alpody/54fef7a246eb349528e4 to your computer and use it in GitHub Desktop.
unittest private method in php
// http://www.webtipblog.com/unit-testing-private-methods-and-properties-with-phpunit/
class Foo {
private $foobar;
private function bar( $number )
{
// keep me private!
}
}
//------------
/**
* testBar
*
* @author Joe Sexton <joe@webtipblog.com>
*/
public function testBar()
{
$number = 1;
$object = new Foo();
$reflector = new ReflectionClass( 'Foo' );
$method = $reflector->getMethod( 'bar' );
$method->setAccessible( true );
$result = $method->invokeArgs( $object, array( $number ) );
$this->assertEquals( 1, $result ); // whatever your assertion is
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment