PHPUnit Mock Objects never expects by default

Is there a way to tell the phpunit mock object to never expect a method call if there are no formally defined expectations for them?

+4
source share
2 answers

In my opinion, he had no idea to never count on each method. Thus phpunit has no functionality. It can use "never" expectations only if you want to make sure completely that some method will not be called.

In any case, you can use some matches to get closer to your goal. Examples:

It was never expected that all methods of the object (it will not be possible to call any of the mocking methods):

$mock->expects($this->never()) ->method($this->anything()); 

So, for example, you can verify that an object will not call any method other than the verified one:

 $mock = $this->getMock('Some\Tested\Class', array('testedMethod')); $mock->expects($this->never()) ->method($this->anything()); 

You can also try with another convention, for example. matchesRegularExpression :

 $mock->expects($this->never()) ->method($this->matchesRegularExpression('/get.*/')); 

For example, the above will not be executed if any receiver is called.

I know that this is not exactly what you want, but I'm afraid there is no such solution with phpunit.

+9
source

If you want to verify that a method is never called when a specific argument is given, use

 $mock->expects($this->any()) ->method('foo') ->with(new PHPUnit_Framework_Constraint_Not('bar')); 
+1
source

All Articles