kimai-plugin-heatmap/Tests/Controller/HeatmapControllerTest.php

193 lines
7 KiB
PHP

<?php
namespace KimaiPlugin\KimaiHeatmapBundle\Tests\Controller;
use App\Entity\User;
use KimaiPlugin\KimaiHeatmapBundle\Controller\HeatmapController;
use KimaiPlugin\KimaiHeatmapBundle\Service\HeatmapService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class HeatmapControllerTest extends TestCase
{
private function createControllerWithUser(): array
{
$user = $this->createMock(User::class);
$user->method('getTimezone')->willReturn('Europe/Berlin');
$user->method('getFirstDayOfWeek')->willReturn('monday');
$controller = new HeatmapController();
$container = $this->createMock(\Symfony\Component\DependencyInjection\ContainerInterface::class);
$tokenStorage = $this->createMock(\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface::class);
$token = $this->createMock(\Symfony\Component\Security\Core\Authentication\Token\TokenInterface::class);
$token->method('getUser')->willReturn($user);
$tokenStorage->method('getToken')->willReturn($token);
$container->method('has')->willReturn(true);
$container->method('get')->willReturn($tokenStorage);
$controller->setContainer($container);
return [$controller, $user];
}
public function testDataReturnsJsonWithDaysAndRange(): void
{
$mockDays = [
['date' => '2026-04-01', 'hours' => 2.0, 'count' => 3],
];
$service = $this->createMock(HeatmapService::class);
$service->method('getDailyAggregation')->willReturn($mockDays);
[$controller, $user] = $this->createControllerWithUser();
$response = $controller->data(new Request(), $service);
$this->assertInstanceOf(JsonResponse::class, $response);
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('days', $data);
$this->assertArrayHasKey('range', $data);
$this->assertCount(1, $data['days']);
$this->assertArrayHasKey('begin', $data['range']);
$this->assertArrayHasKey('end', $data['range']);
}
public function testDataReturnsHourlyMode(): void
{
$mockHours = [['hour' => 9, 'hours' => 2.0, 'count' => 5]];
$service = $this->createMock(HeatmapService::class);
$service->method('getHourlyAggregation')->willReturn($mockHours);
[$controller, $user] = $this->createControllerWithUser();
$request = new Request(['mode' => 'hourly']);
$response = $controller->data($request, $service);
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('hours', $data);
$this->assertArrayHasKey('range', $data);
$this->assertEquals($mockHours, $data['hours']);
}
public function testDataReturnsDayHourMode(): void
{
$mockMatrix = [['day' => 0, 'hour' => 9, 'hours' => 1.0, 'count' => 1]];
$service = $this->createMock(HeatmapService::class);
$service->method('getDayHourAggregation')->willReturn($mockMatrix);
[$controller, $user] = $this->createControllerWithUser();
$request = new Request(['mode' => 'dayhour']);
$response = $controller->data($request, $service);
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('matrix', $data);
$this->assertArrayHasKey('range', $data);
$this->assertEquals($mockMatrix, $data['matrix']);
}
public function testDataDefaultsToDailyMode(): void
{
$mockDays = [['date' => '2026-01-01', 'hours' => 1.0, 'count' => 2]];
$service = $this->createMock(HeatmapService::class);
$service->method('getDailyAggregation')->willReturn($mockDays);
[$controller, $user] = $this->createControllerWithUser();
$request = new Request(); // no mode param
$response = $controller->data($request, $service);
$data = json_decode($response->getContent(), true);
$this->assertArrayHasKey('days', $data);
$this->assertEquals($mockDays, $data['days']);
}
public function testDataPassesFilterParams(): void
{
$service = $this->createMock(HeatmapService::class);
$service->expects($this->once())
->method('getDailyAggregation')
->with(
$this->anything(), // user
$this->anything(), // begin
$this->anything(), // end
$this->isNull(), // projectId
3, // customerId
5 // activityId
)
->willReturn([]);
[$controller, $user] = $this->createControllerWithUser();
$request = new Request(['activity' => '5', 'customer' => '3']);
$controller->data($request, $service);
}
public function testCustomersEndpoint(): void
{
$mockCustomers = [['id' => 1, 'name' => 'Acme']];
$service = $this->createMock(HeatmapService::class);
$service->method('getUserCustomers')->willReturn($mockCustomers);
[$controller, $user] = $this->createControllerWithUser();
$response = $controller->customers($service);
$this->assertInstanceOf(JsonResponse::class, $response);
$data = json_decode($response->getContent(), true);
$this->assertEquals($mockCustomers, $data);
}
public function testProjectsEndpoint(): void
{
$mockProjects = [['id' => 1, 'name' => 'Web']];
$service = $this->createMock(HeatmapService::class);
$service->method('getUserProjects')->willReturn($mockProjects);
[$controller, $user] = $this->createControllerWithUser();
$response = $controller->projects(new Request(), $service);
$this->assertInstanceOf(JsonResponse::class, $response);
$data = json_decode($response->getContent(), true);
$this->assertEquals($mockProjects, $data);
}
public function testActivitiesEndpoint(): void
{
$mockActivities = [['id' => 1, 'name' => 'Dev']];
$service = $this->createMock(HeatmapService::class);
$service->method('getUserActivities')->willReturn($mockActivities);
[$controller, $user] = $this->createControllerWithUser();
$response = $controller->activities(new Request(), $service);
$this->assertInstanceOf(JsonResponse::class, $response);
$data = json_decode($response->getContent(), true);
$this->assertEquals($mockActivities, $data);
}
public function testActivitiesWithProjectParam(): void
{
$service = $this->createMock(HeatmapService::class);
$service->expects($this->once())
->method('getUserActivities')
->with(
$this->anything(), // user
3 // projectId
)
->willReturn([]);
[$controller, $user] = $this->createControllerWithUser();
$request = new Request(['project' => '3']);
$controller->activities($request, $service);
}
}