Skip to content

Commit

Permalink
Merge pull request #277 from HydrefLab/fix-for-null-resource
Browse files Browse the repository at this point in the history
Adds missing methods for serializing null resource.
  • Loading branch information
willishq committed Mar 3, 2016
2 parents 7c3e00f + c3c737b commit 57f6ab4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Resource/ResourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

interface ResourceInterface
{
/**
* Get the resource key.
*
* @return string
*/
public function getResourceKey();

/**
* Get the data.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Serializer/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Serializer/DataArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ public function item($resourceKey, array $data)
{
return ['data' => $data];
}

/**
* Serialize null resource.
*
* @return array
*/
public function null()
{
return ['data' => []];
}
}
7 changes: 7 additions & 0 deletions src/Serializer/SerializerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
34 changes: 34 additions & 0 deletions test/Serializer/ArraySerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions test/Serializer/DataArraySerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 57f6ab4

Please sign in to comment.