Skip to content

Commit

Permalink
Add tests for TestResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
cretueusebiu committed Jan 31, 2017
1 parent 308aae5 commit 0745d9d
Showing 1 changed file with 85 additions and 0 deletions.
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'],
];
}
}

0 comments on commit 0745d9d

Please sign in to comment.