Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nto HongjiangHuang-5.7
  • Loading branch information
taylorotwell committed Nov 12, 2018
2 parents 9b9832d + 76c0c1a commit cfab97f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,45 @@ public static function fromBaseResponse($response)
return new static($response);
}

/**
* JSON decode assoc and keep the empty object.
* @param string $json
* @link https://github.com/laravel/framework/issues/25769
* @return mixed
*/
public static function jsonDecodeKeepEmptyObject(string $json)
{
$patchEmptyObject = function ($array) use (&$patchEmptyObject) {

// If it is an empty class,it is reserved.
// Otherwise converted into an associative array.
if (is_object($array)) {
return empty((array) $array)
? $array
: $patchEmptyObject((array) $array);
}

foreach ($array as $key => $item) {
// We recursively deal with each of these terms.
if (is_array($item)
|| is_object($item)
) {
$array[$key] = $patchEmptyObject($item);
}
}

return $array;
};

$stdClass = json_decode($json);

if ($stdClass === false) {
return $stdClass;
}

return $patchEmptyObject($stdClass);
}

/**
* Assert that the response has a successful status code.
*
Expand Down Expand Up @@ -686,7 +725,7 @@ public function assertJsonMissingValidationErrors($keys)
*/
public function decodeResponseJson($key = null)
{
$decodedResponse = json_decode($this->getContent(), true);
$decodedResponse = static::jsonDecodeKeepEmptyObject($this->getContent());

if (is_null($decodedResponse) || $decodedResponse === false) {
if ($this->exception) {
Expand Down
40 changes: 40 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use JsonSerializable;
use Illuminate\Http\Response;
use PHPUnit\Framework\TestCase;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\View\View;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -258,6 +259,45 @@ public function testAssertJsonMissing()
$response->assertJsonMissing(['id' => 20]);
}

public function testAssertExactJson()
{
$response = new TestResponse((new JsonResponse([
'payload' => (object) [],
'a' => [],
'b' => (object) [],
'status' => 'success',
'data' => [
'name' => 'West Fannieland',
'updated_at' => '2018-10-05 20:48:11',
'created_at' => '2018-10-05 20:48:11',
'id' => 1,
'b' => (object) [],
'c' => [
'b' => (object) [],
'name' => 'albert',
],
],
])));

$response->assertExactJson([
'payload' => (object) [],
'b' => (object) [],
'a' => [],
'status' => 'success',
'data' => [
'name' => 'West Fannieland',
'created_at' => '2018-10-05 20:48:11',
'id' => 1,
'b' => (object) [],
'c' => [
'b' => (object) [],
'name' => 'albert',
],
'updated_at' => '2018-10-05 20:48:11',
],
]);
}

public function testAssertJsonMissingExact()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
Expand Down

0 comments on commit cfab97f

Please sign in to comment.