diff --git a/src/Testing/TestResponse.php b/src/Testing/TestResponse.php index d2e7f0b..75bcd73 100644 --- a/src/Testing/TestResponse.php +++ b/src/Testing/TestResponse.php @@ -29,6 +29,7 @@ class TestResponse /** * TestResponse constructor. + * * @param Response $baseResponse */ protected function __construct(Response $baseResponse) @@ -42,7 +43,7 @@ public static function fromBaseResponse(Response $baseResponse) } /** - * @return null|Crawler + * @return Crawler|null */ public function getCrawler(): ?Crawler { @@ -51,6 +52,7 @@ public function getCrawler(): ?Crawler /** * @param Crawler $crawler + * * @return TestResponse */ public function setCrawler(Crawler $crawler): self @@ -77,6 +79,14 @@ public function assertRedirect($expectedLocation = null): self return $this; } + public function assertInstanceOf(string $expectedClass) + { + PHPUnit::assertInstanceOf( + $expectedClass, + $this->baseResponse + ); + } + public function assertNotFound(): self { return $this->assertStatusCode(Response::HTTP_NOT_FOUND); @@ -114,7 +124,7 @@ public function assertSee(string $needle): self public function assertSeeAll(array $needles): self { - foreach($needles as $needle) { + foreach ($needles as $needle) { PHPUnit::assertContains($needle, $this->baseResponse->getContent()); } @@ -130,7 +140,7 @@ public function assertDontSee(string $needle): self public function assertDontSeeAny(array $needles) { - foreach($needles as $needle) { + foreach ($needles as $needle) { PHPUnit::assertNotContains($needle, $this->baseResponse->getContent()); } @@ -140,7 +150,7 @@ public function assertDontSeeAny(array $needles) public function assertExactJson(array $data): self { $actual = json_encode(Arr::sortRecursive( - (array)$this->decodeResponseJson() + (array) $this->decodeResponseJson() )); PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data)), $actual); @@ -177,7 +187,7 @@ protected function getTwigCollector(): TwigDataCollector protected function getProfile(): Profile { - if(!$this->getContainer()->has('profiler')) { + if (!$this->getContainer()->has('profiler')) { PHPUnit::fail('Profiler is not initialized.'); } @@ -187,7 +197,7 @@ protected function getProfile(): Profile } /** - * @return null|Container + * @return Container|null */ public function getContainer(): ?Container { @@ -196,6 +206,7 @@ public function getContainer(): ?Container /** * @param Container $container + * * @return TestResponse */ public function setContainer(Container $container): self @@ -214,4 +225,4 @@ public function __call($method, $arguments) { return $this->baseResponse->{$method}(...$arguments); } -} \ No newline at end of file +} diff --git a/tests/TestResponseTest.php b/tests/TestResponseTest.php index 2895808..11f8c51 100644 --- a/tests/TestResponseTest.php +++ b/tests/TestResponseTest.php @@ -4,8 +4,10 @@ use Gricob\FunctionalTestBundle\Testing\TestResponse; use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; use Symfony\Component\DomCrawler\Crawler; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; @@ -19,11 +21,29 @@ public function testAssertOk() public function testAssertOkFails() { $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessage("Response status code [500] does not match expected 200 status code."); + $this->expectExceptionMessage('Response status code [500] does not match expected 200 status code.'); TestResponse::fromBaseResponse(Response::create('', 500))->assertOk(); } + public function testAssertInstanceOfTrue() + { + TestResponse::fromBaseResponse(JsonResponse::create('', 200))->assertInstanceOf(Response::class); + } + + public function testAssertInstanceOfFalse() + { + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage( + sprintf( + 'Failed asserting that %s Object (...) is an instance of class "%s".', + Response::class, + JsonResponse::class + ) + ); + TestResponse::fromBaseResponse(Response::create('', 200))->assertInstanceOf(JsonResponse::class); + } + public function testAssertRedirectWithExpectedLocation() { TestResponse::fromBaseResponse(RedirectResponse::create('/test-redirect')) @@ -39,7 +59,7 @@ public function testAssertRedirectWithoutExpectedLocation() public function testAssertRedirectFails() { $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessage("Response redirect location does not match expected [/other-redirect] location"); + $this->expectExceptionMessage('Response redirect location does not match expected [/other-redirect] location'); TestResponse::fromBaseResponse(RedirectResponse::create('/test-redirect')) ->assertRedirect('/other-redirect'); @@ -75,7 +95,7 @@ public function testAssertSeeAll() TestResponse::fromBaseResponse(Response::create('Test text in response')) ->assertSeeAll([ 'text', - 'response' + 'response', ]); } @@ -87,7 +107,7 @@ public function testAssertSeeAllFails() TestResponse::fromBaseResponse(Response::create('Test text in response')) ->assertSeeAll([ 'text', - 'this does not exists' + 'this does not exists', ]); } @@ -96,7 +116,7 @@ public function testAssertDontSeeAny() TestResponse::fromBaseResponse(Response::create('Test text in response')) ->assertDontSeeAny([ 'this does not exists', - 'this one neither' + 'this one neither', ]); } @@ -108,7 +128,7 @@ public function testAssertDontSeeAnyFails() TestResponse::fromBaseResponse(Response::create('Test text in response')) ->assertDontSeeAny([ 'text', - 'this does not exists' + 'this does not exists', ]); } @@ -119,15 +139,15 @@ public function testAssertExactJson() 'key1' => 'Key 1', 'key2' => [ 10, - 30 - ] + 30, + ], ]))) ->assertExactJson([ 'key2' => [ 30, - 10 + 10, ], - 'key1' => 'Key 1' + 'key1' => 'Key 1', ]); } @@ -144,4 +164,4 @@ public function testGetContent() $this->assertEquals('Test content', $testResponse->getContent()); } -} \ No newline at end of file +}