From 967621683d9a2a1b5d8ae9f29a6253d823ce9c2a Mon Sep 17 00:00:00 2001 From: Hannes Kaeufler Date: Sat, 25 Jul 2015 21:22:56 +0200 Subject: [PATCH 1/6] add tests for Criteria --- tests/CriteriaTest.php | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/CriteriaTest.php diff --git a/tests/CriteriaTest.php b/tests/CriteriaTest.php new file mode 100644 index 0000000..c5a2354 --- /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()); + } +} From ec9898ebf574500e28b7363063afe5a57375588b Mon Sep 17 00:00:00 2001 From: Hannes Kaeufler Date: Sat, 25 Jul 2015 21:31:23 +0200 Subject: [PATCH 2/6] add tests for Document --- src/Document.php | 2 +- tests/DocumentTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/DocumentTest.php diff --git a/src/Document.php b/src/Document.php index ceb78d1..9a29e7e 100644 --- a/src/Document.php +++ b/src/Document.php @@ -125,7 +125,7 @@ public function __toString() { return json_encode($this->toArray()); } - + public function jsonSerialize() { return $this->toArray(); diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php new file mode 100644 index 0000000..3b5179c --- /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()); + } +} From 189df8b03dbf7c906a9b59753465b587a44f6d6a Mon Sep 17 00:00:00 2001 From: Hannes Kaeufler Date: Sat, 25 Jul 2015 21:42:52 +0200 Subject: [PATCH 3/6] add Collection tests --- tests/Elements/CollectionTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/Elements/CollectionTest.php diff --git a/tests/Elements/CollectionTest.php b/tests/Elements/CollectionTest.php new file mode 100644 index 0000000..ca91364 --- /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()); + } +} From 2ddf01129cb9ab2cacb5c0be44f028ca67bca9b2 Mon Sep 17 00:00:00 2001 From: Hannes Kaeufler Date: Sat, 25 Jul 2015 21:47:44 +0200 Subject: [PATCH 4/6] fix codestyle error --- src/SerializerAbstract.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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); } From dfbdf529891192330234f9873242aa852b115d1e Mon Sep 17 00:00:00 2001 From: Hannes Kaeufler Date: Sat, 25 Jul 2015 21:47:55 +0200 Subject: [PATCH 5/6] namespace test classes --- tests/CriteriaTest.php | 2 +- tests/DocumentTest.php | 2 +- tests/Elements/CollectionTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/CriteriaTest.php b/tests/CriteriaTest.php index c5a2354..816810a 100644 --- a/tests/CriteriaTest.php +++ b/tests/CriteriaTest.php @@ -1,4 +1,4 @@ - Date: Sat, 25 Jul 2015 21:52:59 +0200 Subject: [PATCH 6/6] break long line --- src/Document.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Document.php b/src/Document.php index 9a29e7e..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;