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:
2024-10-22 23:15:03 +02:00
parent 418794e586
commit c877076429
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
{
$suggestedUsers = $entityWorkflow->getUsersInvolved();
$related = $this->getRelatedEntity($entityWorkflow);
$referrer = $this->getRelatedEntity($entityWorkflow)->getCourse()->getUser();
$suggestedUsers[spl_object_hash($referrer)] = $referrer;
if (null === $related) {
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