2017 Update note: before proceeding think if you really want to test underlying implementation of a class in the first place ;-)
Here’s quick example how to test private class method with SimpleTest using RelectionClass (while you are reading about reflection class I will also point you directly to reflection method and what it’s all about).
Our class that we want to test with some private method:
// sample_class.php
<?php
class SampleClass {
private function myPrivateMethod($arg1) {
return $arg1 == 7;
}
}
?>
And here’s how you test that myPrivateMethod
:
// sample_class_spec.php
<?php
class SampleClassTest extends UnitTestCase {
function testPrivateMethod() {
$reflection_class = new ReflectionClass("SampleClass");
$method = $reflection_class->getMethod("myPrivateMethod");
$method->setAccessible(true);
$sample_class = new SampleClass();
$result = $method->invoke($sample_class, 7); # here you invoke method with you class object and pass arguments
$this->assertEqual($result, true);
}
}
?>