From 0df623ba17fbb7a88b12592c47f1f688d26edf45 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 9 Dec 2025 15:25:46 +0100 Subject: [PATCH] userOrganization service test --- tests/Service/UserOrganizationServiceTest.php | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 tests/Service/UserOrganizationServiceTest.php diff --git a/tests/Service/UserOrganizationServiceTest.php b/tests/Service/UserOrganizationServiceTest.php new file mode 100644 index 0000000..6761f63 --- /dev/null +++ b/tests/Service/UserOrganizationServiceTest.php @@ -0,0 +1,186 @@ +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); + } +} \ No newline at end of file