Skip to content

Commit

Permalink
Add more Symfony response assertions to ResponseAssertions trait (#15)
Browse files Browse the repository at this point in the history
* Add more Symfony response assertions to ResponseAssertions trait
  • Loading branch information
cardoso123inkt committed Jul 22, 2024
1 parent c1122d2 commit 65141fe
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
- `assertResponseIsRedirection`
- `assertResponseIsClientError`
- `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
Expand Down
59 changes: 59 additions & 0 deletions src/Symfony/ResponseAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,63 @@ 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 $expectedContent = null): void
{
self::assertStatusCode($response, $expectedStatusCode);

if ($expectedContent !== null) {
self::assertResponseContent($response, $expectedContent);
}
}

protected static function assertResponseIsSuccessful(Response $response, ?string $expectedContent = null): void
{
self::assertTrue($response->isSuccessful(), 'Response is not successful.');

if ($expectedContent !== null) {
self::assertResponseContent($response, $expectedContent);
}
}

protected static function assertResponseIsRedirection(Response $response, ?string $expectedRedirectionUrl = null): void
{
self::assertTrue($response->isRedirection(), 'Response is not a redirection.');

if ($expectedRedirectionUrl !== null) {
self::assertSame($response->headers->get('Location'), $expectedRedirectionUrl);
}
}

protected static function assertResponseIsClientError(Response $response, ?string $expectedContent = null): void
{
self::assertTrue($response->isClientError(), 'Response is not a client error.');

if ($expectedContent !== null) {
self::assertResponseContent($response, $expectedContent);
}
}

protected static function assertResponseIsServerError(Response $response, ?string $expectedContent = null): void
{
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
{
Assert::assertSame(
$expectedStatusCode,
$response->getStatusCode(),
sprintf('Expected status code %d but got %d.', $expectedStatusCode, $response->getStatusCode())
);
}

private static function assertResponseContent(Response $response, string $expectedContent): void
{
Assert::assertSame($expectedContent, $response->getContent());
}
}
183 changes: 183 additions & 0 deletions tests/Unit/Symfony/ResponseAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

/**
* @covers \DR\PHPUnitExtensions\Symfony\ResponseAssertions
Expand All @@ -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);
}

Expand All @@ -41,6 +43,187 @@ public function testAssertJsonResponseFalse(): void

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that false is identical to Array');

self::assertJsonResponse($expected, $response);
}

/**
* @dataProvider statusCodeProvider
*/
public function testAssertStatusCode(int $statusCode, bool $shouldPass): void
{
$response = new Response('', Response::HTTP_OK);

if ($shouldPass === false) {
$this->expectException(AssertionFailedError::class);
}
self::assertStatusCode($response, $statusCode);
}

/**
* @return array<int, array{int, bool}>
*/
public static function statusCodeProvider(): array
{
return [
[Response::HTTP_OK, true],
[Response::HTTP_NOT_FOUND, false],
];
}

/**
* @dataProvider responseContentProvider
*/
public function testAssertResponseContent(string $messageContent, bool $shouldPass): void
{
$response = new Response($messageContent);
$expectedMessage = 'This is a test message';

if ($shouldPass === false) {
$this->expectException(AssertionFailedError::class);
}
self::assertResponseContent($response, $expectedMessage);
}

/**
* @return array<int, array{string, bool}>
*/
public static function responseContentProvider(): 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', Response::HTTP_OK);

if ($shouldPass === false) {
$this->expectException(AssertionFailedError::class);
}

self::assertResponse($response, $statusCode, $messageContent);
}

/**
* @return array<int, array{int, ?string, bool}>
*/
public static function assertResponseProvider(): array
{
return [
[Response::HTTP_OK, 'This is a test message', true],
[Response::HTTP_NOT_FOUND, 'This is a test message', false],
];
}

/**
* @dataProvider responseIsSuccessfulProvider
*/
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);
}

/**
* @return array<int, array{int, ?string, bool}>
*/
public static function responseIsSuccessfulProvider(): array
{
return [
[Response::HTTP_OK, null, true],
[Response::HTTP_OK, 'Expected message', true],
[Response::HTTP_NOT_FOUND, null, false],
[Response::HTTP_OK, 'Unexpected message', false],
];
}

/**
* @dataProvider responseIsRedirectionProvider
*/
public function testAssertResponseIsRedirection(int $statusCode, ?string $expectedRedirectionUrl, bool $shouldPass): void
{
$response = new Response('', $statusCode);
$response->headers->set('Location', 'http://expected.com');

if ($shouldPass === false) {
$this->expectException(AssertionFailedError::class);
}
self::assertResponseIsRedirection($response, $expectedRedirectionUrl);
}

/**
* @return array<int, array{int, ?string, bool}>
*/
public static function responseIsRedirectionProvider(): array
{
return [
[Response::HTTP_MOVED_PERMANENTLY, null, true],
[Response::HTTP_MOVED_PERMANENTLY, 'http://expected.com', true],
[Response::HTTP_OK, null, false],
[Response::HTTP_MOVED_PERMANENTLY, 'http://unexpected.com', false],
];
}

/**
* @dataProvider responseIsClientErrorProvider
*/
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);
}

/**
* @return array<int, array{int, ?string, bool}>
*/
public static function responseIsClientErrorProvider(): array
{
return [
[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],
];
}

/**
* @dataProvider responseIsServerErrorProvider
*/
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);
}

/**
* @return array<int, array{int, ?string, bool}>
*/
public static function responseIsServerErrorProvider(): array
{
return [
[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],
];
}
}

0 comments on commit 65141fe

Please sign in to comment.