test on members editor post move + listen for event (wip)

This commit is contained in:
Julien Fastré 2022-02-15 00:23:01 +01:00
parent b9dbb1916a
commit 1658fee090
7 changed files with 111 additions and 3 deletions

View File

@ -0,0 +1,53 @@
<?php
namespace Chill\PersonBundle\AccompanyingPeriod\Events;
use Chill\MainBundle\Entity\Notification;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class PersonMoveEventSubscriber implements EventSubscriberInterface
{
private TranslatorInterface $translator;
private Security $security;
public static function getSubscribedEvents()
{
return [
PersonAddressMoveEvent::class => 'resetPeriodLocation'
];
}
public function resetPeriodLocation(PersonAddressMoveEvent $event)
{
if ($event->getPreviousAddress() !== $event->getNextAddress()
&& null !== $event->getPreviousAddress()
) {
$person = $event->getPerson();
foreach ($person->getCurrentAccompanyingPeriods() as $period) {
if ($period->getPersonLocation() === $person) {
$period->setPersonLocation(null);
$period->setAddressLocation($event->getPreviousAddress());
if (null !== $period->getUser() && $period->getUser() !== $this->security->getUser()) {
$notification = new Notification();
$notification
->addAddressee($period->getUser())
->setTitle($this->translator->trans())
->setRelatedEntityClass(AccompanyingPeriod::class)
->setRelatedEntityId($period->getId())
;
}
}
}
}
}
}

View File

@ -180,6 +180,7 @@ class HouseholdMemberController extends ApiController
public function move(Request $request, $_format): Response
{
try {
/** @var MembersEditor $editor */
$editor = $this->getSerializer()
->deserialize(
$request->getContent(),
@ -199,6 +200,9 @@ class HouseholdMemberController extends ApiController
return $this->json($errors, 422);
}
// launch events on post move
$editor->postMove();
$em = $this->getDoctrine()->getManager();
// if new household, persist it

View File

@ -930,6 +930,8 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/**
* Get current accompanyingPeriods array.
*
* @return AccompanyingPeriod[]|array
*/
public function getCurrentAccompanyingPeriods(): array
{

View File

@ -22,6 +22,7 @@ use LogicException;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use function in_array;
use function spl_object_hash;

View File

@ -12,8 +12,8 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Household;
use Chill\PersonBundle\Entity\Household\Household;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class MembersEditorFactory
{

View File

@ -14,10 +14,14 @@ namespace Chill\PersonBundle\Tests\Household;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent;
use Chill\PersonBundle\Household\MembersEditorFactory;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use function count;
/**
@ -26,13 +30,56 @@ use function count;
*/
final class MembersEditorTest extends TestCase
{
use ProphecyTrait;
private MembersEditorFactory $factory;
protected function setUp(): void
{
$validator = $this->createMock(ValidatorInterface::class);
$this->factory = $this->buildMembersEditorFactory();
}
$this->factory = new MembersEditorFactory($validator);
private function buildMembersEditorFactory(
?EventDispatcherInterface $eventDispatcher = null,
?ValidatorInterface $validator = null
) {
if ($eventDispatcher === null) {
$double = $this->getProphet()->prophesize();
$double->willImplement(EventDispatcherInterface::class);
$double->dispatch(Argument::type(PersonAddressMoveEvent::class));
$eventDispatcher = $double->reveal();
}
if ($validator === null) {
$double = $this->getProphet()->prophesize();
$double->willImplement(ValidatorInterface::class);
$validator = $double->reveal();
}
return new MembersEditorFactory(
$eventDispatcher, $validator
);
}
public function testPostMove()
{
$person = new Person();
$position = (new Position())
->setShareHousehold(false);
$household1 = new Household();
$household2 = new Household();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher
->dispatch(Argument::type(PersonAddressMoveEvent::class))
->shouldBeCalled();
$factory = $this->buildMembersEditorFactory(
$eventDispatcher->reveal(), null
);
$editor = $factory->createEditor($household1);
$editor->addMovement(new DateTimeImmutable('now'), $person, $position);
$editor->postMove();
}
public function testMovePersonWithoutSharedHousehold()

View File

@ -547,6 +547,7 @@ period_notification:
Persons are: Les usagers concernés sont les suivants
Social issues are: Les problématiques sociales renseignées sont les suivantes
See it online: Visualisez le parcours en ligne
Person locating period has moved: L'usager qui localise un parcours a déménagé
You are getting a notification for a period which does not exists any more: Cette notification ne correspond pas à une période d'accompagnement valide.
You are getting a notification for a period you are not allowed to see: La notification fait référence à une période d'accompagnement à laquelle vous n'avez pas accès.