From eec798cfd3b908de56b591a406e8f5f50a9237df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 1 Sep 2021 14:04:40 +0200 Subject: [PATCH] entity person: create a validator to check a person entity is linked with a center This validator take a parameter in configuration --- .../ChillPersonExtension.php | 3 ++ .../DependencyInjection/Configuration.php | 33 ++++++------ .../Person/PersonHasCenterValidatorTest.php | 50 +++++++++++++++++++ .../Constraints/Person/PersonHasCenter.php | 15 ++++++ .../Person/PersonHasCenterValidator.php | 41 +++++++++++++++ 5 files changed, 127 insertions(+), 15 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Validator/Person/PersonHasCenterValidatorTest.php create mode 100644 src/Bundle/ChillPersonBundle/Validator/Constraints/Person/PersonHasCenter.php create mode 100644 src/Bundle/ChillPersonBundle/Validator/Constraints/Person/PersonHasCenterValidator.php diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index 07d5dc88f..48ac993c3 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -60,6 +60,9 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $container->setParameter('chill_person.allow_multiple_simultaneous_accompanying_periods', $config['allow_multiple_simultaneous_accompanying_periods']); + // register all configuration in a unique parameter + $container->setParameter('chill_person', $config); + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config')); $loader->load('services.yaml'); $loader->load('services/widgets.yaml'); diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/Configuration.php b/src/Bundle/ChillPersonBundle/DependencyInjection/Configuration.php index 927612d5f..7daec4f63 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/Configuration.php @@ -44,22 +44,25 @@ class Configuration implements ConfigurationInterface ->canBeDisabled() ->children() ->scalarNode('birthdate_not_after') - ->info($this->validationBirthdateNotAfterInfos) - ->defaultValue('P1D') - ->validate() - ->ifTrue(function($period) { - try { - $interval = new \DateInterval($period); - } catch (\Exception $ex) { - return true; - } - return false; - }) - ->thenInvalid('Invalid period for birthdate validation : "%s" ' - . 'The parameter should match duration as defined by ISO8601 : ' - . 'https://en.wikipedia.org/wiki/ISO_8601#Durations') + ->info($this->validationBirthdateNotAfterInfos) + ->defaultValue('P1D') + ->validate() + ->ifTrue(function($period) { + try { + $interval = new \DateInterval($period); + } catch (\Exception $ex) { + return true; + } + return false; + }) + ->thenInvalid('Invalid period for birthdate validation : "%s" ' + . 'The parameter should match duration as defined by ISO8601 : ' + . 'https://en.wikipedia.org/wiki/ISO_8601#Durations') ->end() // birthdate_not_after, parent = children of validation - + ->booleanNode('center_required') + ->info('Enable a center for each person entity. If disabled, you must provide your own center provider') + ->defaultValue(true) + ->end() ->end() // children for 'validation', parent = validation ->end() //validation, parent = children of root ->end() // children of root, parent = root diff --git a/src/Bundle/ChillPersonBundle/Tests/Validator/Person/PersonHasCenterValidatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Validator/Person/PersonHasCenterValidatorTest.php new file mode 100644 index 000000000..8cd0e6b83 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Validator/Person/PersonHasCenterValidatorTest.php @@ -0,0 +1,50 @@ +getConstraint(); + $personHasCenter = (new Person())->setCenter(new Center()); + $personNoCenter = new Person(); + + $this->validator->validate($personHasCenter, $constraint); + $this->assertNoViolation(); + + $this->validator->validate($personNoCenter, $constraint); + $this->buildViolation('msg') + ->atPath('property.path.center') + ->assertRaised(); + } + + protected function getConstraint() + { + return new PersonHasCenter([ + 'message' => 'msg' + ]); + } + + protected function createValidator() + { + $parameterBag = $this->createMock(ParameterBagInterface::class); + $parameterBag + ->method('get') + ->with($this->equalTo('chill_person')) + ->willReturn([ + 'validation' => [ + 'center_required' => true + ] + ]) + ; + + return new PersonHasCenterValidator($parameterBag); + } +} diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/Person/PersonHasCenter.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/Person/PersonHasCenter.php new file mode 100644 index 000000000..c6c8c7c14 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/Person/PersonHasCenter.php @@ -0,0 +1,15 @@ +centerRequired = $parameterBag->get('chill_person')['validation']['center_required']; + } + + /** + * @inheritDoc + */ + public function validate($person, Constraint $constraint) + { + if (!$person instanceof Person) { + throw new UnexpectedTypeException($constraint, Person::class); + } + + if (!$this->centerRequired) { + return; + } + + if (NULL === $person->getCenter()) { + $this + ->context + ->buildViolation($constraint->message) + ->atPath('center') + ->addViolation() + ; + } + } +}