Skip to content

Commit cbdfde2

Browse files
committed
Add whereHas assertable JSON method
1 parent 41417ca commit cbdfde2

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

src/Illuminate/Testing/Fluent/Concerns/Matching.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,42 @@
99

1010
trait Matching
1111
{
12+
/**
13+
* Assets that all values exist and match their expected values.
14+
*
15+
* @param string $key
16+
* @param array|string $expected
17+
*
18+
* @return $this
19+
*/
20+
public function whereHas(string $key, $expected)
21+
{
22+
$actual = Collection::make(
23+
$this->prop($key) ?? $this->prop()
24+
);
25+
26+
$missing = Collection::make($expected)->reject(function ($search) use ($key, $actual) {
27+
if ($actual->containsStrict($key, $search)) {
28+
return true;
29+
}
30+
31+
return $actual->containsStrict($search);
32+
})->toArray();
33+
34+
$values = array_values($missing);
35+
36+
PHPUnit::assertEmpty(
37+
$missing,
38+
sprintf(
39+
'Property [%s] does not contain [%s].',
40+
$key,
41+
implode(', ', $values)
42+
)
43+
);
44+
45+
return $this;
46+
}
47+
1248
/**
1349
* Asserts that the property matches the expected value.
1450
*

tests/Testing/Fluent/AssertTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,109 @@ public function testAssertWhereFailsWhenDoesNotMatchValueUsingArrayable()
401401
]);
402402
}
403403

404+
public function testAssertWhereHasFailsWithEmptyValue()
405+
{
406+
$assert = AssertableJson::fromArray([]);
407+
408+
$this->expectException(AssertionFailedError::class);
409+
$this->expectExceptionMessage('Property [foo] does not contain [1].');
410+
411+
$assert->whereHas('foo', ['1']);
412+
}
413+
414+
public function testAssertWhereHasFailsWithMissingValue()
415+
{
416+
$assert = AssertableJson::fromArray([
417+
'foo' => ['bar', 'baz'],
418+
]);
419+
420+
$this->expectException(AssertionFailedError::class);
421+
$this->expectExceptionMessage('Property [foo] does not contain [invalid].');
422+
423+
$assert->whereHas('foo', ['bar', 'baz', 'invalid']);
424+
}
425+
426+
public function testAssertWhereHasFailsWithMissingNestedValue()
427+
{
428+
$assert = AssertableJson::fromArray([
429+
['id' => 1],
430+
['id' => 2],
431+
['id' => 3],
432+
['id' => 4],
433+
]);
434+
435+
$this->expectException(AssertionFailedError::class);
436+
$this->expectExceptionMessage('Property [id] does not contain [5].');
437+
438+
$assert->whereHas('id', [1,2,3,4,5]);
439+
}
440+
441+
public function testAssertWhereHasFailsWhenDoesNotMatchType()
442+
{
443+
$assert = AssertableJson::fromArray([
444+
'foo' => [1,2,3,4]
445+
]);
446+
447+
$this->expectException(AssertionFailedError::class);
448+
$this->expectExceptionMessage('Property [foo] does not contain [1].');
449+
450+
$assert->whereHas('foo', ['1']);
451+
}
452+
453+
public function testAssertWhereHasWithNestedValue()
454+
{
455+
$assert = AssertableJson::fromArray([
456+
['id' => 1],
457+
['id' => 2],
458+
['id' => 3],
459+
['id' => 4],
460+
]);
461+
462+
$assert->whereHas('id', 1);
463+
$assert->whereHas('id', [1,2,3,4]);
464+
$assert->whereHas('id', [4,3,2,1]);
465+
}
466+
467+
public function testAssertWhereHasWithMatchingType()
468+
{
469+
$assert = AssertableJson::fromArray([
470+
'foo' => [1,2,3,4]
471+
]);
472+
473+
$assert->whereHas('foo', 1);
474+
$assert->whereHas('foo', [1]);
475+
}
476+
477+
public function testAssertWhereHasWithNullValue()
478+
{
479+
$assert = AssertableJson::fromArray([
480+
'foo' => null,
481+
]);
482+
483+
$assert->whereHas('foo', null);
484+
$assert->whereHas('foo', [null]);
485+
}
486+
487+
public function testAssertWhereHasWithOutOfOrderMatchingType()
488+
{
489+
$assert = AssertableJson::fromArray([
490+
'foo' => [4,1,7,3]
491+
]);
492+
493+
$assert->whereHas('foo', [1,7,4,3]);
494+
}
495+
496+
public function testAssertWhereHasWithOutOfOrderNestedMatchingType()
497+
{
498+
$assert = AssertableJson::fromArray([
499+
['bar' => 5],
500+
['baz' => 4],
501+
['zal' => 8],
502+
]);
503+
504+
$assert->whereHas('baz', 4);
505+
}
506+
404507
public function testAssertNestedWhereMatchesValue()
405508
{
406509
$assert = AssertableJson::fromArray([

0 commit comments

Comments
 (0)