Skip to content

Commit

Permalink
Add new methods to AssertsStatusCodes (#47277)
Browse files Browse the repository at this point in the history
Co-authored-by: Volodya Khurshudyan <volodya.khurshudyan@softconstruct.com>
  • Loading branch information
xurshudyan and Volodya Khurshudyan committed May 30, 2023
1 parent 20b309a commit 8e71667
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
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

0 comments on commit 8e71667

Please sign in to comment.