Summer Sale! All accounts are 50% off this week.

dmag's avatar
Level 6

Assert key => value pair exists with the arrays of array

The following array is returned (not http call):

[
    [
        'order_id' => '123',
    ],
    [
        'order_id' => '124',
    ],
]

how do I assert that 'order_id' => '124' exists within the returned array?

0 likes
5 replies
AungHtetPaing__'s avatar

@dmag may be like this?

$array = [
    [
        'order_id' => '123',
    ],
    [
        'order_id' => '124',
    ],
];

$collection = collect($array)->pluck('order_id')->toArray(); // or collect($array)->flatten()->all();
dd(in_array('124', $collection));
1 like
Sinnbeck's avatar
if (collect($arr)->firstWhere('order_id', '123'))
1 like
dmag's avatar
Level 6

@Sinnbeck I was hoping there was phpunit specific assertion for that, which I wasn't aware of.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@dmag Not to my knowledge. But you can easily convert it to an assertion

$this->assertNotNull(collect($arr)->firstWhere('order_id', '123'));
1 like

Please or to participate in this conversation.