PHPUnit:期望以数组作为参数的方法调用

PHPUnit: expect method call with array as argument

我有一个 PHP 单元测试用例,我对其中的以下片段感到困惑。我想检查方法 actionUpload 是否正确调用了函数 exposeAndSaveDataLines,即第一个参数是我期望的数组。

public function test_actionUpload()
{
    $sut = $this->getMockBuilder('MasterdataController')
                ->setMethods(array('exposeAndSaveDataLines', 'render'))
                ->disableOriginalConstructor()
                ->getMock();

    $expectedLines = require_once ($this->dataDir . 'expectedLines.php');

    $sut->expects($this->once())
        ->method('exposeAndSaveDataLines')
        ->with($this->equalTo($expectedLines),
            $this->anything(),
            $this->anything(),
            $this->anything(),
            $this->anything());

    $sut->actionUpload();
}

预期数据是当前数组的打印输出,在实际代码中使用临时 print_r (var_export($lines)) 制作。我return在文件expectedLines.php里面,手动打印的时候是正确的

现在,当我 运行 在 expectedLines 中故意拼错单个字符的测试用例时,我得到以下错误(如预期的那样)。

Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
             3 => 'Colour Group Code'
-            4 => '{2F30E832-D3DB-447E-B733-7BC5125CBCCc}'
+            4 => '{2F30E832-D3DB-447E-B733-7BC5125CBCCC}'
         )
     )
 )

但是我改错的时候还是提示两个数组不相等。但是,它现在打印整个数组(至少它的开头,它是一个长数组),但它没有显示任何差异(任何行前面没有 - 和 +)。为什么 expects 方法不能识别两个数组相同?我怎样才能正确测试它?

编辑 1

我已经缩短了数组,以便在它们不相等时打印整个数组。比较中仍然没有 + 或 - 符号。

这是我期望的 PHP 文件的结尾。

    'RetTarget Area' => array(
        0 => array(
            0 => '',
            1 => '',
            2 => '{C19D52BC-834C-45DA-B17F-74D73A2EC0BB}
'
        ),
        1 => array(
            0 => '1',
            1 => '1',
            2 => '{5E25C44F-C18A-4F54-B6B1-248955A82E59}'
        )
    )
);

我在控制台的比较输出到此结束。

     'RetTarget Area' => Array (
         0 => Array (
             0 => ''
             1 => ''
             2 => '{C19D52BC-834C-45DA-B17F-74D73A2EC0BB}
             '
         )
         1 => Array (...)
     )
 )

最后一个数组在比较中没有完全显示,我觉得很可疑。

编辑 2

我发现 数组的顺序很重要。我很确定虽然我的所有元素都按相同的顺序排列,但如果 PHP 没有在幕后做一些秘密的事情。那里提到的解决方案我无法复制,因为我没有 $this->assertEquals 而是 ->with($this->equalTo 语法。

编辑 3

我读到 here 关于一个未记录的参数 $canonicalize,它在比较之前对数组进行排序。当我这样使用它时:

$sut->expects($this->once())
    ->method('exposeAndSaveDataLines')
    ->with($this->equalTo($expectedLines, $delta = 0.0, $maxDepth = 10, $canonicalize = true, $ignoreCase = false),
        $this->anything(),
        $this->anything(),
        $this->anything(),
        $this->anything());

我看到数组的顺序确实改变了,但我仍然看到同样的错误。此外,还有一个数组是 'collapsed',我怀疑这是导致此失败的原因。此外,我不想对我所有的子数组进行排序,它们在实际结果和预期结果中的顺序应该相同。

--- Expected
+++ Actual
@@ @@
 Array (
     0 => Array (
         0 => Array (
             0 => ''
             1 => ''
             2 => '{C19D52BC-834C-45DA-B17F-74D73A2EC0BB}
             '
         )
         1 => Array (...)
     )

编辑 4

当我使用 identicalTo 而不是 equalTo 时,我得到了一个更详细的错误消息,说一个数组与另一个数组不同,同时打印它们。我将它们都复制粘贴到一个文本文件中,并使用命令 diff 检查是否有任何差异,但有 none。不过,PHPUnit 声称这两个数组不是 equal/identical.

编辑 5

当我使用 greaterThanOrEqual 甚至 greaterThan 而不是 equalTo 时,测试通过。 lessThanOrEqual 不会发生这种情况。这意味着两个数组之间存在 差异。

如果我手动将预期结果更改为按字母顺序排列在正确字符串之前的字符串,我也可以 lessThan 通过,但当然 greaterThanOrEqual 会失败。

编辑 6

我越来越确信我的数组中字符串的行结尾导致比较失败,这并没有出现在所有比较中。

我现在有以下断言。

public function test_actionUpload_v10MasterdataFile()
{
    ....
    $sut->expects($this->once())
        ->method('exposeAndSaveDataLines')
        ->will($this->returnCallback(function($lines) {
            $expectedLines = include ($this->dataDir . 'ExpectedLines.php');
            $arrays_similar = $this->similar_arrays($lines, $expectedLines);
            PHPUnit_Framework_Assert::assertTrue($arrays_similar);
        }));
    $sut->actionUpload();
}

private function similar_arrays($a, $b)
{
    if(is_array($a) && is_array($b))
    {
        if(count(array_diff(array_keys($a), array_keys($b))) > 0)
        {
            print_r(array_diff(array_keys($a), array_keys($b)));
            return false;
        }

        foreach($a as $k => $v)
        {
            if(!$this->similar_arrays($v, $b[$k]))
            {
                return false;
            }
        }

        return true;
    }
    else
    {
        if ($a !== $b)
        {
            print_r(PHP_EOL . 'A: '. $a. PHP_EOL . 'Type: ' . gettype($a) . PHP_EOL);
            print_r(PHP_EOL . 'B: '. $b. PHP_EOL . 'Type: ' . gettype($b) . PHP_EOL);
        }
        return $a === $b;
    }
}

结果如下。

A: {72C2F175-9F50-4C9C-AF82-9E3FB875EA82}

Type: string

B: {72C2F175-9F50-4C9C-AF82-9E3FB875EA82}

Type: string

我终于让它工作了,尽管这有点妥协。我现在在比较数组之前删除换行符。这在with方法中是做不到的,所以我做了如下构造

public function test_actionUpload_v10MasterdataFile()
{
    /*
     * Create a stub to disable the original constructor.
     * Exposing data and rendering are stubbed.
     * All other methods behave exactly the same as in the real Controller.
     */
    $sut = $this->getMockBuilder('MasterdataController')
                ->setMethods(array('exposeAndSaveDataLines', 'render'))
                ->disableOriginalConstructor()
                ->getMock();

    $sut->expects($this->once())
        ->method('exposeAndSaveDataLines')
        ->will($this->returnCallback(function($lines) {
            $expectedLines = include ($this->dataDir . 'ExpectedLines.php');
            PHPUnit_Framework_Assert::assertTrue($this->similar_arrays($lines, $expectedLines));
        }));

    // Execute the test
    $sut->actionUpload();
}

...

private function similar_arrays($a, $b)
{
    /**
     * Check if two arrays have equal keys and values associated with it, without
     * looking at order of elements, and discarding newlines.
     */
    if(is_array($a) && is_array($b))
    {
        if(count(array_diff(array_keys($a), array_keys($b))) > 0)
        {
            return false;
        }

        foreach($a as $k => $v)
        {
            if(!$this->similar_arrays($v, $b[$k]))
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        $a = rtrim($a);
        $b = rtrim($b);
        $extended_output = false;
        if ($extended_output && ($a !== $b))
        {
            print_r(PHP_EOL . 'A: '. $a. PHP_EOL . 'Type: ' . gettype($a) . PHP_EOL);
            print_r(PHP_EOL . 'B: '. $b. PHP_EOL . 'Type: ' . gettype($b) . PHP_EOL);
        }
        return $a === $b;
    }
}