Skip to content

Commit

Permalink
Add unittest for new ResponseAssertions.php trait
Browse files Browse the repository at this point in the history
  • Loading branch information
bram123 committed Dec 15, 2023
1 parent 2babff0 commit 4947873
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
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
6 changes: 5 additions & 1 deletion src/Symfony/ResponseAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
namespace DR\PHPUnitExtensions\Symfony;

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

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

Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Symfony/ResponseAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace DR\PHPUnitExtensions\Tests\Unit\Symfony;

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

#[CoversClass(ResponseAssertions::class)]
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 4947873

Please sign in to comment.