From b91ebc1597b11af65ad07f948ebed686cfbf85ec Mon Sep 17 00:00:00 2001 From: Joao Cardoso Date: Fri, 19 Jul 2024 17:19:39 +0200 Subject: [PATCH 1/6] Add more Symfony response assertions to ResponseAssertions trait --- src/Symfony/ResponseAssertions.php | 47 ++++++++ tests/Unit/Symfony/ResponseAssertionsTest.php | 104 +++++++++++++++++- 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/src/Symfony/ResponseAssertions.php b/src/Symfony/ResponseAssertions.php index 256be2d..ca34afe 100644 --- a/src/Symfony/ResponseAssertions.php +++ b/src/Symfony/ResponseAssertions.php @@ -20,4 +20,51 @@ protected static function assertJsonResponse(array $expected, JsonResponse $resp $content = json_decode($response->getContent(), true); Assert::assertSame($expected, $content); } + + protected static function assertResponse(Response $response, int $expectedStatusCode, ?string $expectedMessage = null): void + { + self::assertStatusCode($response, $expectedStatusCode); + + if ($expectedMessage !== null) { + self::assertResponseMessage($response, $expectedMessage); + } + } + + protected static function assertResponseIsSuccessful(Response $response, ?string $expectedMessage = null): void + { + self::assertResponse($response, Response::HTTP_OK, $expectedMessage); + } + + protected static function assertResponseIsRedirect(Response $response, ?string $expectedMessage = null): void + { + self::assertResponse($response, Response::HTTP_FOUND, $expectedMessage); + } + + protected static function assertResponseIsClientError(Response $response, ?string $expectedMessage = null): void + { + self::assertResponse($response, Response::HTTP_BAD_REQUEST, $expectedMessage); + } + + protected static function assertResponseIsServerError(Response $response, ?string $expectedMessage = null): void + { + self::assertResponse($response, Response::HTTP_INTERNAL_SERVER_ERROR, $expectedMessage); + } + + private static function assertStatusCode(Response $response, int $expectedStatusCode): void + { + Assert::assertEquals( + $expectedStatusCode, + $response->getStatusCode(), + sprintf('Expected status code %d but got %d.', $expectedStatusCode, $response->getStatusCode()) + ); + } + + private static function assertResponseMessage(Response $response, string $expectedMessage): void + { + Assert::assertSame( + $expectedMessage, + $response->getContent(), + sprintf('Expected response message to contain "%s".', $expectedMessage) + ); + } } diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php index a6d6e79..6179cd5 100644 --- a/tests/Unit/Symfony/ResponseAssertionsTest.php +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -6,12 +6,13 @@ use DR\PHPUnitExtensions\Symfony\ResponseAssertions; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; -/** - * @covers \DR\PHPUnitExtensions\Symfony\ResponseAssertions - */ +#[CoversClass(ResponseAssertions::class)] class ResponseAssertionsTest extends TestCase { use ResponseAssertions; @@ -31,6 +32,7 @@ public function testAssertJsonResponseFails(): void $this->expectException(AssertionFailedError::class); $this->expectExceptionMessage('Failed asserting that two arrays are identical.'); + self::assertJsonResponse($expected, $response); } @@ -41,6 +43,102 @@ public function testAssertJsonResponseFalse(): void $this->expectException(AssertionFailedError::class); $this->expectExceptionMessage('Failed asserting that false is identical to Array'); + self::assertJsonResponse($expected, $response); } + + #[TestWith([200, true])] + #[TestWith([404, false])] + public function testAssertStatusCode(int $statusCode, bool $shouldPass): void + { + $response = new Response('', 200); + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + self::assertStatusCode($response, $statusCode); + } + + #[TestWith(['This is a test message', true])] + #[TestWith(['Different message', false])] + public function testAssertResponseMessage(string $messageContent, bool $shouldPass): void + { + $response = new Response($messageContent); + $expectedMessage = 'This is a test message'; + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + self::assertResponseMessage($response, $expectedMessage); + } + + #[TestWith([200, 'This is a test message', true])] + #[TestWith([404, 'This is a test message', false])] + public function testAssertResponseDifferentCode(int $statusCode, ?string $messageContent, bool $shouldPass): void + { + $response = new Response('This is a test message', 200); + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + + self::assertResponse($response, $statusCode, $messageContent); + } + + #[TestWith([200, null, true])] + #[TestWith([200, 'Expected message', true])] + #[TestWith([404, null, false])] + #[TestWith([200, 'Unexpected message', false])] + public function testAssertResponseIsSuccessful(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + { + $response = new Response('Expected message', $statusCode); + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + + self::assertResponseIsSuccessful($response, $expectedMessage); + } + + #[TestWith([302, null, true])] + #[TestWith([302, 'Expected message', true])] + #[TestWith([200, null, false])] + #[TestWith([302, 'Unexpected message', false])] + public function testAssertResponseIsRedirect(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + { + $response = new Response('Expected message', $statusCode); + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + self::assertResponseIsRedirect($response, $expectedMessage); + } + + #[TestWith([400, null, true])] + #[TestWith([400, 'Expected message', true])] + #[TestWith([200, null, false])] + #[TestWith([400, 'Unexpected message', false])] + public function testAssertResponseIsClientError(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + { + $response = new Response('Expected message', $statusCode); + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + self::assertResponseIsClientError($response, $expectedMessage); + } + + #[TestWith([500, null, true])] + #[TestWith([500, 'Expected message', true])] + #[TestWith([200, null, false])] + #[TestWith([500, 'Unexpected message', false])] + public function testAssertResponseIsServerError(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + { + $response = new Response('Expected message', $statusCode); + + if ($shouldPass === false) { + $this->expectException(AssertionFailedError::class); + } + self::assertResponseIsServerError($response, $expectedMessage); + } } From 4a4e64730df1868af41fc9b42177358db1ee3f5d Mon Sep 17 00:00:00 2001 From: Joao Cardoso Date: Fri, 19 Jul 2024 17:27:57 +0200 Subject: [PATCH 2/6] Add more Symfony response assertions to ResponseAssertions trait --- tests/Unit/Symfony/ResponseAssertionsTest.php | 113 ++++++++++++++---- 1 file changed, 89 insertions(+), 24 deletions(-) diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php index 6179cd5..b9e74da 100644 --- a/tests/Unit/Symfony/ResponseAssertionsTest.php +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -12,7 +12,9 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; -#[CoversClass(ResponseAssertions::class)] +/** + * @covers \DR\PHPUnitExtensions\Symfony\ResponseAssertions + */ class ResponseAssertionsTest extends TestCase { use ResponseAssertions; @@ -47,8 +49,9 @@ public function testAssertJsonResponseFalse(): void self::assertJsonResponse($expected, $response); } - #[TestWith([200, true])] - #[TestWith([404, false])] + /** + * @dataProvider statusCodeProvider + */ public function testAssertStatusCode(int $statusCode, bool $shouldPass): void { $response = new Response('', 200); @@ -59,11 +62,20 @@ public function testAssertStatusCode(int $statusCode, bool $shouldPass): void self::assertStatusCode($response, $statusCode); } - #[TestWith(['This is a test message', true])] - #[TestWith(['Different message', false])] + public static function statusCodeProvider(): array + { + return [ + [200, true], + [404, false], + ]; + } + + /** + * @dataProvider responseMessageProvider + */ public function testAssertResponseMessage(string $messageContent, bool $shouldPass): void { - $response = new Response($messageContent); + $response = new Response($messageContent); $expectedMessage = 'This is a test message'; if ($shouldPass === false) { @@ -72,8 +84,17 @@ public function testAssertResponseMessage(string $messageContent, bool $shouldPa self::assertResponseMessage($response, $expectedMessage); } - #[TestWith([200, 'This is a test message', true])] - #[TestWith([404, 'This is a test message', false])] + public static function responseMessageProvider(): array + { + return [ + ['This is a test message', true], + ['Different message', false], + ]; + } + + /** + * @dataProvider assertResponseProvider + */ public function testAssertResponseDifferentCode(int $statusCode, ?string $messageContent, bool $shouldPass): void { $response = new Response('This is a test message', 200); @@ -85,10 +106,17 @@ public function testAssertResponseDifferentCode(int $statusCode, ?string $messag self::assertResponse($response, $statusCode, $messageContent); } - #[TestWith([200, null, true])] - #[TestWith([200, 'Expected message', true])] - #[TestWith([404, null, false])] - #[TestWith([200, 'Unexpected message', false])] + public static function assertResponseProvider(): array + { + return [ + [200, 'This is a test message', true], + [404, 'This is a test message', false], + ]; + } + + /** + * @dataProvider responseIsSuccessfulProvider + */ public function testAssertResponseIsSuccessful(int $statusCode, ?string $expectedMessage, bool $shouldPass): void { $response = new Response('Expected message', $statusCode); @@ -100,10 +128,19 @@ public function testAssertResponseIsSuccessful(int $statusCode, ?string $expecte self::assertResponseIsSuccessful($response, $expectedMessage); } - #[TestWith([302, null, true])] - #[TestWith([302, 'Expected message', true])] - #[TestWith([200, null, false])] - #[TestWith([302, 'Unexpected message', false])] + public static function responseIsSuccessfulProvider(): array + { + return [ + [200, null, true], + [200, 'Expected message', true], + [404, null, false], + [200, 'Unexpected message', false], + ]; + } + + /** + * @dataProvider responseIsRedirectProvider + */ public function testAssertResponseIsRedirect(int $statusCode, ?string $expectedMessage, bool $shouldPass): void { $response = new Response('Expected message', $statusCode); @@ -114,10 +151,19 @@ public function testAssertResponseIsRedirect(int $statusCode, ?string $expectedM self::assertResponseIsRedirect($response, $expectedMessage); } - #[TestWith([400, null, true])] - #[TestWith([400, 'Expected message', true])] - #[TestWith([200, null, false])] - #[TestWith([400, 'Unexpected message', false])] + public static function responseIsRedirectProvider(): array + { + return [ + [302, null, true], + [302, 'Expected message', true], + [200, null, false], + [302, 'Unexpected message', false], + ]; + } + + /** + * @dataProvider responseIsClientErrorProvider + */ public function testAssertResponseIsClientError(int $statusCode, ?string $expectedMessage, bool $shouldPass): void { $response = new Response('Expected message', $statusCode); @@ -128,10 +174,19 @@ public function testAssertResponseIsClientError(int $statusCode, ?string $expect self::assertResponseIsClientError($response, $expectedMessage); } - #[TestWith([500, null, true])] - #[TestWith([500, 'Expected message', true])] - #[TestWith([200, null, false])] - #[TestWith([500, 'Unexpected message', false])] + public static function responseIsClientErrorProvider(): array + { + return [ + [400, null, true], + [400, 'Expected message', true], + [200, null, false], + [400, 'Unexpected message', false], + ]; + } + + /** + * @dataProvider responseIsServerErrorProvider + */ public function testAssertResponseIsServerError(int $statusCode, ?string $expectedMessage, bool $shouldPass): void { $response = new Response('Expected message', $statusCode); @@ -141,4 +196,14 @@ public function testAssertResponseIsServerError(int $statusCode, ?string $expect } self::assertResponseIsServerError($response, $expectedMessage); } + + public static function responseIsServerErrorProvider(): array + { + return [ + [500, null, true], + [500, 'Expected message', true], + [200, null, false], + [500, 'Unexpected message', false], + ]; + } } From 20c38abd85e814571943fdf80d5cd7c24660560e Mon Sep 17 00:00:00 2001 From: Joao Cardoso Date: Fri, 19 Jul 2024 17:36:09 +0200 Subject: [PATCH 3/6] Add more Symfony response assertions to ResponseAssertions trait --- tests/Unit/Symfony/ResponseAssertionsTest.php | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php index b9e74da..c5a2418 100644 --- a/tests/Unit/Symfony/ResponseAssertionsTest.php +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -6,8 +6,6 @@ use DR\PHPUnitExtensions\Symfony\ResponseAssertions; use PHPUnit\Framework\AssertionFailedError; -use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -62,6 +60,9 @@ public function testAssertStatusCode(int $statusCode, bool $shouldPass): void self::assertStatusCode($response, $statusCode); } + /** + * @return array + */ public static function statusCodeProvider(): array { return [ @@ -84,6 +85,9 @@ public function testAssertResponseMessage(string $messageContent, bool $shouldPa self::assertResponseMessage($response, $expectedMessage); } + /** + * @return array + */ public static function responseMessageProvider(): array { return [ @@ -106,6 +110,9 @@ public function testAssertResponseDifferentCode(int $statusCode, ?string $messag self::assertResponse($response, $statusCode, $messageContent); } + /** + * @return array + */ public static function assertResponseProvider(): array { return [ @@ -128,6 +135,9 @@ public function testAssertResponseIsSuccessful(int $statusCode, ?string $expecte self::assertResponseIsSuccessful($response, $expectedMessage); } + /** + * @return array + */ public static function responseIsSuccessfulProvider(): array { return [ @@ -151,6 +161,9 @@ public function testAssertResponseIsRedirect(int $statusCode, ?string $expectedM self::assertResponseIsRedirect($response, $expectedMessage); } + /** + * @return array + */ public static function responseIsRedirectProvider(): array { return [ @@ -174,6 +187,9 @@ public function testAssertResponseIsClientError(int $statusCode, ?string $expect self::assertResponseIsClientError($response, $expectedMessage); } + /** + * @return array + */ public static function responseIsClientErrorProvider(): array { return [ @@ -197,6 +213,9 @@ public function testAssertResponseIsServerError(int $statusCode, ?string $expect self::assertResponseIsServerError($response, $expectedMessage); } + /** + * @return array + */ public static function responseIsServerErrorProvider(): array { return [ From e7513d68ff6bc41d8dced25ce48c275d2f45fc0a Mon Sep 17 00:00:00 2001 From: Joao Cardoso Date: Fri, 19 Jul 2024 20:03:48 +0200 Subject: [PATCH 4/6] Add more Symfony response assertions to ResponseAssertions trait --- README.md | 12 ++++++++++++ src/Symfony/ResponseAssertions.php | 12 ++++-------- tests/Unit/Symfony/ResponseAssertionsTest.php | 8 ++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0e8ed8d..6f778cd 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,18 @@ class MyConstraintValidatorTest extends AbstractConstraintValidatorTestCase - `expectAtPath` - `expectAddViolation` +### ResponseAssertions trait +The ResponseAssertions trait provides a set of assertion methods designed to streamline the testing of Symfony HTTP responses. +This trait includes methods for verifying the status code, response message content and specific types of responses such as JSON responses + +**Methods** +- `assertJsonResponse` +- `assertResponse` +- `assertResponseIsSuccessful` +- `assertResponseIsRedirect` +- `assertResponseIsBadRequest` +- `assertResponseIsServerError` + ## About us At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP diff --git a/src/Symfony/ResponseAssertions.php b/src/Symfony/ResponseAssertions.php index ca34afe..c64d289 100644 --- a/src/Symfony/ResponseAssertions.php +++ b/src/Symfony/ResponseAssertions.php @@ -37,10 +37,10 @@ protected static function assertResponseIsSuccessful(Response $response, ?string protected static function assertResponseIsRedirect(Response $response, ?string $expectedMessage = null): void { - self::assertResponse($response, Response::HTTP_FOUND, $expectedMessage); + self::assertResponse($response, Response::HTTP_MOVED_PERMANENTLY, $expectedMessage); } - protected static function assertResponseIsClientError(Response $response, ?string $expectedMessage = null): void + protected static function assertResponseIsBadRequest(Response $response, ?string $expectedMessage = null): void { self::assertResponse($response, Response::HTTP_BAD_REQUEST, $expectedMessage); } @@ -52,7 +52,7 @@ protected static function assertResponseIsServerError(Response $response, ?strin private static function assertStatusCode(Response $response, int $expectedStatusCode): void { - Assert::assertEquals( + Assert::assertSame( $expectedStatusCode, $response->getStatusCode(), sprintf('Expected status code %d but got %d.', $expectedStatusCode, $response->getStatusCode()) @@ -61,10 +61,6 @@ private static function assertStatusCode(Response $response, int $expectedStatus private static function assertResponseMessage(Response $response, string $expectedMessage): void { - Assert::assertSame( - $expectedMessage, - $response->getContent(), - sprintf('Expected response message to contain "%s".', $expectedMessage) - ); + Assert::assertSame($expectedMessage, $response->getContent()); } } diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php index c5a2418..93d8bfb 100644 --- a/tests/Unit/Symfony/ResponseAssertionsTest.php +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -175,22 +175,22 @@ public static function responseIsRedirectProvider(): array } /** - * @dataProvider responseIsClientErrorProvider + * @dataProvider responseIsBadRequestProvider */ - public function testAssertResponseIsClientError(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + public function testAssertResponseIsBadRequest(int $statusCode, ?string $expectedMessage, bool $shouldPass): void { $response = new Response('Expected message', $statusCode); if ($shouldPass === false) { $this->expectException(AssertionFailedError::class); } - self::assertResponseIsClientError($response, $expectedMessage); + self::assertResponseIsBadRequest($response, $expectedMessage); } /** * @return array */ - public static function responseIsClientErrorProvider(): array + public static function responseIsBadRequestProvider(): array { return [ [400, null, true], From 5ed091a32eaf95bacfba02f48064613431847e5b Mon Sep 17 00:00:00 2001 From: Joao Cardoso Date: Fri, 19 Jul 2024 20:15:40 +0200 Subject: [PATCH 5/6] Add more Symfony response assertions to ResponseAssertions trait --- tests/Unit/Symfony/ResponseAssertionsTest.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php index 93d8bfb..12728b3 100644 --- a/tests/Unit/Symfony/ResponseAssertionsTest.php +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -52,7 +52,7 @@ public function testAssertJsonResponseFalse(): void */ public function testAssertStatusCode(int $statusCode, bool $shouldPass): void { - $response = new Response('', 200); + $response = new Response('', Response::HTTP_OK); if ($shouldPass === false) { $this->expectException(AssertionFailedError::class); @@ -66,8 +66,8 @@ public function testAssertStatusCode(int $statusCode, bool $shouldPass): void public static function statusCodeProvider(): array { return [ - [200, true], - [404, false], + [Response::HTTP_OK, true], + [Response::HTTP_NOT_FOUND, false], ]; } @@ -76,7 +76,7 @@ public static function statusCodeProvider(): array */ public function testAssertResponseMessage(string $messageContent, bool $shouldPass): void { - $response = new Response($messageContent); + $response = new Response($messageContent); $expectedMessage = 'This is a test message'; if ($shouldPass === false) { @@ -101,7 +101,7 @@ public static function responseMessageProvider(): array */ public function testAssertResponseDifferentCode(int $statusCode, ?string $messageContent, bool $shouldPass): void { - $response = new Response('This is a test message', 200); + $response = new Response('This is a test message', Response::HTTP_OK); if ($shouldPass === false) { $this->expectException(AssertionFailedError::class); @@ -116,8 +116,8 @@ public function testAssertResponseDifferentCode(int $statusCode, ?string $messag public static function assertResponseProvider(): array { return [ - [200, 'This is a test message', true], - [404, 'This is a test message', false], + [Response::HTTP_OK, 'This is a test message', true], + [Response::HTTP_NOT_FOUND, 'This is a test message', false], ]; } @@ -141,10 +141,10 @@ public function testAssertResponseIsSuccessful(int $statusCode, ?string $expecte public static function responseIsSuccessfulProvider(): array { return [ - [200, null, true], - [200, 'Expected message', true], - [404, null, false], - [200, 'Unexpected message', false], + [Response::HTTP_OK, null, true], + [Response::HTTP_OK, 'Expected message', true], + [Response::HTTP_NOT_FOUND, null, false], + [Response::HTTP_OK, 'Unexpected message', false], ]; } @@ -167,10 +167,10 @@ public function testAssertResponseIsRedirect(int $statusCode, ?string $expectedM public static function responseIsRedirectProvider(): array { return [ - [302, null, true], - [302, 'Expected message', true], - [200, null, false], - [302, 'Unexpected message', false], + [Response::HTTP_MOVED_PERMANENTLY, null, true], + [Response::HTTP_MOVED_PERMANENTLY, 'Expected message', true], + [Response::HTTP_OK, null, false], + [Response::HTTP_MOVED_PERMANENTLY, 'Unexpected message', false], ]; } @@ -193,10 +193,10 @@ public function testAssertResponseIsBadRequest(int $statusCode, ?string $expecte public static function responseIsBadRequestProvider(): array { return [ - [400, null, true], - [400, 'Expected message', true], - [200, null, false], - [400, 'Unexpected message', false], + [Response::HTTP_BAD_REQUEST, null, true], + [Response::HTTP_BAD_REQUEST, 'Expected message', true], + [Response::HTTP_OK, null, false], + [Response::HTTP_BAD_REQUEST, 'Unexpected message', false], ]; } @@ -219,10 +219,10 @@ public function testAssertResponseIsServerError(int $statusCode, ?string $expect public static function responseIsServerErrorProvider(): array { return [ - [500, null, true], - [500, 'Expected message', true], - [200, null, false], - [500, 'Unexpected message', false], + [Response::HTTP_INTERNAL_SERVER_ERROR, null, true], + [Response::HTTP_INTERNAL_SERVER_ERROR, 'Expected message', true], + [Response::HTTP_OK, null, false], + [Response::HTTP_INTERNAL_SERVER_ERROR, 'Unexpected message', false], ]; } } From d539e743d42c63f8b839bf2e7acb1c9d6a311c91 Mon Sep 17 00:00:00 2001 From: Joao Cardoso Date: Sun, 21 Jul 2024 20:53:43 +0200 Subject: [PATCH 6/6] Add more Symfony response assertions to ResponseAssertions trait --- README.md | 4 +- src/Symfony/ResponseAssertions.php | 42 +++++++++++++------ tests/Unit/Symfony/ResponseAssertionsTest.php | 31 +++++++------- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 6f778cd..ec120af 100644 --- a/README.md +++ b/README.md @@ -110,8 +110,8 @@ This trait includes methods for verifying the status code, response message cont - `assertJsonResponse` - `assertResponse` - `assertResponseIsSuccessful` -- `assertResponseIsRedirect` -- `assertResponseIsBadRequest` +- `assertResponseIsRedirection` +- `assertResponseIsClientError` - `assertResponseIsServerError` ## About us diff --git a/src/Symfony/ResponseAssertions.php b/src/Symfony/ResponseAssertions.php index c64d289..3de3f95 100644 --- a/src/Symfony/ResponseAssertions.php +++ b/src/Symfony/ResponseAssertions.php @@ -21,33 +21,49 @@ protected static function assertJsonResponse(array $expected, JsonResponse $resp Assert::assertSame($expected, $content); } - protected static function assertResponse(Response $response, int $expectedStatusCode, ?string $expectedMessage = null): void + protected static function assertResponse(Response $response, int $expectedStatusCode, ?string $expectedContent = null): void { self::assertStatusCode($response, $expectedStatusCode); - if ($expectedMessage !== null) { - self::assertResponseMessage($response, $expectedMessage); + if ($expectedContent !== null) { + self::assertResponseContent($response, $expectedContent); } } - protected static function assertResponseIsSuccessful(Response $response, ?string $expectedMessage = null): void + protected static function assertResponseIsSuccessful(Response $response, ?string $expectedContent = null): void { - self::assertResponse($response, Response::HTTP_OK, $expectedMessage); + self::assertTrue($response->isSuccessful(), 'Response is not successful.'); + + if ($expectedContent !== null) { + self::assertResponseContent($response, $expectedContent); + } } - protected static function assertResponseIsRedirect(Response $response, ?string $expectedMessage = null): void + protected static function assertResponseIsRedirection(Response $response, ?string $expectedRedirectionUrl = null): void { - self::assertResponse($response, Response::HTTP_MOVED_PERMANENTLY, $expectedMessage); + self::assertTrue($response->isRedirection(), 'Response is not a redirection.'); + + if ($expectedRedirectionUrl !== null) { + self::assertSame($response->headers->get('Location'), $expectedRedirectionUrl); + } } - protected static function assertResponseIsBadRequest(Response $response, ?string $expectedMessage = null): void + protected static function assertResponseIsClientError(Response $response, ?string $expectedContent = null): void { - self::assertResponse($response, Response::HTTP_BAD_REQUEST, $expectedMessage); + self::assertTrue($response->isClientError(), 'Response is not a client error.'); + + if ($expectedContent !== null) { + self::assertResponseContent($response, $expectedContent); + } } - protected static function assertResponseIsServerError(Response $response, ?string $expectedMessage = null): void + protected static function assertResponseIsServerError(Response $response, ?string $expectedContent = null): void { - self::assertResponse($response, Response::HTTP_INTERNAL_SERVER_ERROR, $expectedMessage); + self::assertTrue($response->isServerError(), 'Response is not a server error.'); + + if ($expectedContent !== null) { + self::assertResponseContent($response, $expectedContent); + } } private static function assertStatusCode(Response $response, int $expectedStatusCode): void @@ -59,8 +75,8 @@ private static function assertStatusCode(Response $response, int $expectedStatus ); } - private static function assertResponseMessage(Response $response, string $expectedMessage): void + private static function assertResponseContent(Response $response, string $expectedContent): void { - Assert::assertSame($expectedMessage, $response->getContent()); + Assert::assertSame($expectedContent, $response->getContent()); } } diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php index 12728b3..af4b4c7 100644 --- a/tests/Unit/Symfony/ResponseAssertionsTest.php +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -72,9 +72,9 @@ public static function statusCodeProvider(): array } /** - * @dataProvider responseMessageProvider + * @dataProvider responseContentProvider */ - public function testAssertResponseMessage(string $messageContent, bool $shouldPass): void + public function testAssertResponseContent(string $messageContent, bool $shouldPass): void { $response = new Response($messageContent); $expectedMessage = 'This is a test message'; @@ -82,13 +82,13 @@ public function testAssertResponseMessage(string $messageContent, bool $shouldPa if ($shouldPass === false) { $this->expectException(AssertionFailedError::class); } - self::assertResponseMessage($response, $expectedMessage); + self::assertResponseContent($response, $expectedMessage); } /** * @return array */ - public static function responseMessageProvider(): array + public static function responseContentProvider(): array { return [ ['This is a test message', true], @@ -149,48 +149,49 @@ public static function responseIsSuccessfulProvider(): array } /** - * @dataProvider responseIsRedirectProvider + * @dataProvider responseIsRedirectionProvider */ - public function testAssertResponseIsRedirect(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + public function testAssertResponseIsRedirection(int $statusCode, ?string $expectedRedirectionUrl, bool $shouldPass): void { - $response = new Response('Expected message', $statusCode); + $response = new Response('', $statusCode); + $response->headers->set('Location', 'http://expected.com'); if ($shouldPass === false) { $this->expectException(AssertionFailedError::class); } - self::assertResponseIsRedirect($response, $expectedMessage); + self::assertResponseIsRedirection($response, $expectedRedirectionUrl); } /** * @return array */ - public static function responseIsRedirectProvider(): array + public static function responseIsRedirectionProvider(): array { return [ [Response::HTTP_MOVED_PERMANENTLY, null, true], - [Response::HTTP_MOVED_PERMANENTLY, 'Expected message', true], + [Response::HTTP_MOVED_PERMANENTLY, 'http://expected.com', true], [Response::HTTP_OK, null, false], - [Response::HTTP_MOVED_PERMANENTLY, 'Unexpected message', false], + [Response::HTTP_MOVED_PERMANENTLY, 'http://unexpected.com', false], ]; } /** - * @dataProvider responseIsBadRequestProvider + * @dataProvider responseIsClientErrorProvider */ - public function testAssertResponseIsBadRequest(int $statusCode, ?string $expectedMessage, bool $shouldPass): void + public function testAssertResponseIsClientError(int $statusCode, ?string $expectedMessage, bool $shouldPass): void { $response = new Response('Expected message', $statusCode); if ($shouldPass === false) { $this->expectException(AssertionFailedError::class); } - self::assertResponseIsBadRequest($response, $expectedMessage); + self::assertResponseIsClientError($response, $expectedMessage); } /** * @return array */ - public static function responseIsBadRequestProvider(): array + public static function responseIsClientErrorProvider(): array { return [ [Response::HTTP_BAD_REQUEST, null, true],