Skip to content

Commit

Permalink
improve buildNbEnfant to prevent operation error #1942
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinx13 committed Nov 16, 2023
1 parent 9d12135 commit 5268763
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Factory/Esabora/DossierMessageSCHSFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ public function createInstance(Affectation $affectation): DossierMessageSCHS

private function buildCommentaire(Signalement $signalement): string
{
$commentaire = 'Points signalés:\n';
$commentaire = 'Points signalés:'.\PHP_EOL;

foreach ($signalement->getCriticites() as $criticite) {
$commentaire .= '\n'.$criticite->getCritere()->getLabel().' => Etat '.$criticite->getScoreLabel();
$commentaire .= \PHP_EOL.$criticite->getCritere()->getLabel().' => Etat '.$criticite->getScoreLabel();
}

$commentaire .= '\nPropriétaire averti: '.$signalement->getIsProprioAverti() ? 'OUI' : 'NON';
$commentaire .= '\nAdultes: '.$signalement->getNbAdultes().' Adultes';
$commentaire .= \PHP_EOL.'Propriétaire averti: '.$signalement->getIsProprioAverti() ? 'OUI' : 'NON';
$commentaire .= \PHP_EOL.'Adultes: '.$signalement->getNbAdultes().' Adulte(s)';
$commentaire .= $this->buildNbEnfants($signalement);

foreach ($signalement->getAffectations() as $affectation) {
$commentaire .= '\n'.$affectation->getPartner()->getNom().' => '.$affectation->getAffectationLabel();
$commentaire .= \PHP_EOL.$affectation->getPartner()->getNom().' => '.$affectation->getAffectationLabel();
}

return $commentaire;
Expand All @@ -76,13 +76,17 @@ private function buildCommentaire(Signalement $signalement): string
private function buildNbEnfants(Signalement $signalement)
{
$suffix = '';
if (str_ends_with($signalement->getNbEnfantsM6(), '+') || str_ends_with($signalement->getNbEnfantsP6(), '+')) {
if (null !== $signalement->getNbEnfantsM6() && str_ends_with($signalement->getNbEnfantsM6(), '+') ||
null !== $signalement->getNbEnfantsP6() && str_ends_with($signalement->getNbEnfantsP6(), '+')
) {
$suffix = '+';
}
$nbEnfants = str_replace('+', '', $signalement->getNbEnfantsM6()) + str_replace('+', '', $signalement->getNbEnfantsP6());
$nbEnfantsM6 = (int) str_replace('+', '', $signalement->getNbEnfantsM6());
$nbEnfantsP6 = (int) str_replace('+', '', $signalement->getNbEnfantsP6());
$nbEnfants = $nbEnfantsM6 + $nbEnfantsP6;
$nbEnfants .= $suffix;

return '\n'.$nbEnfants.' Enfants';
return \PHP_EOL.$nbEnfants.' Enfant(s)';
}

private function buildPiecesJointesObservation(Signalement $signalement): string
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/Factory/Esabora/DossierMessageSCHSFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Tests\Unit\Factory\Esabora;

use App\Entity\Enum\PartnerType;
use App\Entity\Signalement;
use App\Factory\Esabora\DossierMessageSCHSFactory;
use App\Service\UploadHandlerService;
use App\Tests\FixturesHelper;
Expand Down Expand Up @@ -34,4 +35,37 @@ public function testDossierMessageFactoryIsFullyCreated(): void
$this->assertStringContainsString('25', $dossierMessage->getNumeroAdresseSignalement());
$this->assertStringContainsString('Rue du test', $dossierMessage->getAdresseSignalement());
}

/**
* @dataProvider provideNbChildren
*/
public function testBuildNbEnfants(string $expectedResult, ?string $nbEnfantsM6 = null, ?string $nbEnfantsP6 = null): void
{
$uploadHandlerServiceMock = $this->createMock(UploadHandlerService::class);
$uploadHandlerServiceMock
->expects($this->exactly(0))
->method('getTmpFilepath')
->willReturn(self::FILE);

$dossierMessageFactory = new DossierMessageSCHSFactory($uploadHandlerServiceMock);
$signalement = (new Signalement())->setNbEnfantsM6($nbEnfantsM6)->setNbEnfantsP6($nbEnfantsP6);

$buildNbEnfantsMethod = new \ReflectionMethod(DossierMessageSCHSFactory::class, 'buildNbEnfants');
$buildNbEnfantsMethod->setAccessible(true);

$actualResult = $buildNbEnfantsMethod->invoke($dossierMessageFactory, $signalement);

$this->assertStringContainsString($expectedResult, $actualResult);
}

public function provideNbChildren(): \Generator
{
yield 'No child' => ['0 Enfant(s)', null, null];
yield '1 children M6, 0 children P6' => ['1 Enfant(s)', '1', null];
yield '3 children M6, 2 children P6' => ['5 Enfant(s)', '3', '2'];
yield '4+ children M6, 4+ children P6' => ['8+ Enfant(s)', '4+', '4+'];
yield '4+ children M6, 1 children P6' => ['5+ Enfant(s)', '4+', '1'];
yield '4+ children M6, 0 children P6' => ['4+ Enfant(s)', '4+', null];
yield '0 children M6, 4+ children P6' => ['4+ Enfant(s)', null, '4+'];
}
}

0 comments on commit 5268763

Please sign in to comment.