Laravel Eloquent, claim the collection contains an item

How to assert (in the PHPUnit test) that the Eloquent collection contains an element?

Something like that:

$expected = factory::create(Item::class)->create(); $eloquentCollection = someData(); // Item::orderBy(...)->...->get(); $this->assertContains($expected, $eloquentCollection); 
+6
source share
1 answer

You can use the contains method for assertTrue test like:

 $this->assertTrue($eloquentCollection->contains($expected)); 

You can also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:

 $this->assertTrue($eloquentCollection->contains('id', $expected->id)); 
+7
source

Source: https://habr.com/ru/post/1012995/


All Articles