diff --git a/src/CmfResourceRestBundle.php b/src/CmfResourceRestBundle.php index 50a6bf9..07adfb7 100644 --- a/src/CmfResourceRestBundle.php +++ b/src/CmfResourceRestBundle.php @@ -12,14 +12,7 @@ namespace Symfony\Cmf\Bundle\ResourceRestBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Compiler\EnhancerPass; class CmfResourceRestBundle extends Bundle { - public function build(ContainerBuilder $container) - { - $container->addCompilerPass(new EnhancerPass()); - parent::build($container); - } } diff --git a/src/DependencyInjection/CmfResourceRestExtension.php b/src/DependencyInjection/CmfResourceRestExtension.php index b42e1f7..901b67e 100644 --- a/src/DependencyInjection/CmfResourceRestExtension.php +++ b/src/DependencyInjection/CmfResourceRestExtension.php @@ -20,11 +20,6 @@ class CmfResourceRestExtension extends Extension { - private $nativeEnhancers = array( - 'payload', - 'sonata_admin', - ); - /** * {@inheritdoc} */ @@ -41,13 +36,12 @@ public function load(array $configs, ContainerBuilder $container) } $container->setParameter('cmf_resource_rest.max_depth', $config['max_depth']); + $container->setParameter('cmf_resource_rest.expose_payload', $config['expose_payload']); $loader->load('serializer.xml'); $loader->load('resource-rest.xml'); - $this->loadEnhancers($container, $loader, $config['enhancer_map']); $this->configurePayloadAliasRegistry($container, $config['payload_alias_map']); - $this->configureEnhancers($container, $config['enhancer_map']); } public function getNamespace() @@ -60,66 +54,10 @@ public function getXsdValidationBasePath() return __DIR__.'/../Resources/config/schema'; } - /** - * Automatically include native enhancers. - */ - private function loadEnhancers(ContainerBuilder $container, LoaderInterface $loader, $enhancerMap) - { - $loaded = array(); - foreach ($enhancerMap as $unit) { - $enhancerName = $unit['enhancer']; - - if (!in_array($enhancerName, $this->nativeEnhancers)) { - continue; - } - - if (isset($loaded[$enhancerName])) { - continue; - } - - $loader->load('enhancer.'.$enhancerName.'.xml'); - $loaded[$enhancerName] = true; - } - - $bundles = $container->getParameter('kernel.bundles'); - - if (isset($loaded['sonata_admin'])) { - if (!isset($bundles['SonataAdminBundle'])) { - throw new \InvalidArgumentException( - 'You must enable the SonataAdminBundle in order to use the "sonata_admin" enhancer' - ); - } - } - } private function configurePayloadAliasRegistry(ContainerBuilder $container, $aliasMap) { $registry = $container->getDefinition('cmf_resource_rest.registry.payload_alias'); $registry->replaceArgument(1, $aliasMap); } - - private function configureEnhancers(ContainerBuilder $container, $enhancerMap) - { - $enhancerMap = $this->normalizeEnhancerMap($enhancerMap); - $registry = $container->getDefinition('cmf_resource_rest.registry.enhancer'); - $registry->replaceArgument(1, $enhancerMap); - } - - private function normalizeEnhancerMap($enhancerMap) - { - // normalize enhancer map - $normalized = array(); - foreach ($enhancerMap as $enhancerMapping) { - $repository = $enhancerMapping['repository']; - $enhancer = $enhancerMapping['enhancer']; - - if (!isset($normalized[$repository])) { - $normalized[$repository] = array(); - } - - $normalized[$repository][] = $enhancer; - } - - return $normalized; - } } diff --git a/src/DependencyInjection/Compiler/EnhancerPass.php b/src/DependencyInjection/Compiler/EnhancerPass.php deleted file mode 100644 index 058b0f3..0000000 --- a/src/DependencyInjection/Compiler/EnhancerPass.php +++ /dev/null @@ -1,57 +0,0 @@ - - */ -class EnhancerPass implements CompilerPassInterface -{ - public function process(ContainerBuilder $container) - { - if (!$container->has('cmf_resource_rest.registry.enhancer')) { - return; - } - - $taggedIds = $container->findTaggedServiceIds('cmf_resource_rest.enhancer'); - - $repositoryMap = array(); - $aliasMap = array(); - foreach ($taggedIds as $id => $attributes) { - if (!isset($attributes[0]['alias'])) { - throw new InvalidArgumentException(sprintf( - 'Resource enhancer "%s" has no "alias" attribute in its tag', - $id - )); - } - - $name = $attributes[0]['alias']; - - if (isset($aliasMap[$name])) { - throw new InvalidArgumentException(sprintf( - 'Enhancer with name "%s" (id: "%s") has already been registered', - $name, - $id - )); - } - - $aliasMap[$name] = $id; - } - - $registryDef = $container->getDefinition('cmf_resource_rest.registry.enhancer'); - $registryDef->replaceArgument(2, $aliasMap); - } -} diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index d0cbe2c..65dcd11 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -26,9 +26,9 @@ public function getConfigTreeBuilder() $treeBuilder = new TreeBuilder(); $treeBuilder->root('cmf_resource_rest') ->fixXmlConfig('payload_alias', 'payload_alias_map') - ->fixXmlConfig('enhance', 'enhancer_map') ->children() ->integerNode('max_depth')->defaultValue(2)->end() + ->booleanNode('expose_payload')->defaultFalse()->end() ->arrayNode('payload_alias_map') ->useAttributeAsKey('name') ->prototype('array') @@ -38,14 +38,6 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() - ->arrayNode('enhancer_map') - ->prototype('array') - ->children() - ->scalarNode('repository')->isRequired()->end() - ->scalarNode('enhancer')->isRequired()->end() - ->end() - ->end() - ->end() ->end(); return $treeBuilder; diff --git a/src/Enhancer/DescriptionEnhancer.php b/src/Enhancer/DescriptionEnhancer.php deleted file mode 100644 index 3328d4c..0000000 --- a/src/Enhancer/DescriptionEnhancer.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ -class DescriptionEnhancer implements EnhancerInterface -{ - /** - * @var DescriptionEnhancer - */ - private $descriptionFactory; - - public function __construct(DescriptionFactory $descriptionFactory) - { - $this->descriptionFactory = $descriptionFactory; - } - - /** - * {@inheritdoc} - */ - public function enhance(array $data, PuliResource $resource) - { - $data['description'] = $this->descriptionFactory->getPayloadDescriptionFor($resource)->all(); - - return $data; - } -} diff --git a/src/Enhancer/EnhancerInterface.php b/src/Enhancer/EnhancerInterface.php deleted file mode 100644 index de35b45..0000000 --- a/src/Enhancer/EnhancerInterface.php +++ /dev/null @@ -1,34 +0,0 @@ - - */ -interface EnhancerInterface -{ - /** - * Enhance the given serialization context. - * - * For example: - * - * $context->addData('foobar', 'Some value'); - * - * @param array $data Context Serialization context - * @param PuliResource $resource The resource being serialized - */ - public function enhance(array $data, PuliResource $resource); -} diff --git a/src/Enhancer/PayloadEnhancer.php b/src/Enhancer/PayloadEnhancer.php deleted file mode 100644 index d97ba54..0000000 --- a/src/Enhancer/PayloadEnhancer.php +++ /dev/null @@ -1,33 +0,0 @@ - - */ -class PayloadEnhancer implements EnhancerInterface -{ - /** - * {@inheritdoc} - */ - public function enhance(array $data, PuliResource $resource) - { - $payload = $resource->getPayload(); - $data['payload'] = $payload; - - return $data; - } -} diff --git a/src/Enhancer/SonataAdminEnhancer.php b/src/Enhancer/SonataAdminEnhancer.php deleted file mode 100644 index 019b03b..0000000 --- a/src/Enhancer/SonataAdminEnhancer.php +++ /dev/null @@ -1,85 +0,0 @@ - - */ -class SonataAdminEnhancer implements EnhancerInterface -{ - /** - * @var Pool - */ - private $pool; - - /** - * @var UrlGeneratorInterface - */ - private $urlGenerator; - - /** - * __construct. - * - * @param Pool $pool - * @param UrlGeneratorInterface $urlGenerator - */ - public function __construct(Pool $pool, UrlGeneratorInterface $urlGenerator) - { - $this->pool = $pool; - $this->urlGenerator = $urlGenerator; - } - - /** - * {@inheritdoc} - */ - public function enhance(array $data, PuliResource $resource) - { - $object = $resource->getPayload(); - - // sonata has dependency on ClassUtils so this is fine. - $class = ClassUtils::getClass($object); - - if (false === $this->pool->hasAdminByClass($class)) { - return $data; - } - - $admin = $this->pool->getAdminByClass($class); - - $links = array(); - - $routeCollection = $admin->getRoutes(); - - foreach ($routeCollection->getElements() as $code => $route) { - $routeName = $route->getDefault('_sonata_name'); - $url = $this->urlGenerator->generate($routeName, array( - $admin->getIdParameter() => $admin->getUrlsafeIdentifier($object), - ), true); - - $routeRole = substr($code, strlen($admin->getCode()) + 1); - - $links[$routeRole] = $url; - } - - $data['label'] = $admin->toString($object); - $data['sonata_label'] = $admin->getLabel(); - $data['sonata_links'] = $links; - - return $data; - } -} diff --git a/src/Registry/EnhancerRegistry.php b/src/Registry/EnhancerRegistry.php deleted file mode 100644 index aee2bc8..0000000 --- a/src/Registry/EnhancerRegistry.php +++ /dev/null @@ -1,84 +0,0 @@ - - */ -class EnhancerRegistry -{ - /** - * @var array - */ - private $aliasMap = array(); - - /** - * @var array - */ - private $enhancerMap = array(); - - /** - * @var ContainerInterface - */ - private $container; - - /** - * @param ContainerInterface $container The service container - * @param array $enhancerMap Map of enhancer aliases to repository names - * @param array $aliasMap Serice ID map for enhancer aliases - */ - public function __construct(ContainerInterface $container, $enhancerMap = array(), $aliasMap = array()) - { - $this->container = $container; - $this->enhancerMap = $enhancerMap; - $this->aliasMap = $aliasMap; - } - - /** - * Return all of the enhancers which are reigstered against - * the repository with the given alias. - * - * @param string $repositoryAlias - * - * @return EnhancerInterface[] - */ - public function getEnhancers($repositoryAlias) - { - if (!isset($this->enhancerMap[$repositoryAlias])) { - return array(); - } - - $aliases = $this->enhancerMap[$repositoryAlias]; - $enhancers = []; - - foreach ($aliases as $alias) { - if (!isset($this->aliasMap[$alias])) { - throw new \InvalidArgumentException(sprintf( - 'Unknown enhancer alias "%s". Known aliases: "%s"', - implode('", "', array_keys($this->aliasMap)) - )); - } - - $enhancer = $this->container->get( - $this->aliasMap[$alias] - ); - $enhancers[] = $enhancer; - } - - return $enhancers; - } -} diff --git a/src/Resources/config/enhancer.payload.xml b/src/Resources/config/enhancer.payload.xml deleted file mode 100644 index 5ecbc8e..0000000 --- a/src/Resources/config/enhancer.payload.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/Resources/config/enhancer.sonata_admin.xml b/src/Resources/config/enhancer.sonata_admin.xml deleted file mode 100644 index bee238b..0000000 --- a/src/Resources/config/enhancer.sonata_admin.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - diff --git a/src/Resources/config/resource-rest.xml b/src/Resources/config/resource-rest.xml index e279b01..73cdfdf 100644 --- a/src/Resources/config/resource-rest.xml +++ b/src/Resources/config/resource-rest.xml @@ -18,16 +18,5 @@ - - - - - - - - - - - diff --git a/src/Resources/config/schema/resource-rest.xsd b/src/Resources/config/schema/resource-rest.xsd index abfd6ee..54e4731 100644 --- a/src/Resources/config/schema/resource-rest.xsd +++ b/src/Resources/config/schema/resource-rest.xsd @@ -9,7 +9,6 @@ - @@ -20,10 +19,5 @@ - - - - - diff --git a/src/Resources/config/serializer.xml b/src/Resources/config/serializer.xml index 6a6118e..856bb34 100644 --- a/src/Resources/config/serializer.xml +++ b/src/Resources/config/serializer.xml @@ -8,8 +8,9 @@ - + %cmf_resource_rest.max_depth% + %cmf_resource_rest.expose_payload% diff --git a/src/Serializer/Jms/Handler/ResourceHandler.php b/src/Serializer/Jms/Handler/ResourceHandler.php index 0927623..c20cdf2 100644 --- a/src/Serializer/Jms/Handler/ResourceHandler.php +++ b/src/Serializer/Jms/Handler/ResourceHandler.php @@ -17,11 +17,11 @@ use JMS\Serializer\Context; use PHPCR\NodeInterface; use PHPCR\Util\PathHelper; +use Symfony\Cmf\Component\Resource\Description\DescriptionFactory; use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource; use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface; -use Symfony\Cmf\Bundle\ResourceRestBundle\Registry\PayloadAliasRegistry; -use Symfony\Cmf\Bundle\ResourceRestBundle\Registry\EnhancerRegistry; use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource; +use Symfony\Cmf\Bundle\ResourceRestBundle\Registry\PayloadAliasRegistry; /** * Handle PHPCR resource serialization. @@ -32,19 +32,22 @@ class ResourceHandler implements SubscribingHandlerInterface { private $registry; private $payloadAliasRegistry; - private $enhancerRegistry; + private $descriptionFactory; private $maxDepth; + private $exposePayload; public function __construct( RepositoryRegistryInterface $registry, PayloadAliasRegistry $payloadAliasRegistry, - EnhancerRegistry $enhancerRegistry, - $maxDepth = 2 + DescriptionFactory $descriptionFactory, + $maxDepth = 2, + $exposePayload = false ) { $this->registry = $registry; $this->payloadAliasRegistry = $payloadAliasRegistry; - $this->enhancerRegistry = $enhancerRegistry; + $this->descriptionFactory = $descriptionFactory; $this->maxDepth = $maxDepth; + $this->exposePayload = $exposePayload; } public static function getSubscribingMethods() @@ -92,14 +95,16 @@ private function doSerializeResource(PuliResource $resource, $depth = 0) if ($resource instanceof CmfResource) { $data['payload_type'] = $resource->getPayloadType(); + + if ($this->exposePayload && null !== $resource->getPayload()) { + $data['payload'] = $resource->getPayload(); + } } $data['path'] = $resource->getPath(); $data['label'] = $data['node_name'] = PathHelper::getNodeName($data['path']); $data['repository_path'] = $resource->getRepositoryPath(); - $enhancers = $this->enhancerRegistry->getEnhancers($repositoryAlias); - $children = array(); foreach ($resource->listChildren() as $name => $childResource) { $children[$name] = array(); @@ -110,9 +115,8 @@ private function doSerializeResource(PuliResource $resource, $depth = 0) } $data['children'] = $children; - foreach ($enhancers as $enhancer) { - $data = $enhancer->enhance($data, $resource); - } + $description = $this->descriptionFactory->getPayloadDescriptionFor($resource); + $data['descriptors'] = $description->all(); return $data; } diff --git a/tests/Features/nesting.feature b/tests/Features/nesting.feature index b90daae..7ade054 100644 --- a/tests/Features/nesting.feature +++ b/tests/Features/nesting.feature @@ -42,9 +42,11 @@ Feature: Nesting resources "node_name": "sub", "label": "sub", "repository_path": "\/foo\/sub", - "children": [] + "children": [], + "descriptors": [] } - } + }, + "descriptors": [] } """ @@ -63,6 +65,7 @@ Feature: Nesting resources "repository_path": "\/foo", "children": { "sub": [] - } + }, + "descriptors": [] } """ diff --git a/tests/Features/resource_api_phpcr.feature b/tests/Features/resource_api_phpcr.feature index ec6500a..ff5c997 100644 --- a/tests/Features/resource_api_phpcr.feature +++ b/tests/Features/resource_api_phpcr.feature @@ -7,16 +7,14 @@ Feature: PHPCR resource repository Given the test application has the following configuration: """ cmf_resource: + description: { enhancers: [dummy] } repositories: phpcr_repo: type: phpcr/phpcr basepath: /tests/cmf/articles cmf_resource_rest: - enhancer_map: - - - repository: phpcr_repo - enhancer: payload + expose_payload: true """ @@ -36,7 +34,10 @@ Feature: PHPCR resource repository "node_name": "foo", "label": "foo", "repository_path": "\/foo", - "children": [] + "children": [], + "descriptors": { + "name_reverse": "oof" + } } """ diff --git a/tests/Features/resource_api_phpcr_odm.feature b/tests/Features/resource_api_phpcr_odm.feature index 399108f..c896e8b 100644 --- a/tests/Features/resource_api_phpcr_odm.feature +++ b/tests/Features/resource_api_phpcr_odm.feature @@ -7,6 +7,7 @@ Feature: PHPCR-ODM resource repository Given the test application has the following configuration: """ cmf_resource: + description: { enhancers: [dummy] } repositories: phpcrodm_repo: type: doctrine/phpcr-odm @@ -37,7 +38,10 @@ Feature: PHPCR-ODM resource repository "node_name": "foo", "label": "foo", "repository_path": "\/foo", - "children": [] + "children": [], + "descriptors": { + "name_reverse": "oof" + } } """ @@ -74,7 +78,8 @@ Feature: PHPCR-ODM resource repository "node_name": "bar", "label": "bar", "repository_path": "/foo/bar", - "children": [ ] + "children": [ ], + "descriptors": { "name_reverse": "rab" } }, "boo": { "repository_alias": "phpcrodm_repo", @@ -85,8 +90,12 @@ Feature: PHPCR-ODM resource repository "node_name": "boo", "label": "boo", "repository_path": "/foo/boo", - "children": [ ] + "children": [ ], + "descriptors": { "name_reverse": "oob" } } + }, + "descriptors": { + "name_reverse": "oof" } } """ diff --git a/tests/Resources/TestBundle/Description/DummyEnhancer.php b/tests/Resources/TestBundle/Description/DummyEnhancer.php new file mode 100644 index 0000000..87a86b6 --- /dev/null +++ b/tests/Resources/TestBundle/Description/DummyEnhancer.php @@ -0,0 +1,30 @@ +set('name_reverse', strrev($description->getResource()->getName())); + } + + public function supports(PuliResource $resource) + { + return $resource instanceof CmfResource; + } +} diff --git a/tests/Resources/app/config/config.php b/tests/Resources/app/config/config.php index b3f1239..f616b24 100644 --- a/tests/Resources/app/config/config.php +++ b/tests/Resources/app/config/config.php @@ -10,6 +10,7 @@ */ use Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\Security\ResourceVoter; +use Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Resources\TestBundle\Description\DummyEnhancer; $container->setParameter('cmf_testing.bundle_fqn', 'Symfony\Cmf\Bundle\ResourceRestBundle'); $loader->import(CMF_TEST_CONFIG_DIR.'/dist/parameters.yml'); @@ -21,3 +22,6 @@ $container->register('app.resource_voter', ResourceVoter::class) ->addTag('security.voter'); + +$container->register('app.dummy_enhancer', DummyEnhancer::class) + ->addTag('cmf_resource.description.enhancer', ['alias' => 'dummy']); diff --git a/tests/Unit/DependencyInjection/CmfResourceRestExtensionTest.php b/tests/Unit/DependencyInjection/CmfResourceRestExtensionTest.php index c89f264..219c840 100644 --- a/tests/Unit/DependencyInjection/CmfResourceRestExtensionTest.php +++ b/tests/Unit/DependencyInjection/CmfResourceRestExtensionTest.php @@ -13,7 +13,6 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\CmfResourceRestExtension; -use Symfony\Cmf\Bundle\ResourceRestBundle\DependencyInjection\Compiler\EnhancerPass; class CmfResourceRestExtensionTest extends AbstractExtensionTestCase { @@ -33,12 +32,6 @@ public function provideExtension() 'type' => 'Article', ), ), - 'enhancer_map' => array( - array( - 'repository' => 'some_repo', - 'enhancer' => 'payload', - ), - ), ), ), ); @@ -50,7 +43,6 @@ public function provideExtension() public function testExtension($config) { $this->container->setParameter('kernel.bundles', ['JMSSerializerBundle' => true]); - $this->container->addCompilerPass(new EnhancerPass()); $this->load($config); diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 7f1b0b8..1efd0d2 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -47,13 +47,8 @@ public function testConfig($source) 'type' => 'Namespace\Article', ), ), - 'enhancer_map' => array( - array( - 'repository' => 'my_repo', - 'enhancer' => 'my_enhancer', - ), - ), 'max_depth' => 2, + 'expose_payload' => false, ), array($source)); } } diff --git a/tests/Unit/DependencyInjection/fixtures/config.xml b/tests/Unit/DependencyInjection/fixtures/config.xml index d02761f..f0f78b0 100644 --- a/tests/Unit/DependencyInjection/fixtures/config.xml +++ b/tests/Unit/DependencyInjection/fixtures/config.xml @@ -5,6 +5,5 @@ http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> - diff --git a/tests/Unit/DependencyInjection/fixtures/config.yml b/tests/Unit/DependencyInjection/fixtures/config.yml index bd2ca6f..33ffc0a 100644 --- a/tests/Unit/DependencyInjection/fixtures/config.yml +++ b/tests/Unit/DependencyInjection/fixtures/config.yml @@ -3,7 +3,3 @@ cmf_resource_rest: article: repository: doctrine_phpcr_odm type: Namespace\Article - enhancer_map: - - - enhancer: my_enhancer - repository: my_repo diff --git a/tests/Unit/Enhancer/SonataAdminEnhancerTest.php b/tests/Unit/Enhancer/SonataAdminEnhancerTest.php deleted file mode 100644 index 4e2f6d4..0000000 --- a/tests/Unit/Enhancer/SonataAdminEnhancerTest.php +++ /dev/null @@ -1,94 +0,0 @@ -admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin'); - $this->pool = $this->prophesize('Sonata\AdminBundle\Admin\Pool'); - $this->visitor = $this->prophesize('JMS\Serializer\GenericSerializationVisitor'); - $this->generator = $this->prophesize('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); - $this->payload = new \stdClass(); - $this->resource = $this->prophesize('Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource'); - $this->route1 = $this->prophesize('Symfony\Component\Routing\Route'); - $this->route2 = $this->prophesize('Symfony\Component\Routing\Route'); - $this->routeCollection = $this->prophesize('Sonata\AdminBundle\Route\RouteCollection'); - - $this->enhancer = new SonataAdminEnhancer($this->pool->reveal(), $this->generator->reveal()); - - $this->admin->getRoutes()->willReturn( - $this->routeCollection->reveal() - ); - } - - public function testEnhancerNoHasClass() - { - $this->pool->hasAdminByClass('stdClass')->willReturn(false); - $this->resource->getPayload()->willReturn($this->payload); - - $result = $this->enhancer->enhance( - array(), - $this->resource->reveal() - ); - } - - public function testEnhancer() - { - $this->pool->hasAdminByClass('stdClass')->willReturn(true); - $this->pool->getAdminByClass('stdClass')->willReturn($this->admin); - - $this->resource->getPayload()->willReturn($this->payload); - $data = array(); - $this->routeCollection->getElements()->willReturn( - array( - 'admin.code.edit' => $this->route1, - 'admin.code.delete' => $this->route2, - ) - ); - $this->admin->getCode()->willReturn('admin.code'); - - $this->route1->getDefault('_sonata_name')->willReturn('route_to_edit'); - $this->route2->getDefault('_sonata_name')->willReturn('route_to_delete'); - - $this->admin->getIdParameter()->willReturn('id'); - $this->admin->getUrlsafeIdentifier($this->payload)->willReturn('/path/to'); - - $this->generator->generate('route_to_edit', array('id' => '/path/to'), true)->willReturn('http://edit'); - $this->generator->generate('route_to_delete', array('id' => '/path/to'), true)->willReturn('http://delete'); - - $this->admin->getIdParameter()->willReturn('id'); - $this->admin->getLabel()->willReturn('Admin Label'); - $this->admin->toString($this->payload)->willReturn('Admin'); - - $result = $this->enhancer->enhance( - $data, - $this->resource->reveal() - ); - - $this->assertEquals(array( - 'label' => 'Admin', - 'sonata_label' => 'Admin Label', - 'sonata_links' => array( - 'edit' => 'http://edit', - 'delete' => 'http://delete', - ), - ), $result); - } -} diff --git a/tests/Unit/Registry/EnhancerRegistryTest.php b/tests/Unit/Registry/EnhancerRegistryTest.php deleted file mode 100644 index 3be8953..0000000 --- a/tests/Unit/Registry/EnhancerRegistryTest.php +++ /dev/null @@ -1,64 +0,0 @@ -container = $this->prophesize('Symfony\Component\DependencyInjection\ContainerInterface'); - } - - public function provideRegistry() - { - return array( - array( - array( - 'phpcr_repo' => array('enhancer_1', 'enhancer_2'), - ), - array( - 'enhancer_1' => 'service_id.enhancer_1', - 'enhancer_2' => 'service_id.enhancer_2', - ), - 'phpcr_repo', - ), - ); - } - - /** - * @dataProvider provideRegistry - */ - public function testRegistryGet($enhancerMap, $aliasMap, $target) - { - $registry = $this->createRegistry($enhancerMap, $aliasMap); - - $enhancers = array(); - foreach ($aliasMap as $alias => $serviceId) { - $enhancer = $this->prophesize('Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer\EnhancerInterface'); - $this->container->get($serviceId)->willReturn( - $enhancer - ); - } - - $result = $registry->getEnhancers($target); - $this->assertCount(count($enhancerMap[$target]), $result); - } - - private function createRegistry($enhancerMap, $aliasMap) - { - return new EnhancerRegistry($this->container->reveal(), $enhancerMap, $aliasMap); - } -} diff --git a/tests/Unit/Serializer/Jms/Handler/ResourceHandlerTest.php b/tests/Unit/Serializer/Jms/Handler/ResourceHandlerTest.php index c2904e0..757db6c 100644 --- a/tests/Unit/Serializer/Jms/Handler/ResourceHandlerTest.php +++ b/tests/Unit/Serializer/Jms/Handler/ResourceHandlerTest.php @@ -12,30 +12,49 @@ namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Serializer\Handler; use Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Jms\Handler\ResourceHandler; +use Symfony\Cmf\Component\Resource\Description\DescriptionFactory; +use Symfony\Cmf\Component\Resource\Description\Description; +use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface; +use Symfony\Cmf\Bundle\ResourceRestBundle\Registry\PayloadAliasRegistry; +use JMS\Serializer\JsonSerializationVisitor; +use JMS\Serializer\Context; +use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource; +use Symfony\Cmf\Component\Resource\Puli\Api\ResourceRepository; use Prophecy\Argument; class ResourceHandlerTest extends \PHPUnit_Framework_TestCase { - public function setUp() + private $repositoryRegistry; + private $payloadAliasRegistry; + private $descriptionFactory; + private $visitor; + private $resource; + private $childResource; + private $repository; + private $context; + private $handler; + private $description; + + protected function setUp() { - parent::setUp(); + $this->repositoryRegistry = $this->prophesize(RepositoryRegistryInterface::class); + $this->payloadAliasRegistry = $this->prophesize(PayloadAliasRegistry::class); + $this->visitor = $this->prophesize(JsonSerializationVisitor::class); + $this->resource = $this->prophesize(CmfResource::class); + $this->childResource = $this->prophesize(CmfResource::class); - $this->repositoryRegistry = $this->prophesize('Symfony\Cmf\Component\Resource\RepositoryRegistryInterface'); - $this->payloadAliasRegistry = $this->prophesize('Symfony\Cmf\Bundle\ResourceRestBundle\Registry\PayloadAliasRegistry'); - $this->enhancerRegistry = $this->prophesize('Symfony\Cmf\Bundle\ResourceRestBundle\Registry\EnhancerRegistry'); - $this->enhancer = $this->prophesize('Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer\EnhancerInterface'); - $this->visitor = $this->prophesize('JMS\Serializer\JsonSerializationVisitor'); - $this->resource = $this->prophesize('Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource'); - $this->childResource = $this->prophesize('Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource'); + $this->repository = $this->prophesize(ResourceRepository::class); + $this->context = $this->prophesize(Context::class); - $this->repository = $this->prophesize('Symfony\Cmf\Component\Resource\Puli\Api\ResourceRepository'); - $this->payload = new \stdClass(); - $this->context = $this->prophesize('JMS\Serializer\Context'); + $this->description = $this->prophesize(Description::class); + $this->description->all()->willReturn([]); + $this->descriptionFactory = $this->prophesize(DescriptionFactory::class); + $this->descriptionFactory->getPayloadDescriptionFor(Argument::any())->willReturn($this->description->reveal()); $this->handler = new ResourceHandler( $this->repositoryRegistry->reveal(), $this->payloadAliasRegistry->reveal(), - $this->enhancerRegistry->reveal() + $this->descriptionFactory->reveal() ); $this->resource->getRepository()->willReturn($this->repository); @@ -47,6 +66,7 @@ public function testHandler() $this->repositoryRegistry->getRepositoryType($this->repository)->willReturn('repo_type'); $this->payloadAliasRegistry->getPayloadAlias($this->resource->reveal())->willReturn('alias'); $this->resource->getPayloadType()->willReturn('payload_type'); + $this->resource->getPayload()->willReturn(null); $this->resource->getPath()->willReturn('/path/to'); $this->resource->getRepositoryPath()->willReturn('/repository/path'); $this->resource->listChildren()->willReturn(array( @@ -55,20 +75,13 @@ public function testHandler() $this->payloadAliasRegistry->getPayloadAlias($this->childResource->reveal())->willReturn('alias'); $this->childResource->getPayloadType()->willReturn('payload_type'); + $this->childResource->getPayload()->willReturn(null); $this->childResource->getPath()->willReturn('/path/to/child'); $this->childResource->getRepositoryPath()->willReturn('/child/repository/path'); $this->childResource->getRepository()->willReturn($this->repository->reveal()); $this->childResource->listChildren()->willReturn(array( )); - $this->enhancerRegistry->getEnhancers('repo')->willReturn(array( - $this->enhancer, - )); - $this->enhancer->enhance(Argument::type('array'), Argument::type('Symfony\Cmf\Component\Resource\Puli\Api\PuliResource')) - ->will(function ($data, $resource) { - return $data[0]; - }); - $expected = array( 'repository_alias' => 'repo', 'repository_type' => 'repo_type', @@ -89,8 +102,10 @@ public function testHandler() 'node_name' => 'child', 'repository_path' => '/child/repository/path', 'children' => array(), + 'descriptors' => [], ), ), + 'descriptors' => [], ); $this->context->accept($expected)->shouldBeCalled();