diff --git a/composer.json b/composer.json index b093f30..a4a4c32 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 948d6db..60f9815 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,6 +11,9 @@ tests/Integration + + tests/Unit + diff --git a/phpunit9.xml.dist b/phpunit9.xml.dist index cedb15c..3c4802e 100644 --- a/phpunit9.xml.dist +++ b/phpunit9.xml.dist @@ -14,6 +14,9 @@ tests/Integration + + tests/Unit + diff --git a/src/Symfony/ResponseAssertions.php b/src/Symfony/ResponseAssertions.php index 4004e48..256be2d 100644 --- a/src/Symfony/ResponseAssertions.php +++ b/src/Symfony/ResponseAssertions.php @@ -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()); diff --git a/tests/Unit/Symfony/ResponseAssertionsTest.php b/tests/Unit/Symfony/ResponseAssertionsTest.php new file mode 100644 index 0000000..8f1b77a --- /dev/null +++ b/tests/Unit/Symfony/ResponseAssertionsTest.php @@ -0,0 +1,45 @@ + '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); + } +}