我正在寻找将对象添加到新集合中,因为我循环了一系列不同的结果.
查询:
$osRed = Item::where('category', 'Hardware')
->where(function ($query) {
$query->where('operating_system', 'xxx')
->orWhere('operating_system', 'xxx')
->orWhere('operating_system', 'xxx');
})
->orderBy('operating_system', 'ASC')
->get();
Run Code Online (Sandbox Code Playgroud)
然后,循环遍历这些结果并将相关对象添加到新集合中.
foreach ($osRed as $os) {
foreach ($os->services as $service) {
if (!($servicesImpacted->contains($service))) {
$servicesImpacted->push($service);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我继续进行另一组结果,并将这些结果中的相关服务添加到集合中.
但是,它并没有意识到对象已经在集合中,并且最终会出现大量(似乎是重复的)重复项.
理想情况下,我想匹配$ service对象的name属性,但contains方法似乎不支持.
toString() must not throw an exception
Run Code Online (Sandbox Code Playgroud) 我对我的应用程序中的大多数模型进行了以下类型的测试:
* @test
*/
public function an_authorised_user_can_delete_a_contact()
{
$contact = Contact::factory()->create();
$this->assertDatabaseHas('contacts', $contact->getAttributes());
$response = $this->actingAs($this->user_delete)
->delete('/contacts/'.$contact->id);
$this->assertSoftDeleted('contacts', $contact->getAttributes());
$response->assertRedirect('contacts');
}
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下都运行良好,但有时会由于时间戳稍有偏差而失败。
无法断言表 [联系人] 中的任何软删除行与属性 {"first_name":"Katherine","last_name":"Will","title":"Jewelry Model OR Mold Makers","telephone":" 匹配+15127653255","手机":"+19366193055","电子邮件":"lucy.lind@example.net","vendor_id":1,"updated_at":"2022-04-04 18:09:50", “created_at”:“2022-04-04 18:09:50”,“id”:1}。
找到:[ {“id”:1,“first_name”:“Katherine”,“last_name”:“Will”,“title”:“珠宝模型或模具制造商”,“电话”:“+15127653255”,“手机” :“+19366193055”,“电子邮件”:“lucy.lind@example.net”,“vendor_id”:1,“created_at”:“2022-04-04 18:09:50”,“updated_at”:“2022- 04-04 18:09:51", "deleted_at": "2022-04-04 18:09:51" } ]
区别在于updated_at
- “2022-04-04 18:09:50”与“2022-04-04 18:09:51”。
是否有更好的方法来构建测试以使其更加稳健?