Skip to content

Commit

Permalink
Fail when an unsupported second argument is provided to has (#28)
Browse files Browse the repository at this point in the history
* Fail when an unsupported second argument is provided to `has`
  • Loading branch information
claudiodekker authored Jan 19, 2021
1 parent 042dbb2 commit b4a02f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Concerns/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use PHPUnit\Framework\Assert as PHPUnit;

trait Has
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit b4a02f0

Please sign in to comment.