如何测试数组仅包含带有phpunit的对象?

发布于 2025-01-30 04:18:39 字数 538 浏览 5 评论 0原文

我正在寻找在我的Laravel项目中使用phpunit测试一系列对象的解决方案。

这是我的干草堆数组:

[
    [
        "id" => 10,
        "name" => "Ten"
    ],
    [
        "id" => 5,
        "name" => "Five"
    ]
]

这是针数组:

[
    [
        "id" => 5,
        "name" => "Five"
    ],
    [
        "id" => 10,
        "name" => "Ten"
    ]
]

对象的顺序无关紧要,对象的键也无关紧要。唯一的问题是我们有两个对象,所有对象的键和完全相同的值。

正确的解决方案是什么?

I'm looking for solution to test an array of objects with PHPUnit in my Laravel project.

This is my haystack array:

[
    [
        "id" => 10,
        "name" => "Ten"
    ],
    [
        "id" => 5,
        "name" => "Five"
    ]
]

And this is the needles array:

[
    [
        "id" => 5,
        "name" => "Five"
    ],
    [
        "id" => 10,
        "name" => "Ten"
    ]
]

The order of objects doesn't matter, also keys of objects doesn't matter. The only matter is we have two objects and all objects has exactly same keys and exactly same values.

What is the correct solution for this?

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

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

发布评论

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

评论(2

成熟稳重的好男人 2025-02-06 04:18:39

您可以使用assertContainSequals方法来执行此操作:

$haystack = [
    [
        'id' => 10,
        'name' => 'Ten'
    ],
    [
        'id' => 5,
        'name' => 'Five'
    ]
];

$needles = [
    [
        'name' => 'Five',
        'id' => 5
    ],
    [
        'id' => 10,
        'name' => 'Ten'
    ]
];

foreach ($needles as $needle) {
    $this->assertContainsEquals($needle, $haystack);
}

如果您打算更频繁地执行断言,也可以创建自己的自信方法:

public function assertContainsEqualsAll(array $needles, array $haystack): void
{
    foreach ($needles as $needle) {
        $this->assertContainsEquals($needle, $haystack);
    }
}

You can do this using the assertContainsEquals method like this:

$haystack = [
    [
        'id' => 10,
        'name' => 'Ten'
    ],
    [
        'id' => 5,
        'name' => 'Five'
    ]
];

$needles = [
    [
        'name' => 'Five',
        'id' => 5
    ],
    [
        'id' => 10,
        'name' => 'Ten'
    ]
];

foreach ($needles as $needle) {
    $this->assertContainsEquals($needle, $haystack);
}

You could also a create your own assert method if you intend to perform the assertion more often:

public function assertContainsEqualsAll(array $needles, array $haystack): void
{
    foreach ($needles as $needle) {
        $this->assertContainsEquals($needle, $haystack);
    }
}
长梦不多时 2025-02-06 04:18:39

基于 @roj vreemen 的答案我实现了此解决方案以确切匹配:

    public function assertArrayContainsEqualsOnly(array $needles, array $haystack, string $context = ''): void
    {
        foreach ($needles as $index => $needle) {
            $this->assertContainsEquals(
                $needle,
                $haystack,
                ($context ? $context . ': ' : '') . 'Object not found in array.'
            );

            unset($haystack[$index]);
        }

        $this->assertEmpty(
            $haystack,
            ($context ? $context . ': ' : '') . 'Not exact match objects in array.'
        );
    }

Based on @Roj Vroemen's answer I implemented this solution for exact match asserting:

    public function assertArrayContainsEqualsOnly(array $needles, array $haystack, string $context = ''): void
    {
        foreach ($needles as $index => $needle) {
            $this->assertContainsEquals(
                $needle,
                $haystack,
                ($context ? $context . ': ' : '') . 'Object not found in array.'
            );

            unset($haystack[$index]);
        }

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