Skip to content

Commit

Permalink
Merge pull request #324 from RTLer/Hotfix/NullResource
Browse files Browse the repository at this point in the history
Fix when nullResource returned from Transformers
  • Loading branch information
greydnls committed Dec 22, 2016
2 parents 746a07f + 61ec92d commit 6b648b4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Scope.php
Expand Up @@ -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);
}

Expand Down
41 changes: 39 additions & 2 deletions test/ScopeTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -50,7 +53,7 @@ public function testGetResource()
}

/**
* @covers League\Fractal\Scope::toArray
* @covers \League\Fractal\Scope::toArray
*/
public function testToArray()
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions test/Stub/ArraySerializerWithNull.php
@@ -0,0 +1,19 @@
<?php


namespace League\Fractal\Test\Stub;

use League\Fractal\Serializer\ArraySerializer;

class ArraySerializerWithNull extends ArraySerializer
{
/**
* Serialize null resource.
*
* @return null
*/
public function null()
{
return null;
}
}
20 changes: 20 additions & 0 deletions test/Stub/Transformer/NullIncludeBookTransformer.php
@@ -0,0 +1,20 @@
<?php namespace League\Fractal\Test\Stub\Transformer;

use League\Fractal\TransformerAbstract;

class NullIncludeBookTransformer extends TransformerAbstract
{
protected $defaultIncludes = [
'author',
];

public function transform()
{
return ['a' => 'b'];
}

public function includeAuthor()
{
return $this->null();
}
}

0 comments on commit 6b648b4

Please sign in to comment.