0

Possible Duplicate:
PhpUnit private method testing

In my unit tests I need to call a private method to set my fixtures to a given state. On the other hand, I'd rather not make this method public. How do I go about this?

Community
  • 1
  • 1
d33tah
  • 10,999
  • 13
  • 68
  • 158

2 Answers2

4

Why not make those functions protected and create a derived class for testing purposes? That class can have public functions for testing that can initialize the various variables etc.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • And it's not necessary to have a subclass - you can just mock such class. Of course, it will create subclass with randomized name, but you do not need to make a subclass by yourself. – scriptin Dec 01 '12 at 14:46
1

Unit testing private methods is generally a bad idea, but if you really want to, reflection is the way to go. This should do it:

$reflection_class = new ReflectionClass($object_under_test);
$method = $reflection_class->getMethod('nameOfMethod');
$method->setAccessible(true);
$method->invoke($object_under_test, $param);

HTH

Friek
  • 1,533
  • 11
  • 13