如何测试数组仅包含带有phpunit的对象?
我正在寻找在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
assertContainSequals
方法来执行此操作:如果您打算更频繁地执行断言,也可以创建自己的自信方法:
You can do this using the
assertContainsEquals
method like this:You could also a create your own assert method if you intend to perform the assertion more often:
基于 @roj vreemen 的答案我实现了此解决方案以确切匹配:
Based on @Roj Vroemen's answer I implemented this solution for exact match asserting: