This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Explode class metadata - Do not generate for non-existing routes - Added support for children links (Sylius)
- Loading branch information
Showing
8 changed files
with
334 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?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; | ||
|
||
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 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()); | ||
$childClasses = $metadata->getChildClasses(); | ||
$childTypes = []; | ||
|
||
// explode the allowed types into concrete classes | ||
foreach ($this->metadataFactory->getAllMetadata() as $childMetadata) { | ||
foreach ($childClasses as $childClass) { | ||
if ($childClass == $childMetadata->name || $childMetadata->getReflectionClass()->isSubclassOf($childClass)) { | ||
$childTypes[] = $childMetadata->name; | ||
} | ||
} | ||
} | ||
|
||
$description->set(Descriptor::CHILDREN_ALLOW, !$metadata->isLeaf()); | ||
$description->set(Descriptor::CHILDREN_TYPES, $childTypes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(PuliResource $resource) | ||
{ | ||
if (false === $resource instanceof CmfResource) { | ||
return false; | ||
} | ||
|
||
return $this->metadataFactory->hasMetadataFor(ClassUtils::getRealClass($resource->getPayloadType())); | ||
} | ||
} |
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
143 changes: 143 additions & 0 deletions
143
Tests/Unit/Description/Enhancer/Doctrine/PhpcrOdmEnhancerTest.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,143 @@ | ||
<?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\Doctrine; | ||
|
||
use Symfony\Cmf\Component\Resource\Description\Description; | ||
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource; | ||
use Symfony\Cmf\Component\Resource\Description\Descriptor; | ||
use Doctrine\ODM\PHPCR\Mapping\ClassMetadataFactory; | ||
use Symfony\Cmf\Component\Resource\Description\Enhancer\Doctrine\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->cmfResource->getPayloadType()->willReturn(\stdClass::class); | ||
$this->metadataFactory->hasMetadataFor(\stdClass::class)->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->cmfResource->getPayloadType()->willReturn(\stdClass::class); | ||
$this->metadataFactory->hasMetadataFor(\stdClass::class)->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() | ||
{ | ||
// object the implements an allowed interface | ||
$mappedObject1 = $this->prophesize(); | ||
$mappedObject1->willImplement(FooInterface::class); | ||
$metadata1 = $this->prophesize(ClassMetadata::class); | ||
$metadata1->name = get_class($mappedObject1->reveal()); | ||
$metadata1->getReflectionClass()->willReturn(new \ReflectionClass($metadata1->name)); | ||
|
||
// object the extends an allowed abstract class | ||
$mappedObject2 = $this->prophesize(); | ||
$mappedObject2->willExtend(AbstractFoo::class); | ||
$metadata2 = $this->prophesize(ClassMetadata::class); | ||
$metadata2->name = get_class($mappedObject2->reveal()); | ||
$metadata2->getReflectionClass()->willReturn(new \ReflectionClass($metadata2->name)); | ||
|
||
// object of exact type that is allowed | ||
$mappedObject3 = $this->prophesize(); | ||
$metadata3 = $this->prophesize(ClassMetadata::class); | ||
$metadata3->name = get_class($mappedObject3->reveal()); | ||
$metadata3->getReflectionClass()->willReturn(new \ReflectionClass($metadata3->reveal())); | ||
|
||
// object that is not permitted | ||
$metadata4 = $this->prophesize(ClassMetadata::class); | ||
$metadata4->name = NotAllowedFoo::class; | ||
$metadata4->getReflectionClass()->willReturn(new \ReflectionClass($metadata4->reveal())); | ||
|
||
$this->description->getResource()->willReturn($this->cmfResource->reveal()); | ||
$this->cmfResource->getPayloadType()->willReturn('payload_type'); | ||
$this->metadataFactory->getMetadataFor('payload_type')->willReturn($this->odmMetadata->reveal()); | ||
$this->metadataFactory->getAllMetadata()->willReturn([ | ||
$metadata1->reveal(), | ||
$metadata2->reveal(), | ||
$metadata3->reveal(), | ||
]); | ||
|
||
$this->odmMetadata->isLeaf()->willReturn(false); | ||
$this->odmMetadata->getChildClasses()->willReturn([ | ||
FooInterface::class, | ||
AbstractFoo::class, | ||
$metadata3->name, | ||
]); | ||
|
||
$this->description->set(Descriptor::CHILDREN_ALLOW, true)->shouldBeCalled(); | ||
$this->description->set(Descriptor::CHILDREN_TYPES, [ | ||
$metadata1->name, | ||
$metadata2->name, | ||
$metadata3->name, | ||
])->shouldBeCalled(); | ||
$this->enhancer->enhance($this->description->reveal()); | ||
} | ||
} | ||
|
||
interface FooInterface | ||
{ | ||
} | ||
|
||
abstract class AbstractFoo | ||
{ | ||
} | ||
|
||
class NotAllowedFoo | ||
{ | ||
} |
Oops, something went wrong.