Add TaskAssignEventSubscriber unit tests

- Covered `getSubscribedEvents` method behavior
- Added tests for `onTaskAssigned` with changed and unchanged assignees
- Validated notification properties in appropriate scenarios
This commit is contained in:
2025-08-25 19:38:42 +02:00
parent 6d9834de71
commit 9df423a201

View File

@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace Chill\TaskBundle\Tests\EventSubscriber;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\TaskBundle\Event\AssignTaskEvent;
use Chill\TaskBundle\Event\TaskAssignEventSubscriber;
use Chill\TaskBundle\Notification\AssignTaskNotificationFlagProvider;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
class TaskAssignEventSubscriberTest extends TestCase
{
private EntityManagerInterface $entityManager;
private Environment $twig;
private TaskAssignEventSubscriber $subscriber;
protected function setUp(): void
{
$this->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());
}
}