Writing unit tests for code which is already written is fun sometimes.
I am writing a test case for the following code (an example):
<?php class mockPrivate { public static function one($a){ $var = static::_two($a); return $var; } private static function _two($a){ return $a+1; } } ?> The test class is like this:
<?php require_once 'mockPvt.php'; class mockPrivate_test extends PHPUnit_Framework_TestCase { public $classMock; protected function setUp(){ $this->classMock = $this->getMock('mockPrivate', array('_two')); } public function test_one(){ $a = 1; $retVal = 2; $classmock = $this->classMock; $classmock::staticExpects($this->once()) ->method('_two') ->with($a) ->will($this->returnValue($retVal)); $value = $classmock::one($a); $this->assertEquals($value, $retVal); } } ?> After running by $ phpunit mockPrivate_test.php I got this error:
PHP Fatal error: Call to private method Mock_mockPrivate_531a1619::_two() from context 'mockPrivate' in /data/www/dev-sumit/tests/example s/mockPvt.php on line 6 But if I change the
private static function _two() to public static function _two() or protected static function _two() it works totally fine. Since this is a legacy code I can't change the private to public/protected. So is there any way I can test the function one or Is this a limitation of phpunit?