Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Bundle/ResourceBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
16 changes: 5 additions & 11 deletions src/Bundle/ResourceBundle/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geloen.eric@gmail.com>
Expand Down Expand Up @@ -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)) {
Expand Down
9 changes: 5 additions & 4 deletions src/Bundle/ResourceBundle/Form/FormFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions src/Bundle/ResourceBundle/Routing/CachedParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Bundle/ResourceBundle/Routing/ParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geloen.eric@gmail.com>
Expand Down Expand Up @@ -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()]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions src/Bundle/ResourceBundle/Tests/Form/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,48 @@ 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()
{
$this->parameterResolver
->expects($this->once())
->method('resolveValidationGroups')
->with($this->identicalTo($resource = $this->createResourceMock()))
->will($this->returnValue($groups = ['group']));

$this->symfonyFormFactory
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand All @@ -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])
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
38 changes: 33 additions & 5 deletions src/Bundle/ResourceBundle/Tests/Routing/ParameterResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geloen.eric@gmail.com>
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions src/Component/Resource/Form/Type/ResourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geloen.eric@gmail.com>
Expand All @@ -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()
Expand Down
9 changes: 1 addition & 8 deletions src/Component/Resource/Tests/Form/Type/ResourceTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geloen.eric@gmail.com>
Expand Down Expand Up @@ -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'));

Expand All @@ -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();
Expand Down