Skip to content

Commit

Permalink
Revert breaking change to assert JSON functions (#26713)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanheffley authored and taylorotwell committed Dec 2, 2018
1 parent 903305b commit cc1b312
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 87 deletions.
48 changes: 1 addition & 47 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,7 @@ public function assertJsonMissingValidationErrors($keys)
*/
public function decodeResponseJson($key = null)
{
$decodedResponse = $this->decodeJsonWhilePreservingEmptyObjects(
$this->getContent()
);
$decodedResponse = json_decode($this->getContent(), true);

if (is_null($decodedResponse) || $decodedResponse === false) {
if ($this->exception) {
Expand All @@ -701,50 +699,6 @@ public function decodeResponseJson($key = null)
return data_get($decodedResponse, $key);
}

/**
* Decode the JSON string while preserving empty objects.
*
* @param string $json
* @return mixed
*/
public function decodeJsonWhilePreservingEmptyObjects(string $json)
{
$payload = json_decode($json);

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

return $this->parseJsonWhilePreservingEmptyObjects($payload);
}

/**
* Parse the given JSON object while preserving empty objects.
*
* @param \stdClass|array $payload
* @return \stdClass|array
*/
protected function parseJsonWhilePreservingEmptyObjects($payload)
{
if (is_object($payload)) {
$originalPayload = $payload;

$payload = (array) $payload;

if (empty($payload)) {
return $originalPayload;
}
}

foreach ($payload as $key => $item) {
if (is_array($item) || is_object($item)) {
$payload[$key] = $this->parseJsonWhilePreservingEmptyObjects($item);
}
}

return $payload;
}

/**
* Validate and return the decoded response JSON.
*
Expand Down
40 changes: 0 additions & 40 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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 @@ -259,45 +258,6 @@ 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 cc1b312

Please sign in to comment.