Fix test according to Junie guidelines

This commit is contained in:
2025-09-29 16:23:13 +02:00
parent c731f1967b
commit 20bbb6b485

View File

@@ -19,6 +19,9 @@ use Chill\TaskBundle\Event\TaskAssignEventSubscriber;
use Chill\TaskBundle\Notification\AssignTaskNotificationFlagProvider; use Chill\TaskBundle\Notification\AssignTaskNotificationFlagProvider;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Twig\Environment; use Twig\Environment;
/** /**
@@ -28,53 +31,53 @@ use Twig\Environment;
*/ */
class TaskAssignEventSubscriberTest extends TestCase class TaskAssignEventSubscriberTest extends TestCase
{ {
private EntityManagerInterface $entityManager; use ProphecyTrait;
private Environment $twig;
private ObjectProphecy $entityManager;
private ObjectProphecy $twig;
private TaskAssignEventSubscriber $subscriber; private TaskAssignEventSubscriber $subscriber;
protected function setUp(): void protected function setUp(): void
{ {
$this->entityManager = $this->createMock(EntityManagerInterface::class); $this->entityManager = $this->prophesize(EntityManagerInterface::class);
$this->twig = $this->createMock(Environment::class); $this->twig = $this->prophesize(Environment::class);
$this->subscriber = new TaskAssignEventSubscriber($this->entityManager, $this->twig); $this->subscriber = new TaskAssignEventSubscriber(
$this->entityManager->reveal(),
$this->twig->reveal()
);
} }
public function testGetSubscribedEvents(): void private function setEntityId(object $entity, int $id): void
{ {
$events = TaskAssignEventSubscriber::getSubscribedEvents(); $reflection = new \ReflectionClass($entity);
$property = $reflection->getProperty('id');
$this->assertArrayHasKey(AssignTaskEvent::PERSIST, $events); $property->setAccessible(true);
$this->assertEquals(['onTaskAssigned', 0], $events[AssignTaskEvent::PERSIST]); $property->setValue($entity, $id);
} }
public function testOnTaskAssignedCreatesNotificationWhenAssigneeChanges(): void public function testOnTaskAssignedCreatesNotificationWhenAssigneeChanges(): void
{ {
// Arrange // Arrange
$task = $this->createMock(SingleTask::class); $initialAssignee = new User();
$assignee = $this->createMock(User::class); $newAssignee = new User();
$event = $this->createMock(AssignTaskEvent::class);
$task->method('getId')->willReturn(123); $task = new SingleTask();
$task->method('getTitle')->willReturn('Test Task'); $task->setTitle('Test Task');
$task->method('getAssignee')->willReturn($assignee); $task->setAssignee($newAssignee);
$this->setEntityId($task, 123);
$event->method('hasAssigneeChanged')->willReturn(true); $event = new AssignTaskEvent($task, $initialAssignee);
$event->method('getTask')->willReturn($task);
$this->twig->expects($this->exactly(2)) $this->twig->render('@ChillTask/Notification/task_assignment_notification_title.txt.twig', Argument::type('array'))
->method('render') ->shouldBeCalledOnce()
->with( ->willReturn('Notification Title');
$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()) $this->twig->render('@ChillTask/Notification/task_assignment_notification_content.txt.twig', Argument::type('array'))
->method('persist') ->shouldBeCalledOnce()
->with($this->isInstanceOf(Notification::class)); ->willReturn('Notification Content');
$this->entityManager->persist(Argument::type(Notification::class))
->shouldBeCalledOnce();
// Act // Act
$this->subscriber->onTaskAssigned($event); $this->subscriber->onTaskAssigned($event);
@@ -83,11 +86,16 @@ class TaskAssignEventSubscriberTest extends TestCase
public function testOnTaskAssignedDoesNothingWhenAssigneeDoesNotChange(): void public function testOnTaskAssignedDoesNothingWhenAssigneeDoesNotChange(): void
{ {
// Arrange // Arrange
$event = $this->createMock(AssignTaskEvent::class); $assignee = new User();
$event->method('hasAssigneeChanged')->willReturn(false);
$this->twig->expects($this->never())->method('render'); $task = new SingleTask();
$this->entityManager->expects($this->never())->method('persist'); $task->setTitle('Test Task');
$task->setAssignee($assignee);
$event = new AssignTaskEvent($task, $assignee);
$this->twig->render(Argument::any(), Argument::any())->shouldNotBeCalled();
$this->entityManager->persist(Argument::any())->shouldNotBeCalled();
// Act // Act
$this->subscriber->onTaskAssigned($event); $this->subscriber->onTaskAssigned($event);
@@ -96,25 +104,24 @@ class TaskAssignEventSubscriberTest extends TestCase
public function testNotificationHasCorrectProperties(): void public function testNotificationHasCorrectProperties(): void
{ {
// Arrange // Arrange
$task = $this->createMock(SingleTask::class); $initialAssignee = new User();
$assignee = $this->createMock(User::class); $newAssignee = new User();
$event = $this->createMock(AssignTaskEvent::class);
$task->method('getId')->willReturn(456); $task = new SingleTask();
$task->method('getTitle')->willReturn('Important Task'); $task->setTitle('Important Task');
$task->method('getAssignee')->willReturn($assignee); $task->setAssignee($newAssignee);
$this->setEntityId($task, 456);
$event->method('hasAssigneeChanged')->willReturn(true); $event = new AssignTaskEvent($task, $initialAssignee);
$event->method('getTask')->willReturn($task);
$this->twig->method('render')->willReturn('Test Content'); $this->twig->render(Argument::any(), Argument::any())->willReturn('Test Content');
// Capture the persisted notification // Capture the persisted notification
$persistedNotification = null; $persistedNotification = null;
$this->entityManager->expects($this->once()) $this->entityManager->persist(Argument::type(Notification::class))
->method('persist') ->shouldBeCalledOnce()
->willReturnCallback(function ($notification) use (&$persistedNotification) { ->will(function ($args) use (&$persistedNotification) {
$persistedNotification = $notification; $persistedNotification = $args[0];
}); });
// Act // Act
@@ -122,7 +129,7 @@ class TaskAssignEventSubscriberTest extends TestCase
// Assert // Assert
$this->assertInstanceOf(Notification::class, $persistedNotification); $this->assertInstanceOf(Notification::class, $persistedNotification);
$this->assertEquals(456, $persistedNotification->getRelatedEntityId()); $this->assertEquals($task->getId(), $persistedNotification->getRelatedEntityId());
$this->assertEquals(SingleTask::class, $persistedNotification->getRelatedEntityClass()); $this->assertEquals(SingleTask::class, $persistedNotification->getRelatedEntityClass());
$this->assertEquals(AssignTaskNotificationFlagProvider::FLAG, $persistedNotification->getType()); $this->assertEquals(AssignTaskNotificationFlagProvider::FLAG, $persistedNotification->getType());
$this->assertEquals('Test Content', $persistedNotification->getTitle()); $this->assertEquals('Test Content', $persistedNotification->getTitle());