PHPUnit 是否有一些内置的递归数组比较函数?

发布于 2024-09-03 20:01:12 字数 154 浏览 6 评论 0原文

我需要做的一些测试需要将已知数组与我从将要运行的函数中获得的结果进行比较。

用于递归比较数组:

  • PHPUnit 有内置函数吗?
  • 这里有人有一些他们构建的代码可以分享吗?
  • 这是我必须自己构建的东西吗?

Some of the testing I will need to do will require comparing a known array with the result I am getting from the functions I will be running.

For comparing arrays recursively:

  • Does PHPUnit have an inbuilt function?
  • Does someone here have some code they have constructed to share?
  • Will this be something I will have to construct on my own?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱花细雨 2024-09-10 20:01:12

是的,确实如此。 assertEquals()assertNotEquals() 文档

具体来说:

assertEquals()

assertEquals(混合$expected, 混合$actual[, string $message = ''])

如果两个变量 $expected$actual 不相等,则报告由 $message 标识的错误。

assertNotEquals() 是此断言的逆过程,并采用相同的参数。

测试代码:

public function testArraysEqual() {
    $arr1 = array( 'hello' => 'a', 'goodbye' => 'b');
    $arr2 = array( 'hello' => 'a', 'goodbye' => 'b');

    $this->assertEquals($arr1, $arr2);
}

public function testArraysNotEqual() {
    $arr1 = array( 'hello' => 'a', 'goodbye' => 'b');
    $arr2 = array( 'hello' => 'b', 'goodbye' => 'a');

    $this->assertNotEquals($arr1, $arr2);
}

[编辑]

这是无序 aList 的代码:

public function testArraysEqualReverse() {
    $arr1 = array( 'hello' => 'a', 'goodbye' => 'b');
    $arr2 = array( 'goodbye' => 'b', 'hello' => 'a');

    $this->assertEquals($arr1, $arr2);
}

此测试失败:

public function testArraysOutOfOrderEqual() {
    $arr1 = array( 'a', 'b');
    $arr2 = array( 'b', 'a');

    $this->assertEquals($arr1, $arr2);
}

带有消息:

Failed asserting that 
Array
(
    [0] => b
    [1] => a
)
 is equal to 
Array
(
    [0] => a
    [1] => b
)

Yes it does. assertEquals() and assertNotEquals() documentation.

Specifically:

assertEquals()

assertEquals(mixed $expected, mixed $actual[, string $message = ''])

Reports an error identified by $message if the two variables $expected and $actual are not equal.

assertNotEquals() is the inverse of this assertion and takes the same arguments.

Test Code:

public function testArraysEqual() {
    $arr1 = array( 'hello' => 'a', 'goodbye' => 'b');
    $arr2 = array( 'hello' => 'a', 'goodbye' => 'b');

    $this->assertEquals($arr1, $arr2);
}

public function testArraysNotEqual() {
    $arr1 = array( 'hello' => 'a', 'goodbye' => 'b');
    $arr2 = array( 'hello' => 'b', 'goodbye' => 'a');

    $this->assertNotEquals($arr1, $arr2);
}

[EDIT]

Here is the code for out of order aLists:

public function testArraysEqualReverse() {
    $arr1 = array( 'hello' => 'a', 'goodbye' => 'b');
    $arr2 = array( 'goodbye' => 'b', 'hello' => 'a');

    $this->assertEquals($arr1, $arr2);
}

This test fails:

public function testArraysOutOfOrderEqual() {
    $arr1 = array( 'a', 'b');
    $arr2 = array( 'b', 'a');

    $this->assertEquals($arr1, $arr2);
}

With message:

Failed asserting that 
Array
(
    [0] => b
    [1] => a
)
 is equal to 
Array
(
    [0] => a
    [1] => b
)
白云不回头 2024-09-10 20:01:12

@wilmoore

$array1 = array('hi','hi2');
$array2 = array('hi2','hi');
$this->assertEquals(array_values($array1), array_values($array2));

会失败。

@Ben Dauphinee

可能值得一看 assertContains(mixed $needle, array $haystack) 但您必须循环遍历两个数组并将每个元素与另一个数组进行比较,以确保它包含所有必需的元素而不是其他元素,但是这不会考虑包含两个相同元素的数组

$array1 = array('hi','hi2','hi');
$array2 = array('hi2','hi');

在这种情况下会通过

它也不会考虑任何进一步的递归,这可能会非常复杂处理。

从长远来看,根据复杂性,实现您自己的断言函数可能会更容易。

@wilmoore

$array1 = array('hi','hi2');
$array2 = array('hi2','hi');
$this->assertEquals(array_values($array1), array_values($array2));

Will fail.

@Ben Dauphinee

It might be worth looking at assertContains(mixed $needle, array $haystack) but you would have to loop through both arrays and compare each element with the other array to ensure it contained all the required elements and no others, this however wouldn't account for an array containing two identical elements

$array1 = array('hi','hi2','hi');
$array2 = array('hi2','hi');

would pass in this case

It also doesn't account for any further recursion which would probably be quite complicated to deal with.

Depending on the complexity might just be easier in the long run to implement your own assert function.

断桥再见 2024-09-10 20:01:12

assertEqual 方法头如下所示:

public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)

如果 canonicalize 参数设置为 true,则数组将首先通过 sort() 运行,如果键是任意的并且只有价值观才重要。

然而,在查看了数组比较器代码之后,assertEqual 实际上并不关心关联数组的顺序:)它只会在结果数组中查找预期的键,然后比较那些钥匙。

The assertEqual method head looks like this:

public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)

If the canonicalize parameter is set to true the arrays will be run through sort() first, this is usable if the keys are arbitrary and only the values matters.

However after looking over the array comparator code, the assertEqual actually does not care about the order of an associated array :) It will simply look for the expected key in the result array, and then compare the values of those keys.

初相遇 2024-09-10 20:01:12

我在使用一些带有键的生成数组时遇到了这个问题 - 在调用assertEquals之前,我最终传递了预期数组和通过ksort测试的数组。但这不适用于递归数组。

$expectedArray = array('foo' => 1, 'bar' => 0);
$array = array('bar' => 0, 'foo' => 1);

ksort($expectedArray);
ksort($array);

var_dump($expectedArray); // outputs array('bar' => 0, 'foo' => 1);
var_dump($array); // outputs array('bar' => 0, 'foo' => 1);

$this->assertEquals($expectedArray, $array); // passes

I had this problem with some generated arrays with keys - I ended up passing both the expected array and the array being tested through ksort before calling assertEquals. This doesn't work on recursive arrays, though.

$expectedArray = array('foo' => 1, 'bar' => 0);
$array = array('bar' => 0, 'foo' => 1);

ksort($expectedArray);
ksort($array);

var_dump($expectedArray); // outputs array('bar' => 0, 'foo' => 1);
var_dump($array); // outputs array('bar' => 0, 'foo' => 1);

$this->assertEquals($expectedArray, $array); // passes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文