From 9df423a20192b615439268165a7cfc55029e5bba Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 25 Aug 2025 19:38:42 +0200 Subject: [PATCH] Add `TaskAssignEventSubscriber` unit tests - Covered `getSubscribedEvents` method behavior - Added tests for `onTaskAssigned` with changed and unchanged assignees - Validated notification properties in appropriate scenarios --- .../TaskAssignEventSubscriberTest.php | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/Bundle/ChillTaskBundle/Tests/EventSubscriber/TaskAssignEventSubscriberTest.php diff --git a/src/Bundle/ChillTaskBundle/Tests/EventSubscriber/TaskAssignEventSubscriberTest.php b/src/Bundle/ChillTaskBundle/Tests/EventSubscriber/TaskAssignEventSubscriberTest.php new file mode 100644 index 000000000..3cd3ff20f --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Tests/EventSubscriber/TaskAssignEventSubscriberTest.php @@ -0,0 +1,119 @@ +entityManager = $this->createMock(EntityManagerInterface::class); + $this->twig = $this->createMock(Environment::class); + $this->subscriber = new TaskAssignEventSubscriber($this->entityManager, $this->twig); + } + + public function testGetSubscribedEvents(): void + { + $events = TaskAssignEventSubscriber::getSubscribedEvents(); + + $this->assertArrayHasKey(AssignTaskEvent::PERSIST, $events); + $this->assertEquals(['onTaskAssigned', 0], $events[AssignTaskEvent::PERSIST]); + } + + public function testOnTaskAssignedCreatesNotificationWhenAssigneeChanges(): void + { + // Arrange + $task = $this->createMock(SingleTask::class); + $assignee = $this->createMock(User::class); + $event = $this->createMock(AssignTaskEvent::class); + + $task->method('getId')->willReturn(123); + $task->method('getTitle')->willReturn('Test Task'); + $task->method('getAssignee')->willReturn($assignee); + + $event->method('hasAssigneeChanged')->willReturn(true); + $event->method('getTask')->willReturn($task); + + $this->twig->expects($this->exactly(2)) + ->method('render') + ->with( + $this->logicalOr( + '@ChillTask/Notification/task_assignment_notification_title.txt.twig', + '@ChillTask/Notification/task_assignment_notification_content.txt.twig' + ), + $this->isType('array') + ) + ->willReturnOnConsecutiveCalls('Notification Title', 'Notification Content'); + + $this->entityManager->expects($this->once()) + ->method('persist') + ->with($this->isInstanceOf(Notification::class)); + + // Act + $this->subscriber->onTaskAssigned($event); + } + + public function testOnTaskAssignedDoesNothingWhenAssigneeDoesNotChange(): void + { + // Arrange + $event = $this->createMock(AssignTaskEvent::class); + $event->method('hasAssigneeChanged')->willReturn(false); + + $this->twig->expects($this->never())->method('render'); + $this->entityManager->expects($this->never())->method('persist'); + + // Act + $this->subscriber->onTaskAssigned($event); + } + + public function testNotificationHasCorrectProperties(): void + { + // Arrange + $task = $this->createMock(SingleTask::class); + $assignee = $this->createMock(User::class); + $event = $this->createMock(AssignTaskEvent::class); + + $task->method('getId')->willReturn(456); + $task->method('getTitle')->willReturn('Important Task'); + $task->method('getAssignee')->willReturn($assignee); + + $event->method('hasAssigneeChanged')->willReturn(true); + $event->method('getTask')->willReturn($task); + + $this->twig->method('render')->willReturn('Test Content'); + + // Capture the persisted notification + $persistedNotification = null; + $this->entityManager->expects($this->once()) + ->method('persist') + ->willReturnCallback(function ($notification) use (&$persistedNotification) { + $persistedNotification = $notification; + }); + + // Act + $this->subscriber->onTaskAssigned($event); + + // Assert + $this->assertInstanceOf(Notification::class, $persistedNotification); + $this->assertEquals(456, $persistedNotification->getRelatedEntityId()); + $this->assertEquals(SingleTask::class, $persistedNotification->getRelatedEntityClass()); + $this->assertEquals(AssignTaskNotificationFlagProvider::FLAG, $persistedNotification->getType()); + $this->assertEquals('Test Content', $persistedNotification->getTitle()); + $this->assertEquals('Test Content', $persistedNotification->getMessage()); + } +}