为什么这个PHPUnit比较是错误的?(..$mock->;with($this->;identicalTo($foo))

为什么这个PHPUnit比较是错误的?(..$mock->;with($this->;identicalTo($foo)),php,unit-testing,testing,phpunit,Php,Unit Testing,Testing,Phpunit,我对PhpUnit有问题。我想使用PhpUnit的isIdentical方法来确保使用特定对象作为参数调用方法。 给出了一个方法为“setBar”的模拟对象: 这当然只是一个示例。它应该可以工作,但不能。为了得到一些提示,我在PHPUnit\u Framework\u Constraint\u isidential方法中编写了一些echo代码: public function evaluate($other, $description = '', $returnResult = FALSE) {

我对PhpUnit有问题。我想使用PhpUnit的
isIdentical
方法来确保使用特定对象作为参数调用方法。 给出了一个方法为“setBar”的模拟对象:

这当然只是一个示例。它应该可以工作,但不能。为了得到一些提示,我在
PHPUnit\u Framework\u Constraint\u isidential
方法中编写了一些echo代码:

public function evaluate($other, $description = '', $returnResult = FALSE)
{
    //...
    echo "\n";
    echo spl_object_hash($other) . "/". spl_object_hash($this->value). " - ". get_class($this->value) ."/". get_class($other);
    echo " ->> ";  
    var_dump($other === $this->value);
}
$other
的对象散列与
$this->value
的对象散列不一样(而
$this->value
的散列实际上是正确的)

同时,我在这里找到了这个错误的原因。这是PHPUnit的问题。对象被PHPUnit克隆。我留下的问题是,是否有人知道解决这个问题的好方法。


var\u dump的结果是什么?From“另一方面,当使用identity运算符(==)时,对象变量在且仅当它们引用同一类的同一实例时是相同的。”我不能确切地告诉您为什么它不起作用,但是如果该方法使用此运算符比较两个对象,那么它们似乎不是同一个实例。@Bhauy是的。我得到了错误的对象$other作为参数!这意味着,早些时候可能出了问题。调试和找到发生这种情况的位置有点困难,因为在PHPUnit中有很多东西是eval'd的(当然这里是必要的)我的第一个假设是eval'd变量无论如何都会得到一个新的散列-但是我在一个测试脚本中检查了这一点,并得到了很好的比较
public function evaluate($other, $description = '', $returnResult = FALSE)
{
    //...
    echo "\n";
    echo spl_object_hash($other) . "/". spl_object_hash($this->value). " - ". get_class($this->value) ."/". get_class($other);
    echo " ->> ";  
    var_dump($other === $this->value);
}