Skip to content

Commit

Permalink
Unit tests for sparse fieldsets
Browse files Browse the repository at this point in the history
  • Loading branch information
ghunti authored and greydnls committed Feb 22, 2017
1 parent 6438f5c commit f3aacbd
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ public function parseIncludes($includes)
*/
public function parseFieldsets(array $fieldsets)
{
$this->requestedFieldsets = [];
foreach ($fieldsets as $type => $fields) {
$this->requestedFieldsets[$type] = explode(',', $fields);
//Remove empty and repeated fields
$this->requestedFieldsets[$type] = array_unique(array_filter(explode(',', $fields)));
}
return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ public function toArray()
// If the serializer wants the includes to be side-loaded then we'll
// serialize the included data and merge it with the data.
if ($serializer->sideloadIncludes()) {
$includedData = $serializer->includedData($this->resource, $rawIncludedData);

//Filter out any relation that wasn't requested
$rawIncludedData = array_map(array($this, 'filterFieldsets'), $rawIncludedData);

$includedData = $serializer->includedData($this->resource, $rawIncludedData);

// If the serializer wants to inject additional information
// about the included resources, it can do so now.
$data = $serializer->injectData($data, $rawIncludedData);
Expand Down
34 changes: 33 additions & 1 deletion test/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace League\Fractal\Test;

use League\Fractal\Manager;
use League\Fractal\ParamBag;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use Mockery;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function testParseIncludes()
$this->assertNull($params['totallymadeup']);
}

public function testParseExcludeSelfie()
public function testParseExcludeSelfie()
{
$manager = new Manager();

Expand Down Expand Up @@ -195,6 +196,37 @@ public function testCreateDataWithCallback()

}

public function testParseFieldsets()
{
$manager = new Manager();

$fields = [
'articles' => 'title,body',
'people' => 'name'
];

$expectedFieldset = [
'articles' => ['title' , 'body'],
'people' => ['name']
];

$manager->parseFieldsets($fields);
$this->assertSame($expectedFieldset, $manager->getRequestedFieldsets());

$paramBag = new ParamBag($expectedFieldset['articles']);
$this->assertEquals($paramBag, $manager->getFieldset('articles'));

// Are repeated fields stripped
$manager->parseFieldsets(['foo' => 'bar,baz,bar']);
$this->assertSame(['foo' => ['bar', 'baz']], $manager->getRequestedFieldsets());

// Are empty fields stripped
$manager->parseFieldsets(['foo' => 'bar,']);
$this->assertSame(['foo' => ['bar']], $manager->getRequestedFieldsets());

$this->assertSame(null, $manager->getFieldset('inexistent'));
}

public function tearDown()
{
Mockery::close();
Expand Down
180 changes: 180 additions & 0 deletions test/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,188 @@ public function testNullResourceDataAndJustMeta()
$this->assertSame(['meta' => ['foo' => 'bar']], $scope->toArray());
}

/**
* @covers League\Fractal\Scope::toArray
* @dataProvider fieldsetsProvider
*/
public function testToArrayWithFieldsets($fieldsetsToParse, $expected)
{
$manager = new Manager();

$resource = new Item(
['foo' => 'bar', 'baz' => 'qux'],
function ($data) {
return $data;
},
'resourceName'
);

$scope = new Scope($manager, $resource);

$manager->parseFieldsets($fieldsetsToParse);
$this->assertSame($expected, $scope->toArray());
}

public function fieldsetsProvider()
{
return [
[
['resourceName' => 'foo'],
['data' => ['foo' => 'bar']]
],
[
['resourceName' => 'foo,baz'],
['data' => ['foo' => 'bar', 'baz' => 'qux']]
],
[
['resourceName' => 'inexistentField'],
['data' => []]
]
];
}

/**
* @covers League\Fractal\Scope::toArray
* @dataProvider fieldsetsWithMandatorySerializerFieldsProvider
*/
public function testToArrayWithFieldsetsAndMandatorySerializerFields($fieldsetsToParse, $expected)
{
$serializer = Mockery::mock('League\Fractal\Serializer\DataArraySerializer')->makePartial();
$serializer->shouldReceive('getMandatoryFields')->andReturn(['foo']);

$resource = new Item(
['foo' => 'bar', 'baz' => 'qux'],
function ($data) {
return $data;
},
'resourceName'
);

$manager = new Manager();
$manager->setSerializer($serializer);
$scope = new Scope($manager, $resource);

$manager->parseFieldsets($fieldsetsToParse);
$this->assertSame($expected, $scope->toArray());
}

public function fieldsetsWithMandatorySerializerFieldsProvider()
{
return [
//Don't request for mandatory field
[
['resourceName' => 'baz'],
['data' => ['foo' => 'bar', 'baz' => 'qux']]
],
//Request required field anyway
[
['resourceName' => 'foo,baz'],
['data' => ['foo' => 'bar', 'baz' => 'qux']]
]
];
}

/**
* @dataProvider fieldsetsWithIncludesProvider
*/
public function testToArrayWithIncludesAndFieldsets($fieldsetsToParse, $expected)
{
$transformer = $this->createTransformerWithIncludedResource('book', ['book' => ['yin' => 'yang']]);

$resource = new Item(
['foo' => 'bar', 'baz' => 'qux'],
$transformer,
'resourceName'
);
$manager = new Manager();
$scope = new Scope($manager, $resource);

$manager->parseIncludes('book');

$manager->parseFieldsets($fieldsetsToParse);
$this->assertSame($expected, $scope->toArray());
}

public function fieldsetsWithIncludesProvider()
{
return [
//Included relation was not requested
[
['resourceName' => 'foo'],
['data' => ['foo' => 'bar']]
],
//Included relation was requested
[
['resourceName' => 'foo,book', 'book' => 'yin'],
['data' => ['foo' => 'bar', 'book' => ['yin' => 'yang']]]
]
];
}

/**
* @covers League\Fractal\Scope::toArray
* @dataProvider fieldsetsWithSideLoadIncludesProvider
*/
public function testToArrayWithSideloadedIncludesAndFieldsets($fieldsetsToParse, $expected)
{
$serializer = Mockery::mock('League\Fractal\Serializer\DataArraySerializer')->makePartial();
$serializer->shouldReceive('sideloadIncludes')->andReturn(true);
$serializer->shouldReceive('item')->andReturnUsing(
function ($key, $data) {
return ['data' => $data];
}
);
$serializer->shouldReceive('includedData')->andReturnUsing(
function ($key, $data) {
$data = array_pop($data);
return empty($data) ? [] : ['sideloaded' => $data];
}
);

$manager = new Manager();
$manager->parseIncludes('book');
$manager->setSerializer($serializer);

$transformer = $this->createTransformerWithIncludedResource('book', ['book' => ['yin' => 'yang']]);

$resource = new Item(['foo' => 'bar'], $transformer, 'resourceName');
$scope = new Scope($manager, $resource);

$manager->parseFieldsets($fieldsetsToParse);
$this->assertSame($expected, $scope->toArray());
}

public function fieldsetsWithSideLoadIncludesProvider()
{
return [
//Included relation was not requested
[
['resourceName' => 'foo'],
['data' => ['foo' => 'bar']]
],
//Included relation was requested
[
['resourceName' => 'foo,book', 'book' => 'yin'],
['data' => ['foo' => 'bar'], 'sideloaded' => ['book' => ['yin' => 'yang']]]
]
];
}

public function tearDown()
{
Mockery::close();
}

protected function createTransformerWithIncludedResource($resourceName, $transformResult)
{
$transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
$transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn([$resourceName]);
$transformer->shouldReceive('transform')->once()->andReturnUsing(
function (array $data) {
return $data;
}
);
$transformer->shouldReceive('processIncludedResources')->once()->andReturn($transformResult);
return $transformer;
}
}
Loading

0 comments on commit f3aacbd

Please sign in to comment.