Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Add assertJsonStructure method to TestResponse #17700

Merged
merged 2 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ public function assertExactJson(array $data)
return $this;
}

/**
* Assert that the response has a given JSON structure.
*
* @param array|null $structure
* @param array|null $responseData
* @return $this
*/
public function assertJsonStructure(array $structure = null, $responseData = null)
{
if (is_null($structure)) {
return $this->assertJson();
}

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

foreach ($structure as $key => $value) {
if (is_array($value) && $key === '*') {
PHPUnit::assertInternalType('array', $responseData);

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

return $this;
}

/**
* Validate and return the decoded response JSON.
*
Expand Down
85 changes: 85 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Illuminate\Tests\Foundation;

use JsonSerializable;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\TestResponse;

class FoundationTestResponseTest extends TestCase
{
public function testAssertJsonWithArray()
{
$response = new TestResponse(new JsonSerializableSingleResourceStub);

$resource = new JsonSerializableSingleResourceStub;

$response->assertJson($resource->jsonSerialize());
}

public function testAssertJsonWithMixed()
{
$response = new TestResponse(new JsonSerializableMixedResourcesStub);

$resource = new JsonSerializableMixedResourcesStub;

$response->assertJson($resource->jsonSerialize());
}

public function testAssertJsonStructure()
{
$response = new TestResponse(new JsonSerializableMixedResourcesStub);

// At root
$response->assertJsonStructure(['foo']);

// Nested
$response->assertJsonStructure(['foobar' => ['foobar_foo', 'foobar_bar']]);

// Wildcard (repeating structure)
$response->assertJsonStructure(['bars' => ['*' => ['bar', 'foo']]]);

// Nested after wildcard
$response->assertJsonStructure(['baz' => ['*' => ['foo', 'bar' => ['foo', 'bar']]]]);

// Wildcard (repeating structure) at root
$response = new TestResponse(new JsonSerializableSingleResourceStub);
$response->assertJsonStructure(['*' => ['foo', 'bar', 'foobar']]);
}
}

class JsonSerializableMixedResourcesStub implements JsonSerializable
{
public function jsonSerialize()
{
return [
'foo' => 'bar',
'foobar' => [
'foobar_foo' => 'foo',
'foobar_bar' => 'bar',
],
'bars' => [
['bar' => 'foo 0', 'foo' => 'bar 0'],
['bar' => 'foo 1', 'foo' => 'bar 1'],
['bar' => 'foo 2', 'foo' => 'bar 2'],
],
'baz' => [
['foo' => 'bar 0', 'bar' => ['foo' => 'bar 0', 'bar' => 'foo 0']],
['foo' => 'bar 1', 'bar' => ['foo' => 'bar 1', 'bar' => 'foo 1']],
],
];
}
}

class JsonSerializableSingleResourceStub implements JsonSerializable
{
public function jsonSerialize()
{
return [
['foo' => 'foo 0', 'bar' => 'bar 0', 'foobar' => 'foobar 0'],
['foo' => 'foo 1', 'bar' => 'bar 1', 'foobar' => 'foobar 1'],
['foo' => 'foo 2', 'bar' => 'bar 2', 'foobar' => 'foobar 2'],
['foo' => 'foo 3', 'bar' => 'bar 3', 'foobar' => 'foobar 3'],
];
}
}