diff --git a/src/Document.php b/src/Document.php index ceb78d1..f7deb9c 100644 --- a/src/Document.php +++ b/src/Document.php @@ -32,7 +32,8 @@ public function addIncluded($link) foreach ($resources as $k => $resource) { foreach ($this->included as $includedResource) { - if ($includedResource->getType() === $resource->getType() && $includedResource->getId() === $resource->getId()) { + if ($includedResource->getType() === $resource->getType() + && $includedResource->getId() === $resource->getId()) { $includedResource->merge($resource); unset($resources[$k]); break; @@ -125,7 +126,7 @@ public function __toString() { return json_encode($this->toArray()); } - + public function jsonSerialize() { return $this->toArray(); diff --git a/src/SerializerAbstract.php b/src/SerializerAbstract.php index ea3c9c3..73c869e 100644 --- a/src/SerializerAbstract.php +++ b/src/SerializerAbstract.php @@ -116,8 +116,7 @@ public function resource($data) ); } - if ($method && $element) - { + if ($method && $element) { if (! ($element instanceof Relationship)) { $element = new Relationship($element); } diff --git a/tests/CriteriaTest.php b/tests/CriteriaTest.php new file mode 100644 index 0000000..816810a --- /dev/null +++ b/tests/CriteriaTest.php @@ -0,0 +1,62 @@ + 'posts,images']); + + $this->assertEquals(['posts', 'images'], $criteria->getInclude()); + } + + public function testGetSortReturnsArrayOfFieldToSortDirection() + { + $criteria = new Criteria(['sort' => '+firstname']); + + $this->assertEquals(['firstname' => 'asc'], $criteria->getSort()); + } + + public function testGetSortSupportsMultipleSortedFieldsSeparatedByComma() + { + $criteria = new Criteria(['sort' => '+firstname,-lastname']); + + $this->assertEquals(['firstname' => 'asc', 'lastname' => 'desc'], $criteria->getSort()); + } + + public function testGetSortIgnoresInvalidDirections() + { + $criteria = new Criteria(['sort' => '*firstname']); + + $this->assertEmpty($criteria->getSort()); + } + + public function testGetSortDefaultsToEmptyArray() + { + $criteria = new Criteria([]); + + $this->assertEmpty($criteria->getSort()); + } + + public function testGetOffsetParsesThePageOffset() + { + $criteria = new Criteria(['page' => ['offset' => 10]]); + + $this->assertEquals(10, $criteria->getOffset()); + } + + public function testGetOffsetIsAtLeastZero() + { + $criteria = new Criteria(['page' => ['offset' => -5]]); + + $this->assertEquals(0, $criteria->getOffset()); + } + + public function testGetLimitParsesThePageLimit() + { + $criteria = new Criteria(['page' => ['limit' => 100]]); + + $this->assertEquals(100, $criteria->getLimit()); + } +} diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php new file mode 100644 index 0000000..48b2b59 --- /dev/null +++ b/tests/DocumentTest.php @@ -0,0 +1,21 @@ +setData($resource); + + $this->assertEquals(['data' => $resource->toArray()], $document->toArray()); + } + + public function testItCanBeSerializedToJson() + { + $this->assertEquals('[]', (string) new Document()); + } +} diff --git a/tests/Elements/CollectionTest.php b/tests/Elements/CollectionTest.php new file mode 100644 index 0000000..35c89cd --- /dev/null +++ b/tests/Elements/CollectionTest.php @@ -0,0 +1,25 @@ +assertEquals([$post1->toArray(), $post2->toArray()], $collection->toArray()); + } + + public function testGetIdReturnsArrayOfResourceIds() + { + $post1 = new Resource('post', 1); + $post2 = new Resource('post', 2); + $collection = new Collection('post', [$post1, $post2]); + + $this->assertEquals([1, 2], $collection->getId()); + } +}