Skip to main content
Removed tag from title.
Link
user212218
user212218

PHPUnit: Testing if a protected method was called

uptaded description and code
Source Link
user32117
user32117

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?

I´m trying to test if a protected method is called in a public interface.

<?php class SomeClass { public function doStuff() { $this->_protectedMethod(); } protected function _protectedMethod(); { // implementation is irrelevant } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled() { $mock = $this->getMock('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 class AnotherClass { public function doAnotherStuff() { $this->_loadCache(); } protected function _loadCache(); { // implementation is irrelevant } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled() { $mock = $this->getMock('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.

I´m trying to test if a protected method is called in a public interface.

<?php abstract class SomeClassAbstract { 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->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 AnotherClassAbstract {  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->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?

added proper class/methods definitions per use request
Source Link
user32117
user32117

I´m trying to test if a protected method is called in a public interface.

Example:

<?php class SomeClass:: { public function doStuff()  {   $this->_protectedMethod(); } protected function _protectedMethod(); { // implementation is irrelevant  } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled()  {   $mock = $this->getMock('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 SomeClass::doStuffclass AnotherClass { public function doAnotherStuff()  {    $this->_loadCache(); } protected function _loadCache(); { // implementation is irrelevant } } <?php testNeverCalledclass MyTest extends PHPUnit_Framework_TestCase { public function testCalled()   {   $mock = $this->getMock('SomeClass''AnotherClass');   $mock->expects($this->never>once())   ->method('_loadCache');   $mock->doStuff>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.

I´m trying to test if a protected method is called in a public interface.

Example:

<?php SomeClass::doStuff() { $this->_protectedMethod(); } <?php testCalled() { $mock = $this->getMock('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 SomeClass::doStuff() { $this->_loadCache(); } <?php testNeverCalled()  { $mock = $this->getMock('SomeClass'); $mock->expects($this->never()) ->method('_loadCache'); $mock->doStuff(); } 

The method is called but PHPUnit says that it is not.

What I´m doing wrong?

I´m trying to test if a protected method is called in a public interface.

<?php class SomeClass { public function doStuff()  {   $this->_protectedMethod(); } protected function _protectedMethod(); { // implementation is irrelevant  } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled()  {   $mock = $this->getMock('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 class AnotherClass { public function doAnotherStuff()  {    $this->_loadCache(); } protected function _loadCache(); { // implementation is irrelevant } } <?php class MyTest extends PHPUnit_Framework_TestCase { public function testCalled()  {   $mock = $this->getMock('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.

Source Link
user32117
user32117
Loading