Do not dispatch PersonMoveEvent if moving to a position not sharing

household
This commit is contained in:
Julien Fastré 2022-03-30 11:57:43 +02:00
parent df24d085ca
commit e433b6a42b
2 changed files with 28 additions and 5 deletions

View File

@ -61,8 +61,6 @@ class MembersEditor
throw new LogicException('You must define a household first');
}
$event = new PersonAddressMoveEvent($person);
$membership = (new HouseholdMember())
->setStartDate($date)
->setPerson($person)
@ -70,9 +68,13 @@ class MembersEditor
->setHolder($holder)
->setComment($comment);
$this->household->addMember($membership);
$event->setNextMembership($membership);
if ($position->getShareHousehold()) {
// launch event only if moving to a "share household" position
$event = new PersonAddressMoveEvent($person);
$event->setNextMembership($membership);
$this->events[] = $event;
foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) {
if ($participation === $membership) {
continue;
@ -108,7 +110,6 @@ class MembersEditor
$this->membershipsAffected[] = $membership;
$this->persistables[] = $membership;
$this->events[] = $event;
return $this;
}

View File

@ -167,7 +167,7 @@ final class MembersEditorTest extends TestCase
$this->assertEquals($date, $membership1->getEndDate());
}
public function testPostMove()
public function testPostMoveToAPositionNotSharingHousehold()
{
$person = new Person();
$position = (new Position())
@ -175,6 +175,28 @@ final class MembersEditorTest extends TestCase
$household1 = new Household();
$household2 = new Household();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher
->dispatch(Argument::type(PersonAddressMoveEvent::class))
->shouldNotBeCalled();
$factory = $this->buildMembersEditorFactory(
$eventDispatcher->reveal(),
null
);
$editor = $factory->createEditor($household1);
$editor->addMovement(new DateTimeImmutable('now'), $person, $position);
$editor->postMove();
}
public function testPostMoveToAPositionSharingHousehold()
{
$person = new Person();
$position = (new Position())
->setShareHousehold(true);
$household1 = new Household();
$household2 = new Household();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher
->dispatch(Argument::type(PersonAddressMoveEvent::class))
->shouldBeCalled();