Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.
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
2 changes: 1 addition & 1 deletion src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function setFields($fields)
/**
* Serialize for JSON usage.
*
* @return array
* @return object
*/
public function jsonSerialize()
{
Expand Down
11 changes: 11 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@

abstract class AbstractTestCase extends PHPUnit_Framework_TestCase
{
/**
* Asserts the two values encode to json and produce same result.
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public static function assertProduceSameJson($expected, $actual, $message = '')
{
self::assertJsonStringEqualsJsonString(json_encode($expected), json_encode($actual), $message);
}
}
109 changes: 62 additions & 47 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public function testResource()

$document = Document::fromData($resource);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => ['type' => 'a', 'id' => '1']
]), json_encode($document));
$this->assertProduceSameJson(
[
'data' => ['type' => 'a', 'id' => '1'],
],
$document
);
}

public function testCollection()
Expand All @@ -35,12 +38,15 @@ public function testCollection()

$document = Document::fromData([$resource1, $resource2]);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => [
['type' => 'a', 'id' => '1'],
['type' => 'a', 'id' => '2']
]
]), json_encode($document));
$this->assertProduceSameJson(
[
'data' => [
['type' => 'a', 'id' => '1'],
['type' => 'a', 'id' => '2'],
],
],
$document
);
}

public function testMergeResource()
Expand All @@ -53,17 +59,20 @@ public function testMergeResource()

$document = Document::fromData([$resource1, $resource2]);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => [
[
'type' => 'a',
'id' => '1',
'attributes' => $merged = array_merge($array1, $array2),
'meta' => $merged,
'links' => $merged
]
]
]), json_encode($document));
$this->assertProduceSameJson(
[
'data' => [
[
'type' => 'a',
'id' => '1',
'attributes' => $merged = array_merge($array1, $array2),
'meta' => $merged,
'links' => $merged,
],
],
],
$document
);
}

public function testSparseFieldsets()
Expand All @@ -75,13 +84,16 @@ public function testSparseFieldsets()
$document = Document::fromData($resource);
$document->setFields(['a' => ['present']]);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => [
'type' => 'a',
'id' => '1',
'attributes' => ['present' => 1]
]
]), json_encode($document));
$this->assertProduceSameJson(
[
'data' => [
'type' => 'a',
'id' => '1',
'attributes' => ['present' => 1],
],
],
$document
);
}

public function testIncludeRelationships()
Expand Down Expand Up @@ -115,46 +127,49 @@ public function testIncludeRelationships()
$document = Document::fromData($resource1);
$document->setInclude(['a', 'a.b']);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => [
'type' => 'a',
'id' => '1',
'relationships' => ['a' => $relationshipJson]
],
'included' => [
[
'type' => 'b',
'id' => '1'
],
[
$this->assertProduceSameJson(
[
'data' => [
'type' => 'a',
'id' => '2',
'relationships' => ['b' => $relationshipJson]
]
]
]), json_encode($document));
'id' => '1',
'relationships' => ['a' => $relationshipJson],
],
'included' => [
[
'type' => 'b',
'id' => '1',
],
[
'type' => 'a',
'id' => '2',
'relationships' => ['b' => $relationshipJson],
],
],
],
$document
);
}

public function testErrors()
{
$document = Document::fromErrors(['a']);

$this->assertJsonStringEqualsJsonString(json_encode(['errors' => ['a']]), json_encode($document));
$this->assertProduceSameJson(['errors' => ['a']], $document);
}

public function testLinks()
{
$document = Document::fromMeta([]);
$document->setLink('a', 'b');

$this->assertJsonStringEqualsJsonString(json_encode(['links' => ['a' => 'b']]), json_encode($document));
$this->assertProduceSameJson(['links' => ['a' => 'b']], $document);
}

public function testMeta()
{
$document = Document::fromMeta(['a' => 'b']);

$this->assertJsonStringEqualsJsonString(json_encode(['meta' => ['a' => 'b']]), json_encode($document));
$this->assertProduceSameJson(['meta' => ['a' => 'b']], $document);
}

private function mockResource($type, $id, $attributes = [], $meta = [], $links = [])
Expand Down
24 changes: 15 additions & 9 deletions tests/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ public function testJsonSerialize()

$relationship = Relationship::fromData($resource1);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => ['type' => 'stub', 'id' => '1']
]), json_encode($relationship));
$this->assertProduceSameJson(
[
'data' => ['type' => 'stub', 'id' => '1'],
],
$relationship
);

$relationship = Relationship::fromData([$resource1, $resource2]);

$this->assertJsonStringEqualsJsonString(json_encode([
'data' => [
['type' => 'stub', 'id' => '1'],
['type' => 'stub', 'id' => '1']
]
]), json_encode($relationship));
$this->assertProduceSameJson(
[
'data' => [
['type' => 'stub', 'id' => '1'],
['type' => 'stub', 'id' => '1'],
],
],
$relationship
);
}
}

Expand Down