To express any method , you can use $this->anything() .
Full example:
<?php class fooTest extends PHPUnit_Framework_TestCase { public function testNeverCallNothing() { $mock = $this->getMock('mockMe'); $mock->expects($this->never())->method($this->anything());
Outputs:
PHPUnit 3.7.10-4-ga0bccf3 by Sebastian Bergmann. . Time: 0 seconds, Memory: 6.50Mb OK (1 test, 1 assertion)
When commenting on a method call
$mock->bar();
he outputs:
PHPUnit 3.7.10-4-ga0bccf3 by Sebastian Bergmann. F Time: 0 seconds, Memory: 6.50Mb There was 1 failure: 1) fooTest::testNeverCallNothing mockMe::bar() was not expected to be called. .../tests/neverCallMe/fooTest.php:9 FAILURES! Tests: 1, Assertions: 0, Failures: 1.
Allowing only one method call, but not calling other methods
It looks a little ugly, but also works
<?php class fooTest extends PHPUnit_Framework_TestCase { public function testNeverCallNothing() { $mock = $this->getMock('mockMe'); $mock->expects($this->once())->method('foo'); $mock->expects($this->never())->method( $this->logicalNot($this->matches('foo')) ); $mock->foo();
Works. When commenting in another method causes it to fail, as shown above.
If you want to allow the use of several methods, it becomes a little more verbose:
$this->logicalNot( $this->logicalOr( $this->matches('foo'), $this->matches('baz'), $this->matches('buz') ) )
source share