From 10adcf3b85a56b157565a6b28aa65d28fc5ebd10 Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Wed, 13 Sep 2017 20:25:56 -0700 Subject: [PATCH] DRY-ed up tests --- src/Document.php | 2 +- tests/AbstractTestCase.php | 11 ++++ tests/DocumentTest.php | 109 +++++++++++++++++++++---------------- tests/RelationshipTest.php | 24 +++++--- 4 files changed, 89 insertions(+), 57 deletions(-) diff --git a/src/Document.php b/src/Document.php index e7bf0c2..a5c509a 100644 --- a/src/Document.php +++ b/src/Document.php @@ -132,7 +132,7 @@ public function setFields($fields) /** * Serialize for JSON usage. * - * @return array + * @return object */ public function jsonSerialize() { diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 8a046e0..050bc7d 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -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); + } } diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index be338a0..ecfd37e 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -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() @@ -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() @@ -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() @@ -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() @@ -115,31 +127,34 @@ 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() @@ -147,14 +162,14 @@ 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 = []) diff --git a/tests/RelationshipTest.php b/tests/RelationshipTest.php index c9584bf..340a4b5 100644 --- a/tests/RelationshipTest.php +++ b/tests/RelationshipTest.php @@ -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 + ); } }