Skip to content

Commit

Permalink
Give Http Code assertion more priority in response assertion, use Lar…
Browse files Browse the repository at this point in the history
…avel built-in assertion
  • Loading branch information
bastien-phi committed Feb 1, 2024
1 parent 9d7f4c1 commit 042e637
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
26 changes: 8 additions & 18 deletions src/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function assertInvalidRequest()
public function assertValidResponse()
{
return fn ($status = null) => $this->runAssertion(function () use ($status) {
if ($status) {
$this->assertStatus($status);
}

$exception = app('spectator')->responseException;

$this->expectsFalse($exception, [
Expand All @@ -75,22 +79,17 @@ public function assertValidResponse()
UnresolvableReferenceException::class,
]);

if ($status) {
$actual = $this->getStatusCode();

PHPUnit::assertTrue(
$actual === $status,
"Expected status code {$status} but received {$actual}."
);
}

return $this;
});
}

public function assertInvalidResponse()
{
return fn ($status = null) => $this->runAssertion(function () use ($status) {
if ($status) {
$this->assertStatus($status);
}

$exception = app('spectator')->responseException;

$this->expectsFalse($exception, [
Expand All @@ -105,15 +104,6 @@ public function assertInvalidResponse()
ResponseValidationException::class,
], 'Failed asserting that the response is invalid.');

if ($status) {
$actual = $this->getStatusCode();

PHPUnit::assertTrue(
$actual === $status,
"Expected status code {$status} but received {$actual}."
);
}

return $this;
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/AssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function test_exception_points_to_mixin_method()

$this->expectException(ErrorException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('No response object matching returned status code [500].');
$this->expectExceptionMessage('Expected response status code [200] but received 500.');

Route::get('/users', function () {
throw new \Exception('Explosion');
Expand Down
20 changes: 20 additions & 0 deletions tests/ResponseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ public function test_validates_invalid_empty_response(): void
->assertValidationMessage('Response body is expected to be empty.');
}

public function test_validates_status_code(): void
{
$this->expectException(ErrorException::class);
$this->expectExceptionMessage('Expected response status code [200] but received 201.');

Route::get('/users', static function () {
return response([
[
'id' => 1,
'name' => 'Jim',
'email' => '[email protected]',
],
], 201);
})->middleware(Middleware::class);

$this->getJson('/users')
->assertValidRequest()
->assertValidResponse(200);
}

public function test_validates_valid_problem_json_response()
{
Route::get('/users', function () {
Expand Down

0 comments on commit 042e637

Please sign in to comment.