PHPUnit 对一个参数使用多个 stringContains 断言模拟方法

wor*_*hit 2 php phpunit unit-testing

有没有办法为多个匹配断言模拟类方法字符串参数?

$this->getMock()
     ->expects($this->any())
     ->method('setString')
     ->with($this->stringContains('word3'))
     ->will($this->returnSelf());
Run Code Online (Sandbox Code Playgroud)

这个例子将通过 for ->setString('word1 word2 word3 word4')

如果 setString() 使用包含“word1”和“word3”的参数调用,我需要做的是匹配

$this->getMock()
     ->expects($this->any())
     ->method('setString')
     ->with(
         $this->stringContains('word1'),
         $this->stringContains('word3')
     )
     ->will($this->returnSelf());
Run Code Online (Sandbox Code Playgroud)

此实现正在检查 setString() 的 2 个参数,这不是我打算检查的。

想法?使用 $this->callback() ?是否有一些更适合的 PHPUnit 断言?

小智 5

我知道这个答案已经很晚了,但我遇到了同样的问题并找到了一个似乎更聪明的解决方案...... :)

试试这个:(未经测试您的情况)

$this->getMock()
     ->expects($this->any())
     ->method('setString')
     ->with($this->logicalAnd(
          $this->stringContains('word1'),
          $this->stringContains('word3')
      ))
     ->will($this->returnSelf());
Run Code Online (Sandbox Code Playgroud)