From f6e8eed8e952c64ddc57971cd000989172338941 Mon Sep 17 00:00:00 2001 From: GeLo Date: Thu, 19 May 2016 17:13:47 +0200 Subject: [PATCH] [ResourceBundle] Generate validation groups in the parameter resolver --- .../ResourceBundle/Controller/Controller.php | 2 +- .../ResourceBundle/Form/FormFactory.php | 16 +++----- .../Form/FormFactoryInterface.php | 9 +++-- .../EventSubscriber/GridViewSubscriber.php | 2 +- .../Routing/CachedParameterResolver.php | 6 +-- .../Routing/ParameterResolver.php | 5 ++- .../Routing/ParameterResolverInterface.php | 4 +- .../Tests/Form/FormFactoryTest.php | 31 +++++++-------- .../GridViewSubscriberTest.php | 15 ++++++++ .../Routing/CachedParameterResolverTest.php | 5 ++- .../Tests/Routing/ParameterResolverTest.php | 38 ++++++++++++++++--- .../Resource/Form/Type/ResourceType.php | 4 -- .../Tests/Form/Type/ResourceTypeTest.php | 9 +---- 13 files changed, 89 insertions(+), 57 deletions(-) diff --git a/src/Bundle/ResourceBundle/Controller/Controller.php b/src/Bundle/ResourceBundle/Controller/Controller.php index ef28241..ed40ae5 100644 --- a/src/Bundle/ResourceBundle/Controller/Controller.php +++ b/src/Bundle/ResourceBundle/Controller/Controller.php @@ -225,7 +225,7 @@ private function find($action, $mandatory = true) */ private function buildForm($form = null, $object = null, array $options = []) { - return $this->getFormFactory()->create($form ?: $this->resource, $object, $options); + return $this->getFormFactory()->create($this->resource, $form, $object, $options); } /** diff --git a/src/Bundle/ResourceBundle/Form/FormFactory.php b/src/Bundle/ResourceBundle/Form/FormFactory.php index 231b42c..ae6a729 100644 --- a/src/Bundle/ResourceBundle/Form/FormFactory.php +++ b/src/Bundle/ResourceBundle/Form/FormFactory.php @@ -14,8 +14,6 @@ use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; use Lug\Component\Resource\Model\ResourceInterface; use Symfony\Component\Form\FormFactoryInterface as SymfonyFormFactoryInterface; -use Symfony\Component\Form\FormTypeInterface; -use Symfony\Component\Form\Test\FormInterface; /** * @author GeLo @@ -43,19 +41,15 @@ public function __construct(SymfonyFormFactoryInterface $factory, ParameterResol } /** - * @param string|FormTypeInterface|ResourceInterface $type - * @param mixed $data - * @param mixed[] $options - * - * @return FormInterface + * {@inheritdoc} */ - public function create($type = null, $data = null, array $options = []) + public function create(ResourceInterface $resource, $type = null, $data = null, array $options = []) { - if ($type instanceof ResourceInterface) { - $type = $this->parameterResolver->resolveForm($type); + if ($type === null) { + $type = $this->parameterResolver->resolveForm($resource); } - $validationGroups = $this->parameterResolver->resolveValidationGroups(); + $validationGroups = $this->parameterResolver->resolveValidationGroups($resource); $translationDomain = $this->parameterResolver->resolveTranslationDomain(); if (!empty($validationGroups)) { diff --git a/src/Bundle/ResourceBundle/Form/FormFactoryInterface.php b/src/Bundle/ResourceBundle/Form/FormFactoryInterface.php index f3362cc..fe57fbb 100644 --- a/src/Bundle/ResourceBundle/Form/FormFactoryInterface.php +++ b/src/Bundle/ResourceBundle/Form/FormFactoryInterface.php @@ -21,11 +21,12 @@ interface FormFactoryInterface { /** - * @param string|FormTypeInterface|ResourceInterface $type - * @param mixed $data - * @param mixed[] $options + * @param ResourceInterface $resource + * @param string|FormTypeInterface|null $type + * @param mixed $data + * @param mixed[] $options * * @return FormInterface */ - public function create($type = null, $data = null, array $options = []); + public function create(ResourceInterface $resource, $type = null, $data = null, array $options = []); } diff --git a/src/Bundle/ResourceBundle/Rest/View/EventSubscriber/GridViewSubscriber.php b/src/Bundle/ResourceBundle/Rest/View/EventSubscriber/GridViewSubscriber.php index 21f1c5d..00973f1 100644 --- a/src/Bundle/ResourceBundle/Rest/View/EventSubscriber/GridViewSubscriber.php +++ b/src/Bundle/ResourceBundle/Rest/View/EventSubscriber/GridViewSubscriber.php @@ -95,7 +95,7 @@ public function onView(ViewEvent $event) if ($grid->getBatchForm() === null) { $batchForm = !isset($data['batch_form']) || !$data['batch_form'] instanceof FormInterface - ? $this->formFactory->create(GridBatchType::class, null, ['grid' => $grid]) + ? $this->formFactory->create($event->getResource(), GridBatchType::class, null, ['grid' => $grid]) : $data['batch_form']; $grid->setBatchForm($batchForm->createView()); diff --git a/src/Bundle/ResourceBundle/Routing/CachedParameterResolver.php b/src/Bundle/ResourceBundle/Routing/CachedParameterResolver.php index 40c0972..0b94f04 100644 --- a/src/Bundle/ResourceBundle/Routing/CachedParameterResolver.php +++ b/src/Bundle/ResourceBundle/Routing/CachedParameterResolver.php @@ -248,10 +248,10 @@ public function resolveTranslationDomain() /** * {@inheritdoc} */ - public function resolveValidationGroups() + public function resolveValidationGroups(ResourceInterface $resource) { - return !isset($this->cache[$key = 'validation_groups']) - ? $this->cache[$key] = $this->parameterResolver->resolveValidationGroups() + return !isset($this->cache[$key = 'validation_groups_'.spl_object_hash($resource)]) + ? $this->cache[$key] = $this->parameterResolver->resolveValidationGroups($resource) : $this->cache[$key]; } diff --git a/src/Bundle/ResourceBundle/Routing/ParameterResolver.php b/src/Bundle/ResourceBundle/Routing/ParameterResolver.php index a640a98..1a60027 100644 --- a/src/Bundle/ResourceBundle/Routing/ParameterResolver.php +++ b/src/Bundle/ResourceBundle/Routing/ParameterResolver.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Symfony\Component\Validator\Constraint; /** * @author GeLo @@ -295,9 +296,9 @@ public function resolveTranslationDomain() /** * {@inheritdoc} */ - public function resolveValidationGroups() + public function resolveValidationGroups(ResourceInterface $resource) { - return $this->resolveParameter('validation_groups', []); + return $this->resolveParameter('validation_groups', [Constraint::DEFAULT_GROUP, 'lug.'.$resource->getName()]); } /** diff --git a/src/Bundle/ResourceBundle/Routing/ParameterResolverInterface.php b/src/Bundle/ResourceBundle/Routing/ParameterResolverInterface.php index 42d039b..97dae65 100644 --- a/src/Bundle/ResourceBundle/Routing/ParameterResolverInterface.php +++ b/src/Bundle/ResourceBundle/Routing/ParameterResolverInterface.php @@ -127,9 +127,11 @@ public function resolveThemes(); public function resolveTranslationDomain(); /** + * @param ResourceInterface $resource + * * @return string|null */ - public function resolveValidationGroups(); + public function resolveValidationGroups(ResourceInterface $resource); /** * @return bool diff --git a/src/Bundle/ResourceBundle/Tests/Form/FormFactoryTest.php b/src/Bundle/ResourceBundle/Tests/Form/FormFactoryTest.php index 5ab07e0..666b559 100644 --- a/src/Bundle/ResourceBundle/Tests/Form/FormFactoryTest.php +++ b/src/Bundle/ResourceBundle/Tests/Form/FormFactoryTest.php @@ -53,40 +53,40 @@ public function testInheritance() $this->assertInstanceOf(BundleFormFactoryInterface::class, $this->formFactory); } - public function testCreateWithStringType() + public function testCreateWithResourceType() { + $this->parameterResolver + ->expects($this->once()) + ->method('resolveForm') + ->with($this->identicalTo($resource = $this->createResourceMock())) + ->will($this->returnValue($resourceType = 'resource_type')); + $this->symfonyFormFactory ->expects($this->once()) ->method('create') ->with( - $this->identicalTo($type = 'type'), + $this->identicalTo($resourceType), $this->identicalTo($data = 'data'), $this->identicalTo($options = ['option']) ) ->will($this->returnValue($form = 'form')); - $this->assertSame($form, $this->formFactory->create($type, $data, $options)); + $this->assertSame($form, $this->formFactory->create($resource, null, $data, $options)); } - public function testCreateWithResourceType() + public function testCreateWithStringType() { - $this->parameterResolver - ->expects($this->once()) - ->method('resolveForm') - ->with($this->identicalTo($type = $this->createResourceMock())) - ->will($this->returnValue($resourceType = 'resource_type')); - $this->symfonyFormFactory ->expects($this->once()) ->method('create') ->with( - $this->identicalTo($resourceType), + $this->identicalTo($type = 'type'), $this->identicalTo($data = 'data'), $this->identicalTo($options = ['option']) ) ->will($this->returnValue($form = 'form')); - $this->assertSame($form, $this->formFactory->create($type, $data, $options)); + $this->assertSame($form, $this->formFactory->create($this->createResourceMock(), $type, $data, $options)); } public function testCreateWithValidationGroups() @@ -94,6 +94,7 @@ public function testCreateWithValidationGroups() $this->parameterResolver ->expects($this->once()) ->method('resolveValidationGroups') + ->with($this->identicalTo($resource = $this->createResourceMock())) ->will($this->returnValue($groups = ['group'])); $this->symfonyFormFactory @@ -106,7 +107,7 @@ public function testCreateWithValidationGroups() ) ->will($this->returnValue($form = 'form')); - $this->assertSame($form, $this->formFactory->create($type, $data, $options)); + $this->assertSame($form, $this->formFactory->create($resource, $type, $data, $options)); } public function testCreateWithTranslationDomain() @@ -126,7 +127,7 @@ public function testCreateWithTranslationDomain() ) ->will($this->returnValue($form = 'form')); - $this->assertSame($form, $this->formFactory->create($type, $data, $options)); + $this->assertSame($form, $this->formFactory->create($this->createResourceMock(), $type, $data, $options)); } public function testCreateWithApi() @@ -146,7 +147,7 @@ public function testCreateWithApi() ) ->will($this->returnValue($form = 'form')); - $this->assertSame($form, $this->formFactory->create($type, $data, $options)); + $this->assertSame($form, $this->formFactory->create($this->createResourceMock(), $type, $data, $options)); } /** diff --git a/src/Bundle/ResourceBundle/Tests/Rest/View/EventSubscriber/GridViewSubscriberTest.php b/src/Bundle/ResourceBundle/Tests/Rest/View/EventSubscriber/GridViewSubscriberTest.php index e2b690f..58482aa 100644 --- a/src/Bundle/ResourceBundle/Tests/Rest/View/EventSubscriber/GridViewSubscriberTest.php +++ b/src/Bundle/ResourceBundle/Tests/Rest/View/EventSubscriber/GridViewSubscriberTest.php @@ -19,6 +19,7 @@ use Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber\GridViewSubscriber; use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent; use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; +use Lug\Component\Resource\Model\ResourceInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormRendererInterface; @@ -201,6 +202,11 @@ public function testViewWithoutBatchForm() ->method('getView') ->will($this->returnValue($view = $this->createViewMock())); + $event + ->expects($this->once()) + ->method('getResource') + ->will($this->returnValue($resource = $this->createResourceMock())); + $view ->expects($this->once()) ->method('getData') @@ -210,6 +216,7 @@ public function testViewWithoutBatchForm() ->expects($this->once()) ->method('create') ->with( + $this->identicalTo($resource), $this->identicalTo(GridBatchType::class), $this->isNull(), $this->identicalTo(['grid' => $gridView]) @@ -387,6 +394,14 @@ private function createViewEventMock() ->getMock(); } + /** + * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface + */ + private function createResourceMock() + { + return $this->getMock(ResourceInterface::class); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|View */ diff --git a/src/Bundle/ResourceBundle/Tests/Routing/CachedParameterResolverTest.php b/src/Bundle/ResourceBundle/Tests/Routing/CachedParameterResolverTest.php index d2c318b..c45fd6c 100644 --- a/src/Bundle/ResourceBundle/Tests/Routing/CachedParameterResolverTest.php +++ b/src/Bundle/ResourceBundle/Tests/Routing/CachedParameterResolverTest.php @@ -304,10 +304,11 @@ public function testResolveValidationGroups() $this->parameterResolver ->expects($this->once()) ->method('resolveValidationGroups') + ->with($this->identicalTo($resource = $this->createResourceMock())) ->will($this->returnValue($groups = ['group'])); - $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups()); - $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups()); + $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups($resource)); + $this->assertSame($groups, $this->cachedParameterResolver->resolveValidationGroups($resource)); } public function testResolveVoter() diff --git a/src/Bundle/ResourceBundle/Tests/Routing/ParameterResolverTest.php b/src/Bundle/ResourceBundle/Tests/Routing/ParameterResolverTest.php index 3d0e7ae..07e69cd 100644 --- a/src/Bundle/ResourceBundle/Tests/Routing/ParameterResolverTest.php +++ b/src/Bundle/ResourceBundle/Tests/Routing/ParameterResolverTest.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; +use Symfony\Component\Validator\Constraint; /** * @author GeLo @@ -1076,7 +1077,16 @@ public function testResolveTranslationDomainExplicitWithGrid() public function testResolveValidationGroupsWithoutRequest() { - $this->assertEmpty($this->parameterResolver->resolveValidationGroups()); + $resource = $this->createResourceMock(); + $resource + ->expects($this->once()) + ->method('getName') + ->will($this->returnValue($name = 'name')); + + $this->assertSame( + [Constraint::DEFAULT_GROUP, 'lug.'.$name], + $this->parameterResolver->resolveValidationGroups($resource) + ); } public function testResolveValidationGroupsDefault() @@ -1086,13 +1096,22 @@ public function testResolveValidationGroupsDefault() ->method('getMasterRequest') ->will($this->returnValue($request = $this->createRequestMock())); + $resource = $this->createResourceMock(); + $resource + ->expects($this->once()) + ->method('getName') + ->will($this->returnValue($name = 'name')); + $request->attributes ->expects($this->once()) ->method('get') - ->with($this->identicalTo('_lug_validation_groups'), $this->identicalTo($groups = [])) + ->with( + $this->identicalTo('_lug_validation_groups'), + $this->identicalTo($groups = [Constraint::DEFAULT_GROUP, 'lug.'.$name]) + ) ->will($this->returnValue($groups)); - $this->assertEmpty($this->parameterResolver->resolveValidationGroups()); + $this->assertSame($groups, $this->parameterResolver->resolveValidationGroups($resource)); } public function testResolveValidationGroupsExplicit() @@ -1102,13 +1121,22 @@ public function testResolveValidationGroupsExplicit() ->method('getMasterRequest') ->will($this->returnValue($request = $this->createRequestMock())); + $resource = $this->createResourceMock(); + $resource + ->expects($this->once()) + ->method('getName') + ->will($this->returnValue($name = 'name')); + $request->attributes ->expects($this->once()) ->method('get') - ->with($this->identicalTo('_lug_validation_groups'), $this->identicalTo([])) + ->with( + $this->identicalTo('_lug_validation_groups'), + $this->identicalTo([Constraint::DEFAULT_GROUP, 'lug.'.$name]) + ) ->will($this->returnValue($groups = ['group'])); - $this->assertSame($groups, $this->parameterResolver->resolveValidationGroups()); + $this->assertSame($groups, $this->parameterResolver->resolveValidationGroups($resource)); } public function testResolveVoterWithoutRequest() diff --git a/src/Component/Resource/Form/Type/ResourceType.php b/src/Component/Resource/Form/Type/ResourceType.php index cc895f1..e02ffba 100644 --- a/src/Component/Resource/Form/Type/ResourceType.php +++ b/src/Component/Resource/Form/Type/ResourceType.php @@ -17,7 +17,6 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\Component\Validator\Constraint; /** * @author GeLo @@ -40,9 +39,6 @@ public function configureOptions(OptionsResolver $resolver) 'label_prefix' => function (Options $options) { return 'lug.'.$options['resource']->getName(); }, - 'validation_groups' => function (Options $options) { - return [Constraint::DEFAULT_GROUP, 'lug.'.$options['resource']->getName()]; - }, 'empty_data' => function (FormInterface $form) { return $form->isRequired() || !$form->isEmpty() ? $form->getConfig()->getOption('factory')->create() diff --git a/src/Component/Resource/Tests/Form/Type/ResourceTypeTest.php b/src/Component/Resource/Tests/Form/Type/ResourceTypeTest.php index bc93b39..81e708a 100644 --- a/src/Component/Resource/Tests/Form/Type/ResourceTypeTest.php +++ b/src/Component/Resource/Tests/Form/Type/ResourceTypeTest.php @@ -17,7 +17,6 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\Forms; -use Symfony\Component\Validator\Constraint; /** * @author GeLo @@ -55,7 +54,7 @@ public function testSubmit() { $resource = $this->createResourceMock(); $resource - ->expects($this->exactly(2)) + ->expects($this->once()) ->method('getName') ->will($this->returnValue($name = 'name')); @@ -81,12 +80,6 @@ public function testSubmit() $this->assertSame($factory, $form->getConfig()->getOption('factory')); $this->assertSame($model, $form->getConfig()->getOption('data_class')); $this->assertSame('lug.'.$name, $form->getConfig()->getOption('label_prefix')); - - $this->assertSame( - [Constraint::DEFAULT_GROUP, 'lug.'.$name], - $form->getConfig()->getOption('validation_groups') - ); - $this->assertSame($object, $form->getData()); $form->createView();