-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
308aae5
commit 0745d9d
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |