Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
PHPCR-ODM enhancer
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Jun 20, 2016
1 parent 0265227 commit c520f48
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Description/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
63 changes: 63 additions & 0 deletions Description/Enhancer/Doctrine/PhpcrOdm/PhpcrOdmEnhancer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Resource\Description\Enhancer\Doctrine\PhpcrOdm;

use Sylius\Component\Resource\Metadata\RegistryInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
use Symfony\Cmf\Component\Resource\Description\Description;
use Puli\Repository\Api\Resource\PuliResource;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactory;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Cmf\Component\Resource\Description\Descriptor;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory;
use Doctrine\Common\Util\ClassUtils;

/**
* Add descriptors from the Doctrine PHPCR ODM
*
* @author Daniel Leech <[email protected]>
*/
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)));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Resource\Tests\Unit\Description\Enhancer;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Cmf\Component\Resource\Description\Description;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Prophecy\Argument;
use Symfony\Cmf\Component\Resource\Description\Descriptor;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory;
use Symfony\Cmf\Component\Resource\Description\Enhancer\Doctrine\PhpcrOdm\PhpcrOdmEnhancer;
use Puli\Repository\Api\Resource\PuliResource;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;

class PhpcrOdmEnhancerTest extends \PHPUnit_Framework_TestCAse
{
private $metadataFactory;
private $enhancer;
private $cmfResource;
private $puliResource;
private $odmMetadata;
private $description;

public function setUp()
{
$this->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());
}
}

0 comments on commit c520f48

Please sign in to comment.