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

Add more Symfony response assertions to ResponseAssertions trait #15

Merged
merged 6 commits into from
Jul 22, 2024
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`
- `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
Expand Down
43 changes: 43 additions & 0 deletions src/Symfony/ResponseAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,47 @@ 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
cardoso123inkt marked this conversation as resolved.
Show resolved Hide resolved
{
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);
cardoso123inkt marked this conversation as resolved.
Show resolved Hide resolved
}

protected static function assertResponseIsRedirect(Response $response, ?string $expectedMessage = null): void
{
self::assertResponse($response, Response::HTTP_MOVED_PERMANENTLY, $expectedMessage);
cardoso123inkt marked this conversation as resolved.
Show resolved Hide resolved
}

protected static function assertResponseIsBadRequest(Response $response, ?string $expectedMessage = null): void
{
self::assertResponse($response, Response::HTTP_BAD_REQUEST, $expectedMessage);
cardoso123inkt marked this conversation as resolved.
Show resolved Hide resolved
}

protected static function assertResponseIsServerError(Response $response, ?string $expectedMessage = null): void
{
self::assertResponse($response, Response::HTTP_INTERNAL_SERVER_ERROR, $expectedMessage);
cardoso123inkt marked this conversation as resolved.
Show resolved Hide resolved
}

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 assertResponseMessage(Response $response, string $expectedMessage): void
{
Assert::assertSame($expectedMessage, $response->getContent());
}
}
182 changes: 182 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,186 @@ 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 responseMessageProvider
*/
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);
}

/**
* @return array<int, array{string, bool}>
*/
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', 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 responseIsRedirectProvider
*/
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);
}

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

/**
* @dataProvider responseIsBadRequestProvider
*/
public function testAssertResponseIsBadRequest(int $statusCode, ?string $expectedMessage, bool $shouldPass): void
{
$response = new Response('Expected message', $statusCode);

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

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