From c3c737b3d2bc16530cf1d07b4d5befc4ac39e3ef Mon Sep 17 00:00:00 2001 From: Michal Golon Date: Thu, 25 Feb 2016 13:01:06 +0100 Subject: [PATCH] Adds missing methods for serializing null resource. --- src/Resource/ResourceInterface.php | 7 ++++ src/Serializer/ArraySerializer.php | 10 ++++++ src/Serializer/DataArraySerializer.php | 10 ++++++ src/Serializer/SerializerAbstract.php | 7 ++++ test/Serializer/ArraySerializerTest.php | 34 ++++++++++++++++++ test/Serializer/DataArraySerializerTest.php | 39 +++++++++++++++++++++ 6 files changed, 107 insertions(+) diff --git a/src/Resource/ResourceInterface.php b/src/Resource/ResourceInterface.php index e63fb6a4..409d699a 100644 --- a/src/Resource/ResourceInterface.php +++ b/src/Resource/ResourceInterface.php @@ -13,6 +13,13 @@ interface ResourceInterface { + /** + * Get the resource key. + * + * @return string + */ + public function getResourceKey(); + /** * Get the data. * diff --git a/src/Serializer/ArraySerializer.php b/src/Serializer/ArraySerializer.php index 4f49136a..dc8a5279 100644 --- a/src/Serializer/ArraySerializer.php +++ b/src/Serializer/ArraySerializer.php @@ -43,6 +43,16 @@ public function item($resourceKey, array $data) return $data; } + /** + * Serialize null resource. + * + * @return array + */ + public function null() + { + return []; + } + /** * Serialize the included data. * diff --git a/src/Serializer/DataArraySerializer.php b/src/Serializer/DataArraySerializer.php index 5547f7a3..70339833 100644 --- a/src/Serializer/DataArraySerializer.php +++ b/src/Serializer/DataArraySerializer.php @@ -38,4 +38,14 @@ public function item($resourceKey, array $data) { return ['data' => $data]; } + + /** + * Serialize null resource. + * + * @return array + */ + public function null() + { + return ['data' => []]; + } } diff --git a/src/Serializer/SerializerAbstract.php b/src/Serializer/SerializerAbstract.php index dc6083b1..f829998d 100644 --- a/src/Serializer/SerializerAbstract.php +++ b/src/Serializer/SerializerAbstract.php @@ -37,6 +37,13 @@ abstract public function collection($resourceKey, array $data); */ abstract public function item($resourceKey, array $data); + /** + * Serialize null resource. + * + * @return array + */ + abstract public function null(); + /** * Serialize the included data. * diff --git a/test/Serializer/ArraySerializerTest.php b/test/Serializer/ArraySerializerTest.php index cdf8aa5b..bb039a7d 100644 --- a/test/Serializer/ArraySerializerTest.php +++ b/test/Serializer/ArraySerializerTest.php @@ -3,6 +3,7 @@ use League\Fractal\Manager; use League\Fractal\Resource\Collection; use League\Fractal\Resource\Item; +use League\Fractal\Resource\NullResource; use League\Fractal\Scope; use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Test\Stub\Transformer\GenericBookTransformer; @@ -143,6 +144,39 @@ public function testSerializingCollectionResource() $this->assertSame($expectedJson, $scope->toJson()); } + public function testSerializingNullResource() + { + $manager = new Manager(); + $manager->parseIncludes('author'); + $manager->setSerializer(new ArraySerializer()); + + $resource = new NullResource($this->bookCollectionInput, new GenericBookTransformer(), 'books'); + + // Try without metadata + $scope = new Scope($manager, $resource); + + $expected = []; + $this->assertSame($expected, $scope->toArray()); + + // JSON array of JSON objects + $expectedJson = '[]'; + $this->assertSame($expectedJson, $scope->toJson()); + + // Same again with metadata + $resource->setMetaValue('foo', 'bar'); + $scope = new Scope($manager, $resource); + + $expected = [ + 'meta' => [ + 'foo' => 'bar', + ], + ]; + $this->assertSame($expected, $scope->toArray()); + + $expectedJson = '{"meta":{"foo":"bar"}}'; + $this->assertSame($expectedJson, $scope->toJson()); + } + public function testSerializingCollectionResourceWithoutName() { $manager = new Manager(); diff --git a/test/Serializer/DataArraySerializerTest.php b/test/Serializer/DataArraySerializerTest.php index 7dab047d..5de42b51 100644 --- a/test/Serializer/DataArraySerializerTest.php +++ b/test/Serializer/DataArraySerializerTest.php @@ -3,6 +3,7 @@ use League\Fractal\Manager; use League\Fractal\Resource\Collection; use League\Fractal\Resource\Item; +use League\Fractal\Resource\NullResource; use League\Fractal\Scope; use League\Fractal\Serializer\DataArraySerializer; use League\Fractal\Test\Stub\Transformer\GenericBookTransformer; @@ -163,6 +164,44 @@ public function testSerializingCollectionResource() $this->assertSame($expectedJson, $scope->toJson()); } + public function testSerializingNullResource() + { + $manager = new Manager(); + $manager->parseIncludes('author'); + $manager->setSerializer(new DataArraySerializer()); + + $bookData = [ + 'title' => 'Foo', + 'year' => '1991', + '_author' => [ + 'name' => 'Dave', + ], + ]; + + // Try without metadata + $resource = new NullResource($bookData, new GenericBookTransformer(), 'book'); + $scope = new Scope($manager, $resource); + + $expected = [ + 'data' => [], + ]; + $this->assertSame($expected, $scope->toArray()); + + // Same again with metadata + $resource = new NullResource($bookData, new GenericBookTransformer(), 'book'); + $resource->setMetaValue('foo', 'bar'); + + $scope = new Scope($manager, $resource); + + $expected = [ + 'data' => [], + 'meta' => [ + 'foo' => 'bar', + ], + ]; + $this->assertSame($expected, $scope->toArray()); + } + public function tearDown() { Mockery::close();