I´m trying to test if a protected method is called in a public interface.
<?php abstract class SomeClassSomeClassAbstract { abstract public foo(); public function doStuff() { $this->_protectedMethod(); } protected function _protectedMethod(); { // implementation is irrelevant } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled() { $mock = $this->getMock>getMockForAbstractClass('SomeClass'); $mock->expects($this->once()) ->method('_protectedMethod'); $mock->doStuff(); } } I know it is called correctly, but PHPUnit says its never called.
The same happens when I test the other way, when a method is never called:
<?php abstract class AnotherClassAnotherClassAbstract { abstract public foo(); public function doAnotherStuff() { $this->_loadCache(); } protected function _loadCache(); { // implementation is irrelevant } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled() { $mock = $this->getMock>getMockForAbstractClass('AnotherClass'); $mock->expects($this->once()) ->method('_loadCache'); $mock->doAnotherStuff(); } } The method is called but PHPUnit says that it is not.
What I´m doing wrong?
Edit I wasn´t declaring my methods with double colons, it was just for denoting that it was a public method (interface). Updated to full class/methods declarations.
Edit 2 I should have said that I´m testing some method implementations in an abstract class (edited the code to reflect this). Since I can not instantiate the class, how can I test this?
I´m thinking in creating an SomeClassSimple extending SomeClassAbstract and testing this one instead. Is it the right approach?