sgi31955
sgi31955

Reputation: 23

PHP Unit assert no method is called except some

Similar question to this: PHPUnit assert no method is called

How to assert that no method is called except some that I can define? The following test doesn't pass, because PHPUnit validates all expects().

//Here assert that only 'firstMethodToBeCalled' and 'secondMethodToBeCalled' are called, and no more
$mock = $this->getMockBuilder('SomeClass')->getMock();
$mock->expects($this->never())
    ->method($this->anything());
$mock->expects($this->once())
    ->method('firstMethodToBeCalled');
$mock->expects($this->once())
    ->method('secondMethodToBeCalled');

Upvotes: 0

Views: 138

Answers (1)

maximkou
maximkou

Reputation: 5332

Try to use this:

$mock = $this->getMockBuilder('SomeClass')->getMock();

$mock->expects($this->once())
    ->method('firstMethodToBeCalled');
$mock->expects($this->once())
    ->method('secondMethodToBeCalled');
$mock->expects($this->never())
    ->method(
        $this->logicalAnd(
            $this->logicalNot($this->equalTo('firstMethodToBeCalled')),
            $this->logicalNot($this->equalTo('secondMethodToBeCalled')),
        )
    );

Upvotes: 1

Related Questions