Skip to content

Commit

Permalink
Merge 079be9c into 5fcc7a4
Browse files Browse the repository at this point in the history
  • Loading branch information
curzio-della-santa committed Feb 13, 2020
2 parents 5fcc7a4 + 079be9c commit d138e38
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Normalization/NormalizationGroupsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NormalizationGroupsProvider implements
/**
* @return string[]
*/
public function getDefaultGroups()
public function getDefaultGroups(): array
{
return $this->defaultGroups;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Normalization/NormalizationGroupsProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ interface NormalizationGroupsProviderInterface
* @return array
*/
public function getGroups($object);

/**
* @return string[]
*/
public function getDefaultGroups(): array;
}
8 changes: 8 additions & 0 deletions src/View/AcceptsNormalizationGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Detail\Apigility\View;

interface AcceptsNormalizationGroups
{
public function setNormalizationGroups(?array $groups): void;
}
43 changes: 36 additions & 7 deletions src/View/JsonRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

namespace Detail\Apigility\View;

use Zend\View\Model\ModelInterface as ZendModelInterface;
use Zend\View\Renderer\JsonRenderer as BaseJsonRenderer;

use Detail\Normalization\Normalizer\NormalizerAwareInterface;
use Detail\Normalization\Normalizer\NormalizerInterface;

use Detail\Apigility\Normalization\NormalizationGroupsProviderAwareInterface;
use Detail\Apigility\Normalization\NormalizationGroupsProviderInterface;
use Detail\Normalization\Normalizer\NormalizerAwareInterface;
use Detail\Normalization\Normalizer\NormalizerInterface;
use Zend\View\Model\ModelInterface as ZendModelInterface;
use Zend\View\Renderer\JsonRenderer as BaseJsonRenderer;

class JsonRenderer extends BaseJsonRenderer implements
AcceptsNormalizationGroups,
NormalizerAwareInterface,
NormalizationGroupsProviderAwareInterface
{
use NormalizerBasedRendererTrait;
use NormalizerBasedRendererTrait {getNormalizationGroups as private getObjectNormalizationGroups; }

/** @var string[]|null */
private $normalizationGroups;

/**
* @param NormalizerInterface $normalizer
Expand Down Expand Up @@ -51,4 +53,31 @@ public function render($nameOrModel, $values = null)

return parent::render($nameOrModel, $values);
}

public function setNormalizationGroups(?array $normalizationGroups): void
{
if ($normalizationGroups === null) {
$this->normalizationGroups = null;

return;
}

// First get the normalization group default ones
$this->normalizationGroups = $this->getNormalizationGroupsProvider()->getDefaultGroups();

// Validate and apply own normalization groups
foreach ($normalizationGroups as $normalizationGroup) {
$this->normalizationGroups[] = (string) $normalizationGroup;
}
}

/**
* @param mixed $object
* @return array|null
*/
protected function getNormalizationGroups($object)
{
// Own normalization groups take precedence over group-provider defined ones
return $this->normalizationGroups ?? $this->getObjectNormalizationGroups($object);
}
}
85 changes: 85 additions & 0 deletions tests/View/JsonRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace DetailTest\Apigility\View;

use Detail\Apigility\Normalization\NormalizationGroupsProvider;
use Detail\Apigility\View\JsonModel;
use Detail\Apigility\View\JsonRenderer;
use PHPUnit\Framework\TestCase;
use ZF\Hal\Entity as HalEntity;

class JsonRendererTest extends TestCase
{
public function provideEntitiesAndGroups(): \Generator
{
// First two tests are same as in \DetailTest\Apigility\Normalization\NormalizationGroupsProviderTest::testProperGroupNameForEntity
// Needed here to test hat the 'normalisation groups' extension does not mess up when no 'normalisation groups' is provided
yield 'Without own normalisation groups' => [
'Foo',
null,
['Default', 'foo'],
];

yield 'Without own normalisation groups (entity name conversion to snake case)' => [
'FooBar',
null,
['Default', 'foo_bar'],
];

yield 'Own normalisation group' => [
'Foo',
['bar'],
['Default', 'bar'],
];

yield 'Own normalisation groups' => [
'Foo',
['one', 'two'],
['Default', 'one', 'two'],
];
}

/**
* @param string $entityName
* @param array|null $normalizationGroups
* @param array $expectedGroups
* @dataProvider provideEntitiesAndGroups
*/
public function testProperGroupNameForEntity($entityName, ?array $normalizationGroups, array $expectedGroups)
{
$provider = $this->getMockBuilder(NormalizationGroupsProvider::CLASS)
->setMethods(['getEntityName'])
->getMock();

$provider
->expects($this->any())
->method('getEntityName')
->will($this->returnValue($entityName));

$renderer = $this->getMockBuilder(JsonRenderer::CLASS)
->disableOriginalConstructor()
->setMethods(['getNormalizationGroupsProvider', 'normalize'])
->getMock();

$renderer
->expects($this->any())
->method('getNormalizationGroupsProvider')
->will($this->returnValue($provider));

$renderer
->expects($this->atLeastOnce())
->method('normalize')
->willReturnCallback(
function ($object, $groups = null) use ($expectedGroups) { // Return type not important, should be :array|string
$this->assertEquals($expectedGroups, $groups, 'Groups passed to normalization do not match');

return '';
}
);

/** @var JsonRenderer $renderer */

$renderer->setNormalizationGroups($normalizationGroups);
$renderer->render(new JsonModel(['payload' => new HalEntity(new \stdClass())]));
}
}

0 comments on commit d138e38

Please sign in to comment.