-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Extract status methods to traits (#45789)
* 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
1 parent
1a42e08
commit 8262d6d
Showing
6 changed files
with
631 additions
and
117 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.