Skip to content

Commit

Permalink
Merge pull request #37 from gricob/feature/add-instance-of-response-a…
Browse files Browse the repository at this point in the history
…ssert

Add instanceOf assert to TestResponse class to check with
  • Loading branch information
juanjomb authored Jan 21, 2020
2 parents 7288ffe + f9e61f0 commit 90eebd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
25 changes: 18 additions & 7 deletions src/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TestResponse

/**
* TestResponse constructor.
*
* @param Response $baseResponse
*/
protected function __construct(Response $baseResponse)
Expand All @@ -42,7 +43,7 @@ public static function fromBaseResponse(Response $baseResponse)
}

/**
* @return null|Crawler
* @return Crawler|null
*/
public function getCrawler(): ?Crawler
{
Expand All @@ -51,6 +52,7 @@ public function getCrawler(): ?Crawler

/**
* @param Crawler $crawler
*
* @return TestResponse
*/
public function setCrawler(Crawler $crawler): self
Expand All @@ -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);
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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);
Expand Down Expand Up @@ -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.');
}

Expand All @@ -187,7 +197,7 @@ protected function getProfile(): Profile
}

/**
* @return null|Container
* @return Container|null
*/
public function getContainer(): ?Container
{
Expand All @@ -196,6 +206,7 @@ public function getContainer(): ?Container

/**
* @param Container $container
*
* @return TestResponse
*/
public function setContainer(Container $container): self
Expand All @@ -214,4 +225,4 @@ public function __call($method, $arguments)
{
return $this->baseResponse->{$method}(...$arguments);
}
}
}
42 changes: 31 additions & 11 deletions tests/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'))
Expand All @@ -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');
Expand Down Expand Up @@ -75,7 +95,7 @@ public function testAssertSeeAll()
TestResponse::fromBaseResponse(Response::create('Test text in response'))
->assertSeeAll([
'text',
'response'
'response',
]);
}

Expand All @@ -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',
]);
}

Expand All @@ -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',
]);
}

Expand All @@ -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',
]);
}

Expand All @@ -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',
]);
}

Expand All @@ -144,4 +164,4 @@ public function testGetContent()

$this->assertEquals('Test content', $testResponse->getContent());
}
}
}

0 comments on commit 90eebd5

Please sign in to comment.