Rename membership edition and add more fixtures

This commit is contained in:
Julien Fastré 2021-05-28 18:26:22 +02:00
parent edc86af659
commit ace3b1969e
6 changed files with 70 additions and 57 deletions

View File

@ -4,7 +4,7 @@ namespace Chill\PersonBundle\DataFixtures\ORM;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Household\MoveMembersFactory;
use Chill\PersonBundle\Household\MembersEditorFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
@ -12,33 +12,53 @@ use Doctrine\Common\DataFixtures\DependentFixtureInterface;
class LoadHousehold extends Fixture implements DependentFixtureInterface
{
private MoveMembersFactory $movementFactory;
private MembersEditorFactory $editorFactory;
private EntityManagerInterface $em;
private CONST NUMBER_OF_HOUSEHOLD = 10;
public function __construct(MoveMembersFactory $movementFactory, EntityManagerInterface $em)
public function __construct(MembersEditorFactory $editorFactory, EntityManagerInterface $em)
{
$this->movementFactory = $movementFactory;
$this->editorFactory = $editorFactory;
$this->em = $em;
}
public function load(ObjectManager $manager)
{
// generate two times the participation. This will lead to
// some movement in participation (same people in two differents
// households)
$this->preparePersonIds();
$this->generateHousehold(
$manager,
\DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
);
$this->preparePersonIds();
$this->generateHousehold(
$manager,
\DateTimeImmutable::createFromFormat('Y-m-d', '2015-01-01')
);
$manager->flush();
}
private function generateHousehold(ObjectManager $manager, \DateTimeImmutable $startDate)
{
for ($i=0; $i < self::NUMBER_OF_HOUSEHOLD; $i++) {
$household = new Household();
$manager->persist($household);
$movement = $this->movementFactory->createMovement($household);
$movement = $this->editorFactory->createEditor($household);
// load adults
$k = 0;
foreach ($this->getRandomPersons(1, 3) as $person) {
$date = \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::ADULT);
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
@ -47,21 +67,26 @@ class LoadHousehold extends Fixture implements DependentFixtureInterface
// load children
foreach ($this->getRandomPersons(0, 3) as $person) {
$date = \DateTimeImmutable::createFromFormat('Y-m-d', '2010-01-01')
->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::CHILD);
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
$k++;
}
// load children out
foreach ($this->getRandomPersons(0, 2) as $person) {
$date = $startDate->add(new \DateInterval('P'.\random_int(1, 200).'W'));
$position = $this->getReference(LoadHouseholdPosition::CHILD_OUT);
$movement->addMovement($date, $person, $position, $k === 0, "self generated");
$k++;
}
foreach ($movement->getPersistable() as $obj) {
print($obj->getStartDate()->format('Y-m-d'));
$manager->persist($obj);
}
}
$manager->flush();
}
private function preparePersonIds()

View File

@ -10,7 +10,7 @@ use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class MoveMembers
class MembersEditor
{
private ValidatorInterface $validator;
private Household $household;
@ -18,18 +18,12 @@ class MoveMembers
private array $persistables = [];
private array $memershipsAffected = [];
public function __construct(ValidatorInterface $validator)
public function __construct(ValidatorInterface $validator, Household $household)
{
$this->validation = $validator;
$this->household = $household;
}
public function toHousehold(Household $household): self
{
$this->household = $household;
return $this;
}
public function addMovement(\DateTimeInterface $date, Person $person, Position $position, ?bool $holder = false, ?string $comment = null): self
{
if (NULL === $this->household) {

View File

@ -0,0 +1,21 @@
<?php
namespace Chill\PersonBundle\Household;
use Chill\PersonBundle\Household\MembersEditor;
use Chill\PersonBundle\Entity\Household\Household;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class MembersEditorFactory
{
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function createEditor(Household $household): MembersEditor
{
return new MembersEditor($this->validator, $household);
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace Chill\PersonBundle\Household;
use Chill\PersonBundle\Household\MoveMembers;
use Chill\PersonBundle\Entity\Household\Household;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class MoveMembersFactory
{
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function createMovement(?Household $household): MoveMembers
{
$movement = new MoveMembers($this->validator);
if ($household) {
$movement->toHousehold($household);
}
return $movement;
}
}

View File

@ -5,21 +5,21 @@ namespace Chill\PersonBundle\Tests\Household;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Household\MoveMembersFactory;
use Chill\PersonBundle\Household\MembersEditorFactory;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use PHPUnit\Framework\TestCase;
class MoveMembersTest extends TestCase
class MembersEditorTest extends TestCase
{
private MoveMembersFactory $factory;
private MembersEditorFactory $factory;
protected function setUp()
{
$validator = $this->createMock(ValidatorInterface::class);
$this->factory = new MoveMembersFactory($validator);
$this->factory = new MembersEditorFactory($validator);
}
public function testMovePersonWithSharedHousehold()
@ -30,7 +30,7 @@ class MoveMembersTest extends TestCase
;
$household1 = new Household();
$household2 = new Household();
$editor = $this->factory->createMovement($household1);
$editor = $this->factory->createEditor($household1);
$editor->addMovement(
\DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'),
@ -46,7 +46,7 @@ class MoveMembersTest extends TestCase
// move to another household
$date = \DateTimeImmutable::createFromFormat('Y-m-d', '2021-01-01');
$editor = $this->factory->createMovement($household2);
$editor = $this->factory->createEditor($household2);
$editor->addMovement(
$date,
$person,
@ -70,7 +70,7 @@ class MoveMembersTest extends TestCase
;
$household1 = new Household();
$household2 = new Household();
$editor = $this->factory->createMovement($household1);
$editor = $this->factory->createEditor($household1);
$editor->addMovement(
\DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'),
@ -86,7 +86,7 @@ class MoveMembersTest extends TestCase
// move to another household
$date = \DateTimeImmutable::createFromFormat('Y-m-d', '2021-01-01');
$editor = $this->factory->createMovement($household2);
$editor = $this->factory->createEditor($household2);
$editor->addMovement(
$date,
$person,

View File

@ -1,3 +1,3 @@
services:
Chill\PersonBundle\Household\MoveMembersFactory:
Chill\PersonBundle\Household\MembersEditorFactory:
autowire: true