(PHPUnit) 如何检查具有不同参数的多个链式方法调用?
(PHPUnit) How do i check multiple chained method calls with different arguments?
我在执行测试后收到以下消息:
Expectation failed for method name is "register" when invoked 4 time(s).
Method was expected to be called 4 times, actually called 1 times.
如何确保使用参数 D、E、F、G 调用寄存器 4 次?
不改变注册方式?
代码:
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();
}
}
好的,我会自己回答的。
刚刚添加了 ->willReturnSelf() 和 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();
我在执行测试后收到以下消息:
Expectation failed for method name is "register" when invoked 4 time(s).
Method was expected to be called 4 times, actually called 1 times.
如何确保使用参数 D、E、F、G 调用寄存器 4 次? 不改变注册方式?
代码:
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();
}
}
好的,我会自己回答的。
刚刚添加了 ->willReturnSelf() 和 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();