userOrganization service test

This commit is contained in:
Charles 2025-12-09 15:25:46 +01:00
parent 55c42c81fa
commit 0df623ba17
1 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,186 @@
<?php
namespace App\Tests\Service;
use App\Entity\Organizations;
use App\Entity\User;
use App\Entity\UsersOrganizations;
use App\Service\ActionService;
use App\Service\LoggerService;
use App\Service\UserOrganizationAppService;
use App\Service\UserOrganizationService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class UserOrganizationServiceTest extends TestCase
{
private UserOrganizationService $service;
private MockObject|UserOrganizationAppService $userOrganizationAppService;
private MockObject|EntityManagerInterface $entityManager;
private MockObject|ActionService $actionService;
private MockObject|LoggerService $loggerService;
protected function setUp(): void
{
$this->userOrganizationAppService = $this->createMock(UserOrganizationAppService::class);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->actionService = $this->createMock(ActionService::class);
$this->loggerService = $this->createMock(LoggerService::class);
$this->service = new UserOrganizationService(
$this->userOrganizationAppService,
$this->entityManager,
$this->actionService,
$this->loggerService
);
}
/**
* Helper to set private ID property on entities via Reflection.
* Essential because your service calls getId() on entities.
*/
private function setEntityId(object $entity, int $id): void
{
$reflection = new \ReflectionClass($entity);
if ($reflection->hasProperty('id')) {
$property = $reflection->getProperty('id');
$property->setValue($entity, $id);
}
}
public function testDeactivateAllLinksByUser(): void
{
// 1. Setup Data
$actingUser = new User();
$this->setEntityId($actingUser, 1);
$targetUser = new User();
$this->setEntityId($targetUser, 2);
$org = new Organizations();
$this->setEntityId($org, 100);
$org->setName('Test Org');
// Create a dummy UsersOrganizations link
$uo = new UsersOrganizations();
$uo->setUsers($targetUser);
$uo->setOrganization($org);
$uo->setIsActive(true);
// Assuming there is an ID on UO, though not strictly used in the logic provided
$this->setEntityId($uo, 555);
// 2. Mock Repository
$repo = $this->createMock(EntityRepository::class);
$repo->expects($this->once())
->method('findBy')
->with(['users' => $targetUser, 'isActive' => true])
->willReturn([$uo]);
$this->entityManager->expects($this->once())
->method('getRepository')
->with(UsersOrganizations::class)
->willReturn($repo);
// 3. Expect Side Effects on Dependencies
// Expect deactivation of app links
$this->userOrganizationAppService->expects($this->once())
->method('deactivateAllUserOrganizationsAppLinks')
->with($uo);
// Expect Logging
$this->loggerService->expects($this->once())
->method('logOrganizationInformation')
->with(100, 1, 'Uo link deactivated'); // OrgID, ActingUserID
// Expect Persist
$this->entityManager->expects($this->once())
->method('persist')
->with($uo);
// Expect Action Creation
$this->actionService->expects($this->once())
->method('createAction')
->with("Deactivate UO link", $actingUser, $org, 'Test Org');
// 4. Run Method
$this->service->deactivateAllUserOrganizationLinks($actingUser, $targetUser, null);
// 5. Assert State Change
$this->assertFalse($uo->isActive(), 'The user-organization link should have been set to inactive.');
}
public function testDeactivateAllLinksByOrganization(): void
{
// 1. Setup Data
$actingUser = new User();
$this->setEntityId($actingUser, 1);
$org = new Organizations();
$this->setEntityId($org, 200);
$org->setName('Org B');
$uo1 = new UsersOrganizations();
$uo1->setOrganization($org);
$uo1->setIsActive(true);
$uo2 = new UsersOrganizations();
$uo2->setOrganization($org);
$uo2->setIsActive(true);
// 2. Mock Repository to return 2 items
$repo = $this->createMock(EntityRepository::class);
$repo->expects($this->once())
->method('findBy')
->with(['organization' => $org, 'isActive' => true])
->willReturn([$uo1, $uo2]);
$this->entityManager->expects($this->once())
->method('getRepository')
->with(UsersOrganizations::class)
->willReturn($repo);
// 3. Expect Side Effects (Called twice, once for each UO)
$this->userOrganizationAppService->expects($this->exactly(2))
->method('deactivateAllUserOrganizationsAppLinks');
$this->loggerService->expects($this->exactly(2))
->method('logOrganizationInformation');
$this->entityManager->expects($this->exactly(2))
->method('persist');
$this->actionService->expects($this->exactly(2))
->method('createAction');
// 4. Run Method (User is null, Organization is provided)
$this->service->deactivateAllUserOrganizationLinks($actingUser, null, $org);
// 5. Assert State
$this->assertFalse($uo1->isActive());
$this->assertFalse($uo2->isActive());
}
public function testDeactivateDoesNothingIfNoLinksFound(): void
{
$actingUser = new User();
$targetUser = new User();
// Repo returns empty array
$repo = $this->createMock(EntityRepository::class);
$repo->method('findBy')->willReturn([]);
$this->entityManager->method('getRepository')->willReturn($repo);
// Ensure services are NEVER called
$this->userOrganizationAppService->expects($this->never())->method('deactivateAllUserOrganizationsAppLinks');
$this->loggerService->expects($this->never())->method('logOrganizationInformation');
$this->entityManager->expects($this->never())->method('persist');
$this->actionService->expects($this->never())->method('createAction');
$this->service->deactivateAllUserOrganizationLinks($actingUser, $targetUser);
}
}