diff --git a/src/Scope.php b/src/Scope.php index 027f9290..0191e2dc 100644 --- a/src/Scope.php +++ b/src/Scope.php @@ -273,6 +273,14 @@ public function toArray() // Pull out all of OUR metadata and any custom meta data to merge with the main level data $meta = $serializer->meta($this->resource->getMeta()); + // in case of returning NullResource we should return null and not to go with array_merge + if (is_null($data)) { + if (!empty($meta)) { + return $meta; + } + return null; + } + return array_merge($data, $meta); } diff --git a/test/ScopeTest.php b/test/ScopeTest.php index 6ee3c213..73a76ce9 100755 --- a/test/ScopeTest.php +++ b/test/ScopeTest.php @@ -4,9 +4,12 @@ use League\Fractal\Pagination\Cursor; 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\ArraySerializerWithNull; use League\Fractal\Test\Stub\Transformer\DefaultIncludeBookTransformer; +use League\Fractal\Test\Stub\Transformer\NullIncludeBookTransformer; use Mockery; class ScopeTest extends \PHPUnit_Framework_TestCase @@ -50,7 +53,7 @@ public function testGetResource() } /** - * @covers League\Fractal\Scope::toArray + * @covers \League\Fractal\Scope::toArray */ public function testToArray() { @@ -299,7 +302,7 @@ public function testRunAppropriateTransformerWithCollection() } /** - * @covers League\Fractal\Scope::executeResourceTransformers + * @covers \League\Fractal\Scope::executeResourceTransformers * @expectedException InvalidArgumentException * @expectedExceptionMessage Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection */ @@ -428,6 +431,40 @@ public function testDefaultIncludeSuccess() $this->assertSame($expected, $scope->toArray()); } + public function testNullResourceIncludeSuccess() + { + $manager = new Manager(); + $manager->setSerializer(new ArraySerializerWithNull); + + // Send this stub junk, it has a specific format anyhow + $resource = new Item([], new NullIncludeBookTransformer); + + // Try without metadata + $scope = new Scope($manager, $resource); + $expected = [ + 'a' => 'b', + 'author' => null, + ]; + + $this->assertSame($expected, $scope->toArray()); + } + + /** + * @covers \League\Fractal\Scope::toArray + */ + public function testNullResourceDataAndJustMeta() + { + $manager = new Manager(); + $manager->setSerializer(new ArraySerializerWithNull); + + $resource = new NullResource(); + $resource->setMeta(['foo' => 'bar']); + + $scope = new Scope($manager, $resource); + + $this->assertSame(['meta' => ['foo' => 'bar']], $scope->toArray()); + } + public function tearDown() { Mockery::close(); diff --git a/test/Stub/ArraySerializerWithNull.php b/test/Stub/ArraySerializerWithNull.php new file mode 100644 index 00000000..9943ae61 --- /dev/null +++ b/test/Stub/ArraySerializerWithNull.php @@ -0,0 +1,19 @@ + 'b']; + } + + public function includeAuthor() + { + return $this->null(); + } +}