2

I have a ClassA that uses a ServiceB. In a certain case, ClassA should end up not invoking any methods of ServiceB except one. I now want to test this and verify no other methods are indeed called.

This can be done as follows:

$classA->expects( $this->once() )->method( 'first_method' );
$classA->expects( $this->never() )->method( 'second_method' );
$classA->expects( $this->never() )->method( 'third_method' );
...

Is there a way to simply state "no methods that are not first should be called on this object" rather then having to specify a restriction for each method?

I currently have this approach which works, though makes my test case dependent on two somewhat internal classes of PHPUnit, which I'd rather avoid.

$schema->expects( $this->never() )
            ->method( new PHPUnit_Framework_Constraint_Not( new PHPUnit_Framework_Constraint_IsEqual( 'addField' ) ) );

Is there a way to do the same using the fluent interface?

1 Answer 1

2

Yep, there is :) Try this one:

$classA->expects($this->once())->method('first_method');
$classA->expects($this->never())
    ->method($this->logicalNot($this->equalTo('first_method')));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.