From b4a02f0465fcb9d291a3065668f7ac8b73bfb1e7 Mon Sep 17 00:00:00 2001 From: Claudio Dekker <1752195+claudiodekker@users.noreply.github.com> Date: Tue, 19 Jan 2021 20:12:20 +0100 Subject: [PATCH] Fail when an unsupported second argument is provided to `has` (#28) * Fail when an unsupported second argument is provided to `has` --- src/Concerns/Has.php | 9 +++++---- tests/Unit/AssertTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Concerns/Has.php b/src/Concerns/Has.php index 941f680d..20891e5f 100644 --- a/src/Concerns/Has.php +++ b/src/Concerns/Has.php @@ -5,6 +5,7 @@ use Closure; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use InvalidArgumentException; use PHPUnit\Framework\Assert as PHPUnit; trait Has @@ -60,11 +61,11 @@ public function has(string $key, $value = null, Closure $scope = null): self } if (is_int($value)) { - return $this->count($key, $value); - } - - if (is_callable($value)) { + $this->count($key, $value); + } elseif (is_callable($value)) { $this->scope($key, $value); + } elseif (! is_null($value)) { + throw new InvalidArgumentException('The second argument of `has` is of an invalid type. Did you mean to use `where`?'); } return $this; diff --git a/tests/Unit/AssertTest.php b/tests/Unit/AssertTest.php index 37bbc254..56fb7726 100644 --- a/tests/Unit/AssertTest.php +++ b/tests/Unit/AssertTest.php @@ -10,6 +10,7 @@ use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Collection; use Inertia\Inertia; +use InvalidArgumentException; use PHPUnit\Framework\AssertionFailedError; class AssertTest extends TestCase @@ -292,6 +293,23 @@ public function it_cannot_count_the_amount_of_items_in_a_given_prop_when_the_pro }); } + /** @test */ + public function it_fails_when_the_second_argument_of_the_has_assertion_is_an_unsupported_type(): void + { + $response = $this->makeMockRequest( + Inertia::render('foo', [ + 'bar' => 'baz', + ]) + ); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('The second argument of `has` is of an invalid type. Did you mean to use `where`?'); + + $response->assertInertia(function (Assert $inertia) { + $inertia->has('bar', 'invalid'); + }); + } + /** @test */ public function it_asserts_that_a_prop_is_missing(): void {