Skip to content

Commit db6b39b

Browse files
authored
Add isRedirect assertion for responses in the 3xx range (#17)
1 parent 0475009 commit db6b39b

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ assertThat($response, hasStatus(logicalAnd(greaterThanOrEqual(200), lessThan(400
188188
For the most common checks, some shorthand assertions are available:
189189

190190
- `assertResponseIsSuccess($response)` / `isSuccess()` -- Status codes 200 to 299
191+
- `assertResponseIsRedirect($response)` / `isRedirect()` -- Status codes 300 to 399
191192
- `assertResponseIsClientError($response)` / `isClientError()` -- Status codes 400 to 499
192193
- `assertResponseIsServerError($response)` / `isServerError()` -- Status codes 500 to 599
193194

src/Functions.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ function isSuccess(): Constraint
4545
return Psr7Assertions::isSuccess();
4646
}
4747

48+
function isRedirect(): Constraint
49+
{
50+
return Psr7Assertions::isRedirect();
51+
}
52+
4853
function isClientError(): Constraint
4954
{
5055
return Psr7Assertions::isClientError();
@@ -114,4 +119,4 @@ function bodyMatchesForm(array $constraints): Constraint
114119
function isAbsoluteUri(): Constraint
115120
{
116121
return new IsAbsoluteUriConstraint();
117-
}
122+
}

src/Psr7Assertions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public static function assertResponseIsSuccess(ResponseInterface $response): voi
6363
Assert::assertThat($response, static::isSuccess());
6464
}
6565

66+
public static function assertResponseIsRedirect(ResponseInterface $response): void
67+
{
68+
Assert::assertThat($response, static::isRedirect());
69+
}
70+
6671
public static function assertResponseIsClientError(ResponseInterface $response): void
6772
{
6873
Assert::assertThat($response, static::isClientError());
@@ -141,6 +146,11 @@ public static function isSuccess(): Constraint
141146
return new HasStatusConstraint(Assert::logicalAnd(Assert::greaterThanOrEqual(200), Assert::lessThan(300)));
142147
}
143148

149+
public static function isRedirect(): Constraint
150+
{
151+
return new HasStatusConstraint(Assert::logicalAnd(Assert::greaterThanOrEqual(300), Assert::lessThan(400)));
152+
}
153+
144154
public static function isClientError(): Constraint
145155
{
146156
return new HasStatusConstraint(Assert::logicalAnd(Assert::greaterThanOrEqual(400), Assert::lessThan(500)));

tests/Functional/ConstraintTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ public function testIsSuccessCanFail()
209209
$this->assertResponseIsSuccess(new Response(404));
210210
}
211211

212+
public function testIsRedirectCanSucceed()
213+
{
214+
$this->assertResponseIsRedirect(new Response(300));
215+
}
216+
217+
public function testIsRedirectCanFail()
218+
{
219+
$this->expectException(AssertionFailedError::class);
220+
221+
$this->assertResponseIsRedirect(new Response(200));
222+
}
223+
212224
public function testIsClientErrorCanSucceed()
213225
{
214226
$this->assertResponseIsClientError(new Response(404));

0 commit comments

Comments
 (0)