test(08-02): add failing tests for mode dispatch and cascade endpoints
This commit is contained in:
parent
3a91f993a0
commit
86e7d5f209
1 changed files with 159 additions and 12 deletions
|
|
@ -11,6 +11,25 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
class HeatmapControllerTest extends TestCase
|
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
|
public function testDataReturnsJsonWithDaysAndRange(): void
|
||||||
{
|
{
|
||||||
$mockDays = [
|
$mockDays = [
|
||||||
|
|
@ -20,18 +39,7 @@ class HeatmapControllerTest extends TestCase
|
||||||
$service = $this->createMock(HeatmapService::class);
|
$service = $this->createMock(HeatmapService::class);
|
||||||
$service->method('getDailyAggregation')->willReturn($mockDays);
|
$service->method('getDailyAggregation')->willReturn($mockDays);
|
||||||
|
|
||||||
$controller = new HeatmapController();
|
[$controller, $user] = $this->createControllerWithUser();
|
||||||
|
|
||||||
// Mock user via reflection (AbstractController::getUser is protected)
|
|
||||||
$user = $this->createMock(User::class);
|
|
||||||
$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);
|
|
||||||
|
|
||||||
$response = $controller->data(new Request(), $service);
|
$response = $controller->data(new Request(), $service);
|
||||||
|
|
||||||
|
|
@ -43,4 +51,143 @@ class HeatmapControllerTest extends TestCase
|
||||||
$this->assertArrayHasKey('begin', $data['range']);
|
$this->assertArrayHasKey('begin', $data['range']);
|
||||||
$this->assertArrayHasKey('end', $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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue