Skip to content

Commit

Permalink
Add assertPathExists assertion (#146)
Browse files Browse the repository at this point in the history
This assertion is a convient way to only check whether an endpoint is in
the spec or not.

Co-authored-by: Alexander Lentner <[email protected]>
  • Loading branch information
lentex and Alexander Lentner authored Jul 28, 2023
1 parent f1e369c commit 07bef2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ public function assertErrorsContain()
});
}

public function assertPathExists()
{
return fn () => $this->runAssertion(function () {
$exception = app('spectator')->requestException;

$this->expectsFalse($exception, [
InvalidPathException::class,
]);

return $this;
});
}

public function dumpSpecErrors()
{
return function () {
Expand Down
20 changes: 20 additions & 0 deletions tests/AssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,24 @@ public function test_request_assertion_does_not_format_laravel_validation_respon
->assertValidRequest()
->assertValidResponse(422);
}

public function test_asserts_path_exists()
{
Route::get('/users')->middleware(Middleware::class);

$this->getJson('/users')
->assertPathExists();
}

public function test_asserts_path_does_not_exist()
{
$this->expectException(\ErrorException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Path [GET /invalid] not found in spec.');

Route::get('/invalid')->middleware(Middleware::class);

$this->getJson('/invalid')
->assertPathExists();
}
}

0 comments on commit 07bef2f

Please sign in to comment.