I get the following message after executing the test:
Expectation failed for method name is "register" when invoked 4 time(s). Method was expected to be called 4 times, actually called 1 times.
How do i make sure that register is called 4 times with arguments D,E,F,G? without changing the register method?
Code:
class A { public function register(InterfaceABC $bar): self { //doSomething return $this; } }
class Example { private A $a; public function __construct(A $a){ $this->$a = $a; } public function fooBar() { $this->a->register(new D()) ->register(new E()) ->register(new F()) ->register(new G()); } }
class ExampleTest extends TestCase { public function testFooBar() { $mock = $this->createMock(A:class); $mock->expects(self::exactly(4)) ->method("register") ->withConsecutive( [self::isInstanceOf(D:class)], [self::isInstanceOf(E:class)], [self::isInstanceOf(F:class)], [self::isInstanceOf(G:class)] ); (new Example($mock))->foobar(); } }
Advertisement
Answer
Ok cool, i will answer myself.
Just added ->willReturnSelf() and boom green.
$mock->expects(self::exactly(4)) ->method("register") ->withConsecutive( [self::isInstanceOf(D:class)], [self::isInstanceOf(E:class)], [self::isInstanceOf(F:class)], [self::isInstanceOf(G:class)] )->willReturnSelf();