From 095db409c0ee1d4cde5870eab42faee0a19ebba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A8ne=20Meneuvrier?= Date: Mon, 27 Jan 2025 18:25:14 +0100 Subject: [PATCH] cchanges based on cocmments #3582 --- migrations/Version20250122140631.php | 4 ++-- .../Files/AutoAffectationRule.yml | 18 ++++++++--------- src/Entity/AutoAffectationRule.php | 9 +++++---- .../Affectation/CodeInseeSpecification.php | 6 +++--- src/Validator/InseeToIncludeValidator.php | 10 +++++++++- tests/FixturesHelper.php | 2 +- .../AutoAffectationRuleControllerTest.php | 4 ++-- .../CodeInseeSpecificationTest.php | 20 +++++++++---------- tests/Unit/Entity/AutoAffectationRuleTest.php | 2 +- .../Validator/InseeToIncludeValidatorTest.php | 6 +++++- 10 files changed, 47 insertions(+), 34 deletions(-) diff --git a/migrations/Version20250122140631.php b/migrations/Version20250122140631.php index e0ad5ee6b..f4f0e54d7 100644 --- a/migrations/Version20250122140631.php +++ b/migrations/Version20250122140631.php @@ -16,8 +16,8 @@ public function getDescription(): string public function up(Schema $schema): void { - $this->addSql('ALTER TABLE auto_affectation_rule CHANGE insee_to_include insee_to_include VARCHAR(255) DEFAULT NULL COMMENT \'Value possible null or an array of code insee\''); - $this->addSql("UPDATE auto_affectation_rule SET insee_to_include = null WHERE insee_to_include IN ('all','partner_list')"); + $this->addSql('ALTER TABLE auto_affectation_rule CHANGE insee_to_include insee_to_include VARCHAR(255) NOT NULL COMMENT \'Value possible empty or an array of code insee\''); + $this->addSql("UPDATE auto_affectation_rule SET insee_to_include = '' WHERE insee_to_include IN ('all','partner_list')"); } public function down(Schema $schema): void diff --git a/src/DataFixtures/Files/AutoAffectationRule.yml b/src/DataFixtures/Files/AutoAffectationRule.yml index 4cea70771..1134d2e11 100644 --- a/src/DataFixtures/Files/AutoAffectationRule.yml +++ b/src/DataFixtures/Files/AutoAffectationRule.yml @@ -4,7 +4,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "all" partner_type: "COMMUNE_SCHS" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "all" allocataire: "all" @@ -13,7 +13,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "all" partner_type: "COMMUNE_SCHS" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "prive" allocataire: "all" @@ -22,7 +22,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "all" partner_type: "CAF_MSA" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "prive" allocataire: "caf" @@ -40,7 +40,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "LOCATAIRE" partner_type: "EPCI" - insee_to_include: null + insee_to_include: '' insee_to_exclude: - "34048" parc: "prive" @@ -50,7 +50,7 @@ auto_affectation_rules: status: "ARCHIVED" profile_declarant: "all" partner_type: "EPCI" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "all" allocataire: "all" @@ -59,7 +59,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "all" partner_type: "POLICE_GENDARMERIE" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "all" allocataire: "all" @@ -70,7 +70,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "all" partner_type: "COMMUNE_SCHS" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "all" allocataire: "all" @@ -79,7 +79,7 @@ auto_affectation_rules: status: "ARCHIVED" profile_declarant: "all" partner_type: "EPCI" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "all" allocataire: "all" @@ -88,7 +88,7 @@ auto_affectation_rules: status: "ACTIVE" profile_declarant: "all" partner_type: "BAILLEUR_SOCIAL" - insee_to_include: null + insee_to_include: '' insee_to_exclude: null parc: "public" allocataire: "all" diff --git a/src/Entity/AutoAffectationRule.php b/src/Entity/AutoAffectationRule.php index 13831d37d..7fb0a5565 100644 --- a/src/Entity/AutoAffectationRule.php +++ b/src/Entity/AutoAffectationRule.php @@ -53,10 +53,10 @@ enumType: PartnerType::class, #[AppAssert\ValidProfileDeclarant()] private string $profileDeclarant; - #[ORM\Column(nullable: true, length: 255, options: ['comment' => 'Value possible null or an array of code insee'])] + #[ORM\Column(length: 255, options: ['comment' => 'Value possible empty or an array of code insee'])] #[Assert\Length(max: 255)] #[AppAssert\InseeToInclude()] - private ?string $inseeToInclude; + private string $inseeToInclude; #[ORM\Column(nullable: true, options: ['comment' => 'Value possible null or an array of code insee'])] #[AppAssert\InseeToExclude()] @@ -143,12 +143,12 @@ public function setProfileDeclarant(string $profileDeclarant): self return $this; } - public function getInseeToInclude(): ?string + public function getInseeToInclude(): string { return $this->inseeToInclude; } - public function setInseeToInclude(?string $inseeToInclude): self + public function setInseeToInclude(string $inseeToInclude): self { $this->inseeToInclude = $inseeToInclude; @@ -292,6 +292,7 @@ public function getDescription(bool $isShort = true): string $description .= ' Elle s\'applique '; switch ($this->getInseeToInclude()) { case null: + case '': $description .= 'aux logements situés dans le périmètre géographique du partenaire (codes insee et/ou zones)'; break; default: diff --git a/src/Specification/Affectation/CodeInseeSpecification.php b/src/Specification/Affectation/CodeInseeSpecification.php index a7c6f2527..481f6b960 100644 --- a/src/Specification/Affectation/CodeInseeSpecification.php +++ b/src/Specification/Affectation/CodeInseeSpecification.php @@ -9,9 +9,9 @@ class CodeInseeSpecification implements SpecificationInterface { - public function __construct(private string|array|null $inseeToInclude, private ?array $inseeToExclude) + public function __construct(private string|array $inseeToInclude, private ?array $inseeToExclude) { - if (null !== $inseeToInclude) { + if ('' !== $inseeToInclude) { $this->inseeToInclude = explode(',', $inseeToInclude); } else { $this->inseeToInclude = $inseeToInclude; @@ -40,7 +40,7 @@ public function isSatisfiedBy(SpecificationContextInterface $context): bool } return match ($this->inseeToInclude) { - null => true, + '' => true, default => $this->isInseeIncluded($signalement->getInseeOccupant()), }; } diff --git a/src/Validator/InseeToIncludeValidator.php b/src/Validator/InseeToIncludeValidator.php index c279c75a6..252edd717 100644 --- a/src/Validator/InseeToIncludeValidator.php +++ b/src/Validator/InseeToIncludeValidator.php @@ -14,7 +14,15 @@ public function validate($value, Constraint $constraint): void throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\InseeToInclude'); } /* @var InseeToInclude $constraint */ - if (null === $value || '' === $value) { + if ('' === $value) { + return; + } + + if (null === $value) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', 'null') + ->addViolation(); + return; } diff --git a/tests/FixturesHelper.php b/tests/FixturesHelper.php index 7fcde7205..b3084a7b0 100644 --- a/tests/FixturesHelper.php +++ b/tests/FixturesHelper.php @@ -512,7 +512,7 @@ public function getAutoAffectationRule( ->setProfileDeclarant('all') ->setParc('prive') ->setAllocataire('oui') - ->setInseeToInclude(null) + ->setInseeToInclude('') ->setInseeToExclude(null) ->setPartnerToExclude([]) ->setStatus(AutoAffectationRule::STATUS_ACTIVE); diff --git a/tests/Functional/Controller/AutoAffectationRuleControllerTest.php b/tests/Functional/Controller/AutoAffectationRuleControllerTest.php index 346329d1c..e2f78bf93 100644 --- a/tests/Functional/Controller/AutoAffectationRuleControllerTest.php +++ b/tests/Functional/Controller/AutoAffectationRuleControllerTest.php @@ -52,7 +52,7 @@ public function testAutoAffectationRuleFormSubmit(): void 'auto_affectation_rule[profileDeclarant]' => 'occupant', 'auto_affectation_rule[parc]' => 'all', 'auto_affectation_rule[allocataire]' => 'oui', - 'auto_affectation_rule[inseeToInclude]' => null, + 'auto_affectation_rule[inseeToInclude]' => '', 'auto_affectation_rule[inseeToExclude]' => '', 'auto_affectation_rule[partnerToExclude]' => '', ] @@ -77,7 +77,7 @@ public function testAutoAffectationRuleEditFormSubmit(): void 'auto_affectation_rule[profileDeclarant]' => 'occupant', 'auto_affectation_rule[parc]' => 'all', 'auto_affectation_rule[allocataire]' => 'oui', - 'auto_affectation_rule[inseeToInclude]' => null, + 'auto_affectation_rule[inseeToInclude]' => '', 'auto_affectation_rule[inseeToExclude]' => '', 'auto_affectation_rule[partnerToExclude]' => '', ] diff --git a/tests/Functional/Specification/Affectation/CodeInseeSpecificationTest.php b/tests/Functional/Specification/Affectation/CodeInseeSpecificationTest.php index 59499920b..08da36f1b 100644 --- a/tests/Functional/Specification/Affectation/CodeInseeSpecificationTest.php +++ b/tests/Functional/Specification/Affectation/CodeInseeSpecificationTest.php @@ -42,15 +42,15 @@ public function testIsSatisfiedBy( public function provideRulesAndSignalement(): \Generator { - yield 'null - same insee as partner - no exclude' => [self::INSEE_STMARS, [self::INSEE_STMARS], null, null, true]; - yield 'null - same insee as partner - but excluded' => [self::INSEE_STMARS, [self::INSEE_STMARS], null, [self::INSEE_STMARS], false]; - yield 'null - same insee as partner - another excluded' => [self::INSEE_STMARS, [self::INSEE_STMARS], null, [self::INSEE_CELLIER], true]; - yield 'null - different insee than partner - no exclude' => [self::INSEE_STMARS, [self::INSEE_CELLIER], null, null, true]; - yield 'null - different insee than partner - but excluded' => [self::INSEE_STMARS, [self::INSEE_CELLIER], null, [self::INSEE_STMARS], false]; - yield 'null - different insee than partner - another excluded' => [self::INSEE_STMARS, [self::INSEE_CELLIER], null, [self::INSEE_CELLIER], true]; - yield 'null - partner without insee - no exclude' => [self::INSEE_STMARS, [], null, null, true]; - yield 'null - partner without insee - but excluded' => [self::INSEE_STMARS, [], null, [self::INSEE_STMARS], false]; - yield 'null - partner without insee - another excluded' => [self::INSEE_STMARS, [], null, [self::INSEE_CELLIER], true]; + yield 'empty - same insee as partner - no exclude' => [self::INSEE_STMARS, [self::INSEE_STMARS], '', null, true]; + yield 'empty - same insee as partner - but excluded' => [self::INSEE_STMARS, [self::INSEE_STMARS], '', [self::INSEE_STMARS], false]; + yield 'empty - same insee as partner - another excluded' => [self::INSEE_STMARS, [self::INSEE_STMARS], '', [self::INSEE_CELLIER], true]; + yield 'empty - different insee than partner - no exclude' => [self::INSEE_STMARS, [self::INSEE_CELLIER], '', null, true]; + yield 'empty - different insee than partner - but excluded' => [self::INSEE_STMARS, [self::INSEE_CELLIER], '', [self::INSEE_STMARS], false]; + yield 'empty - different insee than partner - another excluded' => [self::INSEE_STMARS, [self::INSEE_CELLIER], '', [self::INSEE_CELLIER], true]; + yield 'empty - partner without insee - no exclude' => [self::INSEE_STMARS, [], '', null, true]; + yield 'empty - partner without insee - but excluded' => [self::INSEE_STMARS, [], '', [self::INSEE_STMARS], false]; + yield 'empty - partner without insee - another excluded' => [self::INSEE_STMARS, [], '', [self::INSEE_CELLIER], true]; yield 'array of insee with this one - same insee as partner - no exclusion' => [self::INSEE_STMARS, [self::INSEE_STMARS], self::INSEE_STMARS, null, true]; yield 'array of insee with this one - same insee as partner - but excluded' => [self::INSEE_STMARS, [self::INSEE_STMARS], self::INSEE_STMARS, [self::INSEE_STMARS], false]; @@ -73,7 +73,7 @@ public function provideRulesAndSignalement(): \Generator yield 'array of insee without this one - partner without insee - another excluded' => [self::INSEE_STMARS, [], self::INSEE_CELLIER, [self::INSEE_CELLIER], false]; // tous les cas ne sont pas testés en cas d'absence d'insee sur le signalement, car ça renvoie toujours false - yield 'null - no insee signalement' => [null, [self::INSEE_CELLIER], null, [self::INSEE_CELLIER], false]; + yield 'empty - no insee signalement' => [null, [self::INSEE_CELLIER], '', [self::INSEE_CELLIER], false]; yield 'array of insee - no insee signalement - another excluded' => [null, [], self::INSEE_STMARS, [self::INSEE_CELLIER], false]; } } diff --git a/tests/Unit/Entity/AutoAffectationRuleTest.php b/tests/Unit/Entity/AutoAffectationRuleTest.php index 229cf4d39..1e4de2ace 100644 --- a/tests/Unit/Entity/AutoAffectationRuleTest.php +++ b/tests/Unit/Entity/AutoAffectationRuleTest.php @@ -27,7 +27,7 @@ public function testDescriptionLong(): void $this->assertEquals('prive', $autoAffectationRule->getParc()); $this->assertEquals('all', $autoAffectationRule->getProfileDeclarant()); $this->assertEquals('oui', $autoAffectationRule->getAllocataire()); - $this->assertNull($autoAffectationRule->getInseeToInclude()); + $this->assertEmpty($autoAffectationRule->getInseeToInclude()); $this->assertNull($autoAffectationRule->getInseeToExclude()); $this->assertEmpty($autoAffectationRule->getPartnerToExclude()); $this->assertEquals(AutoAffectationRule::STATUS_ACTIVE, $autoAffectationRule->getStatus()); diff --git a/tests/Unit/Validator/InseeToIncludeValidatorTest.php b/tests/Unit/Validator/InseeToIncludeValidatorTest.php index c328d298c..c3d2ba992 100644 --- a/tests/Unit/Validator/InseeToIncludeValidatorTest.php +++ b/tests/Unit/Validator/InseeToIncludeValidatorTest.php @@ -26,6 +26,9 @@ public function testValues(?string $insee, bool $isValid, ?string $message = nul if ($isValid) { $this->assertNoViolation(); } else { + if (null === $insee) { + $insee = 'null'; + } $this->buildViolation($message) ->setParameter('{{ value }}', $insee) ->assertRaised(); @@ -34,7 +37,8 @@ public function testValues(?string $insee, bool $isValid, ?string $message = nul public function provideValues(): \Generator { - yield 'null' => [null, true]; + yield 'null' => [null, false, self::ERROR]; + yield 'empty' => ['', true]; yield 'all' => ['all', false, self::ERROR]; yield 'partner_list' => ['partner_list', false, self::ERROR]; yield '44058' => ['44058', true];