diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php index 5ecf1c0bd..fd2fcfbb2 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php @@ -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; } diff --git a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php index f1a622f5f..84ca9f32e 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php @@ -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();