How do I mock one interface method?

I want to make fun of a method validatefor an interface, and all other interface methods blur the null return (I don't care what happens to them), but there seems to be no way to do this easily.

Here is what I have:

    $validator = $this
        ->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')
        ->setMethods(array('validate'))
        ->getMock();

    $validator
        ->expects($this->once())
        ->method('validate')
        ->willReturn(array());

    $validator->validate();

Doing this results in a fatal error:

The Mock_ValidatorInterface_56c4c003 class contains 6 abstract methods and therefore must be declared abstract or implement the remaining methods

So - I need to somehow say PHPUnit to drown out the other methods that the interface requires. What is the right way to do this?

+4
source share
1 answer

Declare all interface methods in ->setMethods().

, ->setMethods(). .

, .

+4

Source: https://habr.com/ru/post/1616625/


All Articles