Skip to content

Commit

Permalink
[6.x] Utilise illuminate/testing based on #122
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Feb 23, 2020
1 parent 57b9702 commit c3ab74a
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 189 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"illuminate/database": "^7.0",
"illuminate/http": "^7.0",
"illuminate/support": "^7.0",
"illuminate/testing": "^7.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.4|^9.0",
"symfony/console": "^5.0",
Expand Down
203 changes: 30 additions & 173 deletions src/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace Laravel\BrowserKitTesting\Concerns;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use PHPUnit\Framework\Assert as PHPUnit;
use Laravel\BrowserKitTesting\TestResponse;
use PHPUnit\Framework\ExpectationFailedException;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
Expand Down Expand Up @@ -274,7 +271,9 @@ public function handle(Request $request)
{
$this->currentUri = $request->fullUrl();

$this->response = $this->app->prepareResponse($this->app->handle($request));
$this->response = TestResponse::fromBaseResponse(
$this->app->prepareResponse($this->app->handle($request))
);

return $this;
}
Expand Down Expand Up @@ -309,11 +308,7 @@ protected function receiveJson(array $data = null)
*/
public function seeJsonEquals(array $data)
{
$actual = json_encode(Arr::sortRecursive(
(array) $this->decodeResponseJson()
));

$this->assertEquals(json_encode(Arr::sortRecursive($data)), $actual);
$this->response->assertExactJson($data);

return $this;
}
Expand Down Expand Up @@ -362,28 +357,7 @@ public function dontSeeJson(array $data = null)
*/
public function seeJsonStructure(array $structure = null, $responseData = null)
{
if (is_null($structure)) {
return $this->seeJson();
}

if (is_null($responseData)) {
$responseData = $this->decodeResponseJson();
}

foreach ($structure as $key => $value) {
if (is_array($value) && $key === '*') {
$this->assertIsArray($responseData);

foreach ($responseData as $responseDataItem) {
$this->seeJsonStructure($structure['*'], $responseDataItem);
}
} elseif (is_array($value)) {
$this->assertArrayHasKey($key, $responseData);
$this->seeJsonStructure($structure[$key], $responseData[$key]);
} else {
$this->assertArrayHasKey($value, $responseData);
}
}
$this->response->assertJsonStructure($structure, $responseData);

return $this;
}
Expand All @@ -397,62 +371,15 @@ public function seeJsonStructure(array $structure = null, $responseData = null)
*/
protected function seeJsonContains(array $data, $negate = false)
{
$method = $negate ? 'assertFalse' : 'assertTrue';

$actual = json_encode(Arr::sortRecursive(
(array) $this->decodeResponseJson()
));

foreach (Arr::sortRecursive($data) as $key => $value) {
$expected = $this->formatToExpectedJson($key, $value);

$this->{$method}(
Str::contains($actual, $expected),
($negate ? 'Found unexpected' : 'Unable to find').' JSON fragment'.PHP_EOL."[{$expected}]".PHP_EOL.'within'.PHP_EOL."[{$actual}]."
);
if ($negate) {
$this->response->assertJsonMissing($data, false);
} else {
$this->response->assertJsonFragment($data);
}

return $this;
}

/**
* Validate and return the decoded response JSON.
*
* @return array
*/
protected function decodeResponseJson()
{
$decodedResponse = json_decode($this->response->getContent(), true);

if (is_null($decodedResponse) || $decodedResponse === false) {
$this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
}

return $decodedResponse;
}

/**
* Format the given key and value into a JSON string for expectation checks.
*
* @param string $key
* @param mixed $value
* @return string
*/
protected function formatToExpectedJson($key, $value)
{
$expected = json_encode([$key => $value]);

if (Str::startsWith($expected, '{')) {
$expected = substr($expected, 1);
}

if (Str::endsWith($expected, '}')) {
$expected = substr($expected, 0, -1);
}

return trim($expected);
}

/**
* Asserts that the status code of the response matches the given code.
*
Expand All @@ -461,7 +388,7 @@ protected function formatToExpectedJson($key, $value)
*/
protected function seeStatusCode($status)
{
$this->assertEquals($status, $this->response->getStatusCode());
$this->response->assertStatus($status);

return $this;
}
Expand All @@ -475,16 +402,7 @@ protected function seeStatusCode($status)
*/
protected function seeHeader($headerName, $value = null)
{
$headers = $this->response->headers;

$this->assertTrue($headers->has($headerName), "Header [{$headerName}] not present on response.");

if (! is_null($value)) {
$this->assertEquals(
$headers->get($headerName), $value,
"Header [{$headerName}] was found, but value [{$headers->get($headerName)}] does not match [{$value}]."
);
}
$this->response->assertHeader($headerName, $value);

return $this;
}
Expand Down Expand Up @@ -512,32 +430,7 @@ protected function seePlainCookie($cookieName, $value = null)
*/
protected function seeCookie($cookieName, $value = null, $encrypted = true, $unserialize = true)
{
$headers = $this->response->headers;

$exist = false;

foreach ($headers->getCookies() as $cookie) {
if ($cookie->getName() === $cookieName) {
$exist = true;
break;
}
}

$this->assertTrue($exist, "Cookie [{$cookieName}] not present on response.");

if (! $exist || is_null($value)) {
return $this;
}

$cookieValue = $cookie->getValue();

$actual = $encrypted
? $this->app['encrypter']->decrypt($cookieValue, $unserialize) : $cookieValue;

$this->assertEquals(
$actual, $value,
"Cookie [{$cookieName}] was found, but value [{$actual}] does not match [{$value}]."
);
$this->response->assertCookie($cookieName, $value, $encrypted, $unserialize);

return $this;
}
Expand Down Expand Up @@ -586,7 +479,7 @@ public function call($method, $uri, $parameters = [], $cookies = [], $files = []

$kernel->terminate($request, $response);

return $this->response = $response;
return $this->response = TestResponse::fromBaseResponse($response);
}

/**
Expand All @@ -605,7 +498,7 @@ public function callSecure($method, $uri, $parameters = [], $cookies = [], $file
{
$uri = $this->app['url']->secure(ltrim($uri, '/'));

return $this->response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
return $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
}

/**
Expand All @@ -625,7 +518,7 @@ public function action($method, $action, $wildcards = [], $parameters = [], $coo
{
$uri = $this->app['url']->action($action, $wildcards, true);

return $this->response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
return $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
}

/**
Expand All @@ -645,7 +538,7 @@ public function route($method, $name, $routeParameters = [], $parameters = [], $
{
$uri = $this->app['url']->route($name, $routeParameters);

return $this->response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
return $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
}

/**
Expand Down Expand Up @@ -727,9 +620,7 @@ protected function filterFiles($files)
*/
public function assertResponseOk()
{
$actual = $this->response->getStatusCode();

PHPUnit::assertTrue($this->response->isOk(), "Expected status code 200, got {$actual}.");
$this->response->assertResponseOk();

return $this;
}
Expand All @@ -742,9 +633,7 @@ public function assertResponseOk()
*/
public function assertResponseStatus($code)
{
$actual = $this->response->getStatusCode();

PHPUnit::assertEquals($code, $this->response->getStatusCode(), "Expected status code {$code}, got {$actual}.");
$this->response->assertStatus($code);

return $this;
}
Expand All @@ -758,21 +647,7 @@ public function assertResponseStatus($code)
*/
public function assertViewHas($key, $value = null)
{
if (is_array($key)) {
return $this->assertViewHasAll($key);
}

if (! isset($this->response->original) || ! $this->response->original instanceof View) {
return PHPUnit::assertTrue(false, 'The response was not a view.');
}

if (is_null($value)) {
PHPUnit::assertArrayHasKey($key, $this->response->original->getData());
} elseif ($value instanceof Closure) {
PHPUnit::assertTrue($value($this->response->original->$key));
} else {
PHPUnit::assertEquals($value, $this->response->original->$key);
}
$this->response->assertViewHas($key, $value);

return $this;
}
Expand All @@ -785,13 +660,7 @@ public function assertViewHas($key, $value = null)
*/
public function assertViewHasAll(array $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertViewHas($value);
} else {
$this->assertViewHas($key, $value);
}
}
$this->response->assertViewHasAll($bindings);

return $this;
}
Expand All @@ -804,11 +673,7 @@ public function assertViewHasAll(array $bindings)
*/
public function assertViewMissing($key)
{
if (! isset($this->response->original) || ! $this->response->original instanceof View) {
return PHPUnit::assertTrue(false, 'The response was not a view.');
}

PHPUnit::assertArrayNotHasKey($key, $this->response->original->getData());
$this->response->assertViewMissing($key);

return $this;
}
Expand All @@ -822,11 +687,7 @@ public function assertViewMissing($key)
*/
public function assertRedirectedTo($uri, $with = [])
{
PHPUnit::assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $this->response);

PHPUnit::assertEquals($this->app['url']->to($uri), $this->response->headers->get('Location'));

$this->assertSessionHasAll($with);
$this->response->assertRedirectedTo($uri, $with);

return $this;
}
Expand All @@ -841,7 +702,9 @@ public function assertRedirectedTo($uri, $with = [])
*/
public function assertRedirectedToRoute($name, $parameters = [], $with = [])
{
return $this->assertRedirectedTo($this->app['url']->route($name, $parameters), $with);
$this->response->assertRedirectedToRoute($name, $parameters, $with);

return $this;
}

/**
Expand All @@ -854,7 +717,9 @@ public function assertRedirectedToRoute($name, $parameters = [], $with = [])
*/
public function assertRedirectedToAction($name, $parameters = [], $with = [])
{
return $this->assertRedirectedTo($this->app['url']->action($name, $parameters), $with);
$this->response->assertRedirectedToAction($name, $parameters, $with);

return $this;
}

/**
Expand All @@ -864,14 +729,6 @@ public function assertRedirectedToAction($name, $parameters = [], $with = [])
*/
public function dump()
{
$content = $this->response->getContent();

$json = json_decode($content);

if (json_last_error() === JSON_ERROR_NONE) {
$content = $json;
}

dd($content);
$this->response->dump();
}
}
Loading

0 comments on commit c3ab74a

Please sign in to comment.