Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent, assert that collection contains item

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

Something like this:

$expected = factory::create(Item::class)->create();
$eloquentCollection = someData(); // Item::orderBy(...)->...->get();
$this->assertContains($expected, $eloquentCollection);
like image 633
Alex P. Avatar asked Dec 08 '16 12:12

Alex P.


1 Answers

You can use the contains method to assertTrue the test as:

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

You may 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));
like image 181
Amit Gupta Avatar answered Sep 22 '22 21:09

Amit Gupta