Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect a method to be never called with certain arguments

Tags:

php

phpunit

In PHPUnit how can I test if a method is never called with certain arguments? I mean that it can be called with any other arguments even multiple times but never with a certain one.

like image 894
user2394156 Avatar asked Dec 01 '25 06:12

user2394156


1 Answers

Looks like it's not possible using the standard way so you can use a callback for specifying the return value, and check the arguments there (bit tricky but seems to work):

$mock = $thi->getMockBuilder('MyClass')->getMock();
$mock->expects($this->any())
     ->method('myMethod')
     ->willReturnCallback(function() {
        $args = func_get_args();
        $disallowedArgs = [1, 'abc'];
        $this->assertNotEquals($disallowedArgs, $args);
    })
;
like image 164
gontrollez Avatar answered Dec 03 '25 21:12

gontrollez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!