Skip to content

Commit

Permalink
[9.x] Extract status methods to traits (#45789)
Browse files Browse the repository at this point in the history
* Extract status assertions to trait

* Adds bad request status assertion

* Adds request timeout status assertion

* Adds payment required status assertion

* Adds accepted status assertion

* Adds moved permanently status code

* Adds found status assertion

* Adds conflict status assertion

* Adds too many requests status assertion

* Extracts status code checks to trait

* Adds created status check

* Adds accepted status check

* Adds no content status check

* Adds moved permanently status check

* Adds found status check

* Adds bad request status check

* Adds payment required status check

* Adds request timeout status check

* Adds conflict status check

* Adds unprocessable entity status check

* Adds too many requests status check

* Standardises dockblocks and formatting

* Fixes ordering and typo
  • Loading branch information
timacdonald authored Jan 25, 2023
1 parent 1a42e08 commit 8262d6d
Show file tree
Hide file tree
Showing 6 changed files with 631 additions and 117 deletions.
157 changes: 157 additions & 0 deletions src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Illuminate\Http\Client\Concerns;

trait DeterminesStatusCode
{
/**
* Determine if the response code was 200 "OK" response.
*
* @return bool
*/
public function ok()
{
return $this->status() === 200;
}

/**
* Determine if the response code was 201 "Created" response.
*
* @return bool
*/
public function created()
{
return $this->status() === 201;
}

/**
* Determine if the response code was 202 "Accepted" response.
*
* @return bool
*/
public function accepted()
{
return $this->status() === 202;
}

/**
* Determine if the response code was the given status code and the body has no content.
*
* @param int $status
* @return bool
*/
public function noContent($status = 204)
{
return $this->status() === $status && $this->body() === '';
}

/**
* Determine if the response code was a 301 "Moved Permanently".
*
* @return bool
*/
public function movedPermanently()
{
return $this->status() === 301;
}

/**
* Determine if the response code was a 302 "Found" response.
*
* @return bool
*/
public function found()
{
return $this->status() === 302;
}

/**
* Determine if the response was a 400 "Bad Request" response.
*
* @return bool
*/
public function badRequest()
{
return $this->status() === 400;
}

/**
* Determine if the response was a 401 "Unauthorized" response.
*
* @return bool
*/
public function unauthorized()
{
return $this->status() === 401;
}

/**
* Determine if the response was a 402 "Payment Required" response.
*
* @return bool
*/
public function paymentRequired()
{
return $this->status() === 402;
}

/**
* Determine if the response was a 403 "Forbidden" response.
*
* @return bool
*/
public function forbidden()
{
return $this->status() === 403;
}

/**
* Determine if the response was a 404 "Not Found" response.
*
* @return bool
*/
public function notFound()
{
return $this->status() === 404;
}

/**
* Determine if the response was a 408 "Request Timeout" response.
*
* @return bool
*/
public function requestTimeout()
{
return $this->status() === 408;
}

/**
* Determine if the response was a 409 "Conflict" response.
*
* @return bool
*/
public function conflict()
{
return $this->status() === 409;
}

/**
* Determine if the response was a 422 "Unprocessable Entity" response.
*
* @return bool
*/
public function unprocessableEntity()
{
return $this->status() === 422;
}

/**
* Determine if the response was a 429 "Too Many Requests" response.
*
* @return bool
*/
public function tooManyRequests()
{
return $this->status() === 429;
}
}
42 changes: 1 addition & 41 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Response implements ArrayAccess
{
use Macroable {
use Concerns\DeterminesStatusCode, Macroable {
__call as macroCall;
}

Expand Down Expand Up @@ -164,16 +164,6 @@ public function successful()
return $this->status() >= 200 && $this->status() < 300;
}

/**
* Determine if the response code was "OK".
*
* @return bool
*/
public function ok()
{
return $this->status() === 200;
}

/**
* Determine if the response was a redirect.
*
Expand All @@ -184,36 +174,6 @@ public function redirect()
return $this->status() >= 300 && $this->status() < 400;
}

/**
* Determine if the response was a 401 "Unauthorized" response.
*
* @return bool
*/
public function unauthorized()
{
return $this->status() === 401;
}

/**
* Determine if the response was a 403 "Forbidden" response.
*
* @return bool
*/
public function forbidden()
{
return $this->status() === 403;
}

/**
* Determine if the response was a 404 "Not Found" response.
*
* @return bool
*/
public function notFound()
{
return $this->status() === 404;
}

/**
* Determine if the response indicates a client or server error occurred.
*
Expand Down
165 changes: 165 additions & 0 deletions src/Illuminate/Testing/Concerns/AssertsStatusCodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Illuminate\Testing\Concerns;

use Illuminate\Testing\Assert as PHPUnit;

trait AssertsStatusCodes
{
/**
* Assert that the response has a 200 "OK" status code.
*
* @return $this
*/
public function assertOk()
{
return $this->assertStatus(200);
}

/**
* Assert that the response has a 201 "Created" status code.
*
* @return $this
*/
public function assertCreated()
{
return $this->assertStatus(201);
}

/**
* Assert that the response has a 202 "Accepted" status code.
*
* @return $this
*/
public function assertAccepted()
{
return $this->assertStatus(202);
}

/**
* Assert that the response has the given status code and no content.
*
* @param int $status
* @return $this
*/
public function assertNoContent($status = 204)
{
$this->assertStatus($status);

PHPUnit::assertEmpty($this->getContent(), 'Response content is not empty.');

return $this;
}

/**
* Assert that the response has a 301 "Moved Permanently" status code.
*
* @param int $status
* @return $this
*/
public function assertMovedPermanently()
{
return $this->assertStatus(301);
}

/**
* Assert that the response has a 302 "Found" status code.
*
* @param int $status
* @return $this
*/
public function assertFound()
{
return $this->assertStatus(302);
}

/**
* Assert that the response has a 400 "Bad Request" status code.
*
* @return $this
*/
public function assertBadRequest()
{
return $this->assertStatus(400);
}

/**
* Assert that the response has a 402 "Unauthorized" status code.
*
* @return $this
*/
public function assertUnauthorized()
{
return $this->assertStatus(401);
}

/**
* Assert that the response has a 402 "Payment Required" status code.
*
* @return $this
*/
public function assertPaymentRequired()
{
return $this->assertStatus(402);
}

/**
* Assert that the response has a 403 "Forbidden" status code.
*
* @return $this
*/
public function assertForbidden()
{
return $this->assertStatus(403);
}

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

/**
* Assert that the response has a 408 "Request Timeout" status code.
*
* @return $this
*/
public function assertRequestTimeout()
{
return $this->assertStatus(408);
}

/**
* Assert that the response has a 409 "Conflict" status code.
*
* @return $this
*/
public function assertConflict()
{
return $this->assertStatus(409);
}

/**
* Assert that the response has a 422 "Unprocessable Entity" status code.
*
* @return $this
*/
public function assertUnprocessable()
{
return $this->assertStatus(422);
}

/**
* Assert that the response has a 429 "Too Many Requests" status code.
*
* @return $this
*/
public function assertTooManyRequests()
{
return $this->assertStatus(429);
}
}
Loading

0 comments on commit 8262d6d

Please sign in to comment.