-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Symfony response assertion trait (#11)
New assertion trait with assertJsonResponse checks response content is not false + decodes to expected array
- Loading branch information
Showing
7 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |