Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add missing methods to AssertsStatusCodes #47277

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Illuminate/Testing/Concerns/AssertsStatusCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ public function assertFound()
return $this->assertStatus(302);
}

/**
* Assert that the response has a 304 "Not Modified" status code.
*
* @return $this
*/
public function assertNotModified()
{
return $this->assertStatus(304);
}

/**
* Assert that the response has a 307 "Temporary Redirect" status code.
*
* @return $this
*/
public function assertTemporaryRedirect()
{
return $this->assertStatus(307);
}

/**
* Assert that the response has a 308 "Permanent Redirect" status code.
*
* @return $this
*/
public function assertPermanentRedirect()
{
return $this->assertStatus(308);
}

/**
* Assert that the response has a 400 "Bad Request" status code.
*
Expand Down Expand Up @@ -131,6 +161,16 @@ public function assertMethodNotAllowed()
return $this->assertStatus(405);
}

/**
* Assert that the response has a 406 "Not Acceptable" status code.
*
* @return $this
*/
public function assertNotAcceptable()
{
return $this->assertStatus(406);
}

/**
* Assert that the response has a 408 "Request Timeout" status code.
*
Expand Down
76 changes: 76 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,25 @@ public function testAssertMethodNotAllowed()
$response->assertMethodNotAllowed();
}

public function testAssertNotAcceptable()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_NOT_ACCEPTABLE)
);

$response->assertNotAcceptable();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [406] but received 200.\nFailed asserting that 406 is identical to 200.");

$response->assertNotAcceptable();
$this->fail();
}

public function testAssertForbidden()
{
$statusCode = 500;
Expand Down Expand Up @@ -706,6 +725,63 @@ public function testAssertFound()
$this->fail();
}

public function testAssertNotModified()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_NOT_MODIFIED)
);

$response->assertNotModified();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [304] but received 200.\nFailed asserting that 304 is identical to 200.");

$response->assertNotModified();
$this->fail();
}

public function testAssertTemporaryRedirect()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_TEMPORARY_REDIRECT)
);

$response->assertTemporaryRedirect();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [307] but received 200.\nFailed asserting that 307 is identical to 200.");

$response->assertTemporaryRedirect();
$this->fail();
}

public function testAssertPermanentRedirect()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_PERMANENTLY_REDIRECT)
);

$response->assertPermanentRedirect();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [308] but received 200.\nFailed asserting that 308 is identical to 200.");

$response->assertPermanentRedirect();
$this->fail();
}

public function testAssertConflict()
{
$response = TestResponse::fromBaseResponse(
Expand Down