-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2175 from usu/feature/content-type-entity-path
inject entityPath into ContentType
- Loading branch information
Showing
13 changed files
with
224 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Serializer\Normalizer; | ||
|
||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use App\Entity\ContentType; | ||
use App\Metadata\Resource\Factory\UriTemplateFactory; | ||
use Rize\UriTemplate; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\SerializerAwareInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* Adds API link to contentNodes for ContentType based on the defined 'entityClass'. | ||
*/ | ||
class ContentTypeNormalizer implements NormalizerInterface, SerializerAwareInterface { | ||
public function __construct( | ||
private NormalizerInterface $decorated, | ||
private UriTemplate $uriTemplate, | ||
private UriTemplateFactory $uriTemplateFactory, | ||
private IriConverterInterface $iriConverter, | ||
) { | ||
} | ||
|
||
public function supportsNormalization($data, $format = null) { | ||
return $this->decorated->supportsNormalization($data, $format); | ||
} | ||
|
||
public function normalize($object, $format = null, array $context = []) { | ||
$data = $this->decorated->normalize($object, $format, $context); | ||
|
||
if ($object instanceof ContentType && isset($object->entityClass)) { | ||
// get uri for the respective ContentNode entity and add ContentType as query parameter | ||
[$uriTemplate, $templated] = $this->uriTemplateFactory->createFromResourceClass($object->entityClass); | ||
$uri = $this->uriTemplate->expand($uriTemplate, ['contentType' => $this->iriConverter->getIriFromItem($object)]); | ||
|
||
// add uri as HAL link | ||
$data['_links']['contentNodes']['href'] = $uri; | ||
|
||
// unset the property itself (property definition was only needed to ensure proper API documentation) | ||
unset($data['contentNodes']); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
public function setSerializer(SerializerInterface $serializer) { | ||
if ($this->decorated instanceof SerializerAwareInterface) { | ||
$this->decorated->setSerializer($serializer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
api/tests/Serializer/Normalizer/ContentTypeNormalizerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace App\Tests\Serializer\Normalizer; | ||
|
||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameResolverInterface; | ||
use App\Entity\ContentType; | ||
use App\Metadata\Resource\Factory\UriTemplateFactory; | ||
use App\Serializer\Normalizer\ContentTypeNormalizer; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Rize\UriTemplate; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ContentTypeNormalizerTest extends TestCase { | ||
private ContentTypeNormalizer $normalizer; | ||
|
||
private MockObject|NormalizerInterface $decoratedMock; | ||
private MockObject|RouteNameResolverInterface $routeNameResolver; | ||
private MockObject|RouterInterface $routerMock; | ||
private MockObject|IriConverterInterface $iriConverter; | ||
private MockObject|UriTemplate $uriTemplate; | ||
private MockObject|UriTemplateFactory $uriTemplateFactory; | ||
|
||
protected function setUp(): void { | ||
$this->decoratedMock = $this->createMock(ContextAwareNormalizerInterface::class); | ||
|
||
$this->iriConverter = $this->createMock(IriConverterInterface::class); | ||
$this->uriTemplate = $this->createMock(UriTemplate::class); | ||
$this->uriTemplateFactory = $this->createMock(UriTemplateFactory::class); | ||
|
||
$this->normalizer = new ContentTypeNormalizer( | ||
$this->decoratedMock, | ||
$this->uriTemplate, | ||
$this->uriTemplateFactory, | ||
$this->iriConverter, | ||
); | ||
$this->normalizer->setSerializer($this->createMock(SerializerInterface::class)); | ||
} | ||
|
||
public function testDelegatesSupportCheckToDecorated() { | ||
$this->decoratedMock | ||
->expects($this->exactly(2)) | ||
->method('supportsNormalization') | ||
->willReturnOnConsecutiveCalls(true, false) | ||
; | ||
|
||
$this->assertTrue($this->normalizer->supportsNormalization([])); | ||
$this->assertFalse($this->normalizer->supportsNormalization([])); | ||
} | ||
|
||
public function testDelegatesNormalizeToDecorated() { | ||
// given | ||
$resource = new \stdClass(); | ||
$delegatedResult = [ | ||
'hello' => 'world', | ||
]; | ||
$this->decoratedMock->expects($this->once()) | ||
->method('normalize') | ||
->willReturn($delegatedResult) | ||
; | ||
|
||
// when | ||
$result = $this->normalizer->normalize($resource, null, ['resource_class' => \stdClass::class]); | ||
|
||
// then | ||
$this->assertEquals($delegatedResult, $result); | ||
} | ||
|
||
public function testNormalizeAddsEntityPath() { | ||
// given | ||
$contentType = new ContentType(); | ||
$contentType->entityClass = 'App\Entity\ContentNode\DummyContentNode'; | ||
|
||
$delegatedResult = [ | ||
'hello' => 'world', | ||
]; | ||
$this->decoratedMock->expects($this->once()) | ||
->method('normalize') | ||
->willReturn($delegatedResult) | ||
; | ||
$this->uriTemplateFactory->expects($this->once()) | ||
->method('createFromResourceClass') | ||
->willReturn(['/templatedUri', 'true']) | ||
; | ||
|
||
$this->uriTemplate->expects($this->once()) | ||
->method('expand') | ||
->willReturn('/expandedUri') | ||
; | ||
|
||
// when | ||
$result = $this->normalizer->normalize($contentType, null, ['resource_class' => ContentType::class]); | ||
|
||
// then | ||
$expectedResult = [ | ||
'hello' => 'world', | ||
'_links' => [ | ||
'contentNodes' => [ | ||
'href' => '/expandedUri', | ||
], | ||
], | ||
]; | ||
$this->assertEquals($expectedResult, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters