Add and update test handlers for suggested users retrieval

Introduced new test files for workflow handlers and adjusted existing `getSuggestedUsers` methods to handle related entity checks and duplicates removal. Also, modified repos to align with test dependencies.
This commit is contained in:
Julien Fastré 2024-10-22 23:15:03 +02:00
parent 418794e586
commit c877076429
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
10 changed files with 410 additions and 29 deletions

View File

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Tests\Workflow;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
/**
* @internal
*
* @coversNothing
*/
class AccompanyingCourseDocumentWorkflowHandlerTest extends TestCase
{
use ProphecyTrait;
public function testGetSuggestedUsers()
{
$accompanyingPeriod = new AccompanyingPeriod();
$document = new AccompanyingCourseDocument();
$document->setCourse($accompanyingPeriod)->setUser($user1 = new User());
$accompanyingPeriod->setUser($user = new User());
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setRelatedEntityId(1);
$handler = new AccompanyingCourseDocumentWorkflowHandler(
$this->prophesize(TranslatorInterface::class)->reveal(),
$this->prophesize(EntityWorkflowRepository::class)->reveal(),
$this->buildRepository($document, 1),
new WorkflowWithPublicViewDocumentHelper($this->prophesize(Environment::class)->reveal()),
);
$users = $handler->getSuggestedUsers($entityWorkflow);
self::assertCount(2, $users);
self::assertContains($user, $users);
self::assertContains($user1, $users);
}
public function testGetSuggestedUsersWithDuplicates()
{
$accompanyingPeriod = new AccompanyingPeriod();
$document = new AccompanyingCourseDocument();
$document->setCourse($accompanyingPeriod)->setUser($user1 = new User());
$accompanyingPeriod->setUser($user1);
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setRelatedEntityId(1);
$handler = new AccompanyingCourseDocumentWorkflowHandler(
$this->prophesize(TranslatorInterface::class)->reveal(),
$this->prophesize(EntityWorkflowRepository::class)->reveal(),
$this->buildRepository($document, 1),
new WorkflowWithPublicViewDocumentHelper($this->prophesize(Environment::class)->reveal()),
);
$users = $handler->getSuggestedUsers($entityWorkflow);
self::assertCount(1, $users);
self::assertContains($user1, $users);
}
private function buildRepository(AccompanyingCourseDocument $document, int $id): AccompanyingCourseDocumentRepository
{
$repository = $this->prophesize(AccompanyingCourseDocumentRepository::class);
$repository->find($id)->willReturn($document);
return $repository->reveal();
}
}

View File

@ -91,12 +91,28 @@ final readonly class AccompanyingCourseDocumentWorkflowHandler implements Entity
public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array
{ {
$suggestedUsers = $entityWorkflow->getUsersInvolved(); $related = $this->getRelatedEntity($entityWorkflow);
$referrer = $this->getRelatedEntity($entityWorkflow)->getCourse()->getUser(); if (null === $related) {
$suggestedUsers[spl_object_hash($referrer)] = $referrer; return [];
}
return $suggestedUsers; $users = [];
if (null !== $user = $related->getUser()) {
$users[] = $user;
}
if (null !== $user = $related->getCourse()->getUser()) {
$users[] = $user;
}
return array_values(
// filter objects to remove duplicates
array_filter(
$users,
fn ($o, $k) => array_search($o, $users, true) === $k,
ARRAY_FILTER_USE_BOTH
)
);
} }
public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string

View File

@ -19,9 +19,9 @@ use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
readonly class AccompanyingPeriodWorkEvaluationDocumentRepository implements ObjectRepository, AssociatedEntityToStoredObjectInterface class AccompanyingPeriodWorkEvaluationDocumentRepository implements ObjectRepository, AssociatedEntityToStoredObjectInterface
{ {
private EntityRepository $repository; private readonly EntityRepository $repository;
public function __construct(EntityManagerInterface $em) public function __construct(EntityManagerInterface $em)
{ {

View File

@ -22,11 +22,11 @@ use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
final readonly class AccompanyingPeriodWorkRepository implements ObjectRepository class AccompanyingPeriodWorkRepository implements ObjectRepository
{ {
private EntityRepository $repository; private readonly EntityRepository $repository;
public function __construct(private EntityManagerInterface $em) public function __construct(private readonly EntityManagerInterface $em)
{ {
$this->repository = $em->getRepository(AccompanyingPeriodWork::class); $this->repository = $em->getRepository(AccompanyingPeriodWork::class);
} }

View File

@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Tests\Workflow;
use Chill\DocStoreBundle\Workflow\WorkflowWithPublicViewDocumentHelper;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocumentRepository;
use Chill\PersonBundle\Workflow\AccompanyingPeriodWorkEvaluationDocumentWorkflowHandler;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
/**
* @internal
*
* @coversNothing
*/
class AccompanyingPeriodWorkEvaluationDocumentWorkflowHandlerTest extends TestCase
{
use ProphecyTrait;
public function testGetSuggestedUsers()
{
$accompanyingCourse = new AccompanyingPeriod();
$accompanyingCourse->setUser($referrer = new User());
$accompanyingCourse->addWork($work = new AccompanyingPeriod\AccompanyingPeriodWork());
$work->addReferrer($workReferrer1 = new User());
$work->addReferrer($workReferrer2 = new User());
$work->addReferrer($referrer);
$work->addAccompanyingPeriodWorkEvaluation($eval = new AccompanyingPeriod\AccompanyingPeriodWorkEvaluation());
$eval->addDocument($doc = new AccompanyingPeriodWorkEvaluationDocument());
$entityWorkflow = new EntityWorkflow();
// Prophesize each dependency
$workflowRepositoryProphecy = $this->prophesize(EntityWorkflowRepository::class);
$translatableStringHelperProphecy = $this->prophesize(TranslatableStringHelperInterface::class);
$translatorProphecy = $this->prophesize(TranslatorInterface::class);
$twig = $this->prophesize(Environment::class);
// Create an instance of the class under test using revealed prophecies directly
$handler = new AccompanyingPeriodWorkEvaluationDocumentWorkflowHandler(
$this->buildRepository($doc, 1),
$workflowRepositoryProphecy->reveal(),
$translatableStringHelperProphecy->reveal(),
$translatorProphecy->reveal(),
new WorkflowWithPublicViewDocumentHelper($twig->reveal()),
);
$entityWorkflow->setRelatedEntityId(1);
$entityWorkflow->setRelatedEntityClass(AccompanyingPeriodWorkEvaluationDocument::class);
$users = $handler->getSuggestedUsers($entityWorkflow);
self::assertContains($referrer, $users);
self::assertContains($workReferrer1, $users);
self::assertContains($workReferrer2, $users);
}
private function buildRepository(AccompanyingPeriodWorkEvaluationDocument $document, int $id): AccompanyingPeriodWorkEvaluationDocumentRepository
{
$repository = $this->prophesize(AccompanyingPeriodWorkEvaluationDocumentRepository::class);
$repository->find($id)->willReturn($document);
return $repository->reveal();
}
}

View File

@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Tests\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @internal
*
* @coversNothing
*/
class AccompanyingPeriodWorkEvaluationWorkflowHandlerTest extends TestCase
{
use ProphecyTrait;
public function testGetSuggestedUsers()
{
$accompanyingCourse = new AccompanyingPeriod();
$accompanyingCourse->setUser($referrer = new User());
$accompanyingCourse->addWork($work = new AccompanyingPeriod\AccompanyingPeriodWork());
$work->addReferrer($workReferrer1 = new User());
$work->addReferrer($workReferrer2 = new User());
$work->addReferrer($referrer);
$work->addAccompanyingPeriodWorkEvaluation($eval = new AccompanyingPeriod\AccompanyingPeriodWorkEvaluation());
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setRelatedEntityId(1);
// Prophesize each dependency
$workflowRepositoryProphecy = $this->prophesize(EntityWorkflowRepository::class);
$translatableStringHelperProphecy = $this->prophesize(TranslatableStringHelperInterface::class);
$translatorProphecy = $this->prophesize(TranslatorInterface::class);
// Create an instance of the class under test using revealed prophecies directly
$handler = new AccompanyingPeriodWorkEvaluationWorkflowHandler(
$this->buildRepository($eval, 1),
$workflowRepositoryProphecy->reveal(),
$translatableStringHelperProphecy->reveal(),
$translatorProphecy->reveal(),
);
$users = $handler->getSuggestedUsers($entityWorkflow);
self::assertContains($referrer, $users);
self::assertContains($workReferrer1, $users);
self::assertContains($workReferrer2, $users);
}
private function buildRepository(AccompanyingPeriod\AccompanyingPeriodWorkEvaluation $evaluation, int $id): AccompanyingPeriodWorkEvaluationRepository
{
$evaluationRepositoryProphecy = $this->prophesize(AccompanyingPeriodWorkEvaluationRepository::class);
$evaluationRepositoryProphecy->find($id)->willReturn($evaluation);
return $evaluationRepositoryProphecy->reveal();
}
}

View File

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Tests\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @internal
*
* @coversNothing
*/
class AccompanyingPeriodWorkWorkflowHandlerTest extends TestCase
{
use ProphecyTrait;
public function testGetSuggestedUsers()
{
$accompanyingCourse = new AccompanyingPeriod();
$accompanyingCourse->setUser($referrer = new User());
$accompanyingCourse->addWork($work = new AccompanyingPeriod\AccompanyingPeriodWork());
$work->addReferrer($workReferrer1 = new User());
$work->addReferrer($workReferrer2 = new User());
$work->addReferrer($referrer);
$entityWorkflow = new EntityWorkflow();
$entityWorkflow->setRelatedEntityId(1);
// Prophesize each dependency
$workflowRepositoryProphecy = $this->prophesize(EntityWorkflowRepository::class);
$translatableStringHelperProphecy = $this->prophesize(TranslatableStringHelperInterface::class);
$translatorProphecy = $this->prophesize(TranslatorInterface::class);
// Create an instance of the class under test using revealed prophecies directly
$handler = new AccompanyingPeriodWorkWorkflowHandler(
$this->buildRepository($work, 1),
$workflowRepositoryProphecy->reveal(),
$translatableStringHelperProphecy->reveal(),
$translatorProphecy->reveal(),
);
$users = $handler->getSuggestedUsers($entityWorkflow);
self::assertContains($referrer, $users);
self::assertContains($workReferrer1, $users);
self::assertContains($workReferrer2, $users);
}
private function buildRepository(AccompanyingPeriod\AccompanyingPeriodWork $work, int $int): AccompanyingPeriodWorkRepository
{
$accompanyingPeriodWorkRepositoryProphecy = $this->prophesize(AccompanyingPeriodWorkRepository::class);
$accompanyingPeriodWorkRepositoryProphecy
->find($int)
->willReturn($work);
return $accompanyingPeriodWorkRepositoryProphecy->reveal();
}
}

View File

@ -98,17 +98,36 @@ class AccompanyingPeriodWorkEvaluationDocumentWorkflowHandler implements EntityW
public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array
{ {
$suggestedUsers = $entityWorkflow->getUsersInvolved(); $related = $this->getRelatedEntity($entityWorkflow);
$referrer = $this->getRelatedEntity($entityWorkflow) if (null === $related) {
->getAccompanyingPeriodWorkEvaluation() return [];
}
$users = [];
if (null !== $referrer = $related->getAccompanyingPeriodWorkEvaluation()
->getAccompanyingPeriodWork() ->getAccompanyingPeriodWork()
->getAccompanyingPeriod() ->getAccompanyingPeriod()
->getUser(); ->getUser()
) {
$users[] = $referrer;
}
$suggestedUsers[spl_object_hash($referrer)] = $referrer; foreach ($related->getAccompanyingPeriodWorkEvaluation()
->getAccompanyingPeriodWork()
->getReferrersHistoryCurrent() as $referrerHistory
) {
$users[] = $referrerHistory->getUser();
}
return $suggestedUsers; return array_values(
// filter objects to remove duplicates
array_filter(
$users,
fn ($o, $k) => array_search($o, $users, true) === $k,
ARRAY_FILTER_USE_BOTH
)
);
} }
public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string

View File

@ -81,16 +81,34 @@ readonly class AccompanyingPeriodWorkEvaluationWorkflowHandler implements Entity
public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array
{ {
$suggestedUsers = $entityWorkflow->getUsersInvolved(); $related = $this->getRelatedEntity($entityWorkflow);
$referrer = $this->getRelatedEntity($entityWorkflow) if (null === $related) {
->getAccompanyingPeriodWork() return [];
}
$users = [];
if (null !== $referrer = $related->getAccompanyingPeriodWork()
->getAccompanyingPeriod() ->getAccompanyingPeriod()
->getUser(); ->getUser()
) {
$users[] = $referrer;
}
$suggestedUsers[spl_object_hash($referrer)] = $referrer; foreach ($related->getAccompanyingPeriodWork()
->getReferrersHistoryCurrent() as $referrerHistory
) {
$users[] = $referrerHistory->getUser();
}
return $suggestedUsers; return array_values(
// filter objects to remove duplicates
array_filter(
$users,
fn ($o, $k) => array_search($o, $users, true) === $k,
ARRAY_FILTER_USE_BOTH
)
);
} }
public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string

View File

@ -87,17 +87,32 @@ readonly class AccompanyingPeriodWorkWorkflowHandler implements EntityWorkflowHa
public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array public function getSuggestedUsers(EntityWorkflow $entityWorkflow): array
{ {
$suggestedUsers = $entityWorkflow->getUsersInvolved(); $related = $this->getRelatedEntity($entityWorkflow);
$referrer = $this->getRelatedEntity($entityWorkflow) if (null === $related) {
->getAccompanyingPeriod() return [];
->getUser();
if (null !== $referrer) {
$suggestedUsers[spl_object_hash($referrer)] = $referrer;
} }
return $suggestedUsers; $users = [];
if (null !== $referrer = $related->getAccompanyingPeriod()
->getUser()
) {
$users[] = $referrer;
}
foreach ($related->getReferrersHistoryCurrent() as $referrerHistory
) {
$users[] = $referrerHistory->getUser();
}
return array_values(
// filter objects to remove duplicates
array_filter(
$users,
fn ($o, $k) => array_search($o, $users, true) === $k,
ARRAY_FILTER_USE_BOTH
)
);
} }
public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string public function getTemplate(EntityWorkflow $entityWorkflow, array $options = []): string