Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,42 @@ public function assertJsonStructure(array $structure = null, $responseData = nul
return $this;
}

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

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->assertJsonStructureMissing($structure['*'], $responseDataItem);
}
} elseif (is_array($value)) {
PHPUnit::assertArrayHasKey($key, $responseData);

$this->assertJsonStructureMissing($structure[$key], $responseData[$key]);
} else {
PHPUnit::assertArrayNotHasKey($value, $responseData);
}
}

return $this;
}

/**
* Validate and return the decoded response JSON.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ public function testAssertJsonStructure()
$response->assertJsonStructure(['*' => ['foo', 'bar', 'foobar']]);
}

public function testAssertJsonMissingStructure()
{
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));

// Without structure
$response->assertJsonStructureMissing();

// At root
$response->assertJsonStructureMissing(['foo_bar']);

// Nested
$response->assertJsonStructureMissing(['foobar' => ['foobar_foo_bar', 'foobar_bar_baz']]);

// Wildcard (repeating structure)
$response->assertJsonStructureMissing(['bars' => ['*' => ['baz', 'foobar']]]);

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

// Wildcard (repeating structure) at root
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));

$response->assertJsonStructureMissing(['*' => ['baz', 'qux', 'quux']]);
}

public function testMacroable()
{
TestResponse::macro('foo', function () {
Expand Down