Skip to content

Commit

Permalink
Add Symfony response assertion trait (#11)
Browse files Browse the repository at this point in the history
New assertion trait with assertJsonResponse

checks response content is not false + decodes to expected array
  • Loading branch information
bram123 authored Dec 18, 2023
1 parent fd38ab0 commit 3319edc
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"fix": "@fix:phpcbf",
"fix:phpcbf": "phpcbf src tests",
"test": "phpunit",
"test:unit": "phpunit --testsuite unit",
"test:integration": "phpunit --testsuite integration"
},
"suggest": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
3 changes: 3 additions & 0 deletions phpunit9.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<coverage>
<include>
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/ResponseAssertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace DR\PHPUnitExtensions\Symfony;

use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

trait ResponseAssertions
{
/**
* @param mixed[] $expected
*/
protected static function assertJsonResponse(array $expected, JsonResponse $response): void
{
Assert::assertNotFalse($response->getContent());

$content = json_decode($response->getContent(), true);
Assert::assertSame($expected, $content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DR\PHPUnitExtensions\Tests\Integration\Symfony\Controller;

use DR\PHPUnitExtensions\Symfony\AbstractControllerTestCase;
use DR\PHPUnitExtensions\Symfony\ResponseAssertions;
use DR\PHPUnitExtensions\Tests\Resources\Symfony\Controller\GenerateUrlController;

/**
Expand All @@ -13,16 +14,18 @@
*/
class GenerateUrlControllerTest extends AbstractControllerTestCase
{
use ResponseAssertions;

public function testSingleGenerate(): void
{
$this->expectGenerateUrl("first_route")->willReturn('first_url');
static::assertSame('{"url":"first_url"}', ($this->controller)()->getContent());
static::assertJsonResponse(["url" => "first_url"], ($this->controller)());
}

public function testMultiGenerate(): void
{
$this->expectGenerateUrlWithConsecutive(["first_route"], ["second_route"])->willReturnOnConsecutiveCalls('first_url', 'second_url');
static::assertSame('{"first":"first_url","second":"second_url"}', ($this->controller)(true)->getContent());
static::assertJsonResponse(["first" => "first_url", "second" => "second_url"], ($this->controller)(true));
}

public function getController(): GenerateUrlController
Expand Down
3 changes: 1 addition & 2 deletions tests/Resources/Symfony/Controller/GenerateUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class GenerateUrlController extends AbstractController
{
public function __invoke(bool $multi = false): Response
public function __invoke(bool $multi = false): JsonResponse
{
if ($multi) {
return new JsonResponse(
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Symfony/ResponseAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace DR\PHPUnitExtensions\Tests\Unit\Symfony;

use DR\PHPUnitExtensions\Symfony\ResponseAssertions;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* @covers \DR\PHPUnitExtensions\Symfony\ResponseAssertions
*/
class ResponseAssertionsTest extends TestCase
{
use ResponseAssertions;

public function testAssertJsonResponse(): void
{
$expected = ['foo' => 'bar'];
$response = new JsonResponse($expected);

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

public function testAssertJsonResponseFails(): void
{
$expected = ['foo' => 'bar'];
$response = new JsonResponse(['bar' => 'foo']);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that two arrays are identical.');
self::assertJsonResponse($expected, $response);
}

public function testAssertJsonResponseFalse(): void
{
$expected = ['foo' => 'bar'];
$response = new JsonResponse(false);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Failed asserting that false is identical to Array');
self::assertJsonResponse($expected, $response);
}
}

0 comments on commit 3319edc

Please sign in to comment.