diff --git a/Description/Descriptor.php b/Description/Descriptor.php index 097d591..9b0e2b1 100644 --- a/Description/Descriptor.php +++ b/Description/Descriptor.php @@ -51,4 +51,11 @@ final class Descriptor const LINK_REMOVE_REST = 'link.remove.rest'; const LINK_SHOW_REST = 'link.show.rest'; const LINK_LIST_REST = 'link.show.rest'; + + /** + * Permitted children types for this resource. + * Value is an array. + */ + const CHILDREN_TYPES = 'children.types'; + const CHILDREN_ALLOW = 'children.allow'; } diff --git a/Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancer.php b/Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancer.php new file mode 100644 index 0000000..e5b48b6 --- /dev/null +++ b/Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancer.php @@ -0,0 +1,63 @@ + + */ +class PhpcrOdmEnhancer implements DescriptionEnhancerInterface +{ + private $metadataFactory; + + public function __construct(ClassMetadataFactory $metadataFactory) + { + $this->metadataFactory = $metadataFactory; + } + + /** + * {@inheritdoc} + */ + public function enhance(Description $description) + { + $metadata = $this->metadataFactory->getMetadataFor($description->getResource()->getPayloadType()); + + $description->set(Descriptor::CHILDREN_ALLOW, !$metadata->isLeaf()); + $description->set(Descriptor::CHILDREN_TYPES, $metadata->getChildClasses()); + } + + /** + * {@inheritdoc} + */ + public function supports(PuliResource $resource) + { + if (false === $resource instanceof CmfResource) { + return false; + } + + return $this->metadataFactory->hasMetadataFor(ClassUtils::getRealClass(get_class($resource))); + } +} + diff --git a/Tests/Unit/Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancerTest.php b/Tests/Unit/Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancerTest.php new file mode 100644 index 0000000..34527b1 --- /dev/null +++ b/Tests/Unit/Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancerTest.php @@ -0,0 +1,99 @@ +metadataFactory = $this->prophesize(ClassMetadataFactory::class); + $this->enhancer = new PhpcrOdmEnhancer($this->metadataFactory->reveal()); + + $this->cmfResource = $this->prophesize(CmfResource::class); + $this->puliResource = $this->prophesize(PuliResource::class); + $this->odmMetadata = $this->prophesize(ClassMetadata::class); + $this->description = $this->prophesize(Description::class); + } + + /** + * It should return true it supports a given resource. + */ + public function testSupportsResource() + { + $this->metadataFactory->hasMetadataFor(get_class($this->cmfResource->reveal()))->willReturn(true); + + $result = $this->enhancer->supports($this->cmfResource->reveal()); + $this->assertTrue($result); + } + + /** + * It should return false if the resource is not an instance of CmfResource. + */ + public function testNotSupportsNonCmfResource() + { + $this->assertFalse( + $this->enhancer->supports($this->puliResource->reveal()) + ); + } + + /** + * It should return false if the resource is not known by the PHPCR-ODM metadata factory + */ + public function testNotSupportsNotSupportedByPhpcrOdm() + { + $this->metadataFactory->hasMetadataFor(get_class($this->cmfResource->reveal()))->willReturn(false); + + $this->assertFalse( + $this->enhancer->supports($this->cmfResource->reveal()) + ); + } + + /** + * It should enhance the description with the child mapping information from the PHPCR-ODM metadata. + */ + public function testEnhanceDescription() + { + $this->description->getResource()->willReturn($this->cmfResource->reveal()); + $this->cmfResource->getPayloadType()->willReturn('payload_type'); + $this->metadataFactory->getMetadataFor('payload_type')->willReturn($this->odmMetadata->reveal()); + $this->odmMetadata->isLeaf()->willReturn(false); + $this->odmMetadata->getChildClasses()->willReturn([ + 'class_1', + 'class_2', + ]); + + $this->description->set(Descriptor::CHILDREN_ALLOW, true)->shouldBeCalled(); + $this->description->set(Descriptor::CHILDREN_TYPES, [ + 'class_1', + 'class_2', + ])->shouldBeCalled(); + $this->enhancer->enhance($this->description->reveal()); + } +}