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

[9.x] Extract status methods to traits #45789

Merged
merged 23 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f7584d3
Extract status assertions to trait
timacdonald Jan 24, 2023
0e4748f
Adds bad request status assertion
timacdonald Jan 24, 2023
4c749e8
Adds request timeout status assertion
timacdonald Jan 24, 2023
7b3f067
Adds payment required status assertion
timacdonald Jan 24, 2023
4c3c8cf
Adds accepted status assertion
timacdonald Jan 24, 2023
4140203
Adds moved permanently status code
timacdonald Jan 24, 2023
3e4bc0a
Adds found status assertion
timacdonald Jan 24, 2023
c522830
Adds conflict status assertion
timacdonald Jan 25, 2023
ee6432a
Adds too many requests status assertion
timacdonald Jan 25, 2023
36836be
Extracts status code checks to trait
timacdonald Jan 25, 2023
bd6d709
Adds created status check
timacdonald Jan 25, 2023
66f42e7
Adds accepted status check
timacdonald Jan 25, 2023
dfee998
Adds no content status check
timacdonald Jan 25, 2023
e1e0fdd
Adds moved permanently status check
timacdonald Jan 25, 2023
56df5fe
Adds found status check
timacdonald Jan 25, 2023
cd8554c
Adds bad request status check
timacdonald Jan 25, 2023
74ea92f
Adds payment required status check
timacdonald Jan 25, 2023
4648a07
Adds request timeout status check
timacdonald Jan 25, 2023
8572623
Adds conflict status check
timacdonald Jan 25, 2023
c8b5916
Adds unprocessable entity status check
timacdonald Jan 25, 2023
c90430c
Adds too many requests status check
timacdonald Jan 25, 2023
da8b4f2
Standardises dockblocks and formatting
timacdonald Jan 25, 2023
9bf87f7
Fixes ordering and typo
timacdonald Jan 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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