SprintWeekRepositoryTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests\Repositories;
  12. use App\Domain\SprintWeek;
  13. use App\Repositories\SprintRepository;
  14. use App\Repositories\SprintWeekRepository;
  15. use App\Tests\TestCase;
  16. /**
  17. * Phase 12: per-week weekday selection drives Arbeitstage.
  18. */
  19. final class SprintWeekRepositoryTest extends TestCase
  20. {
  21. /** @return array{SprintRepository, SprintWeekRepository, int} */
  22. private function seedSprint(): array
  23. {
  24. $pdo = $this->makeDb();
  25. $sprints = new SprintRepository($pdo);
  26. $weeks = new SprintWeekRepository($pdo);
  27. $sprint = $sprints->create('S', '2026-01-05', '2026-01-30', 0.2);
  28. $sprints->materializeWeeks($sprint->id, '2026-01-05', 4);
  29. return [$sprints, $weeks, $sprint->id];
  30. }
  31. public function testMaterializeWeeksDefaultsToAllFiveDays(): void
  32. {
  33. [, $weeks, $sprintId] = $this->seedSprint();
  34. $rows = $weeks->allForSprint($sprintId);
  35. $this->assertCount(4, $rows);
  36. foreach ($rows as $w) {
  37. $this->assertSame(SprintWeek::MASK_ALL, $w->activeDaysMask);
  38. $this->assertSame(5.0, $w->maxWorkingDays);
  39. }
  40. }
  41. public function testUpdateActiveDaysWritesBothColumns(): void
  42. {
  43. [, $weeks, $sprintId] = $this->seedSprint();
  44. $first = $weeks->allForSprint($sprintId)[0];
  45. $result = $weeks->updateActiveDays($first->id, 0b01111); // drop Fr
  46. $this->assertSame(SprintWeek::MASK_ALL, $result['before']->activeDaysMask);
  47. $this->assertSame(0b01111, $result['after']->activeDaysMask);
  48. $this->assertSame(4.0, $result['after']->maxWorkingDays);
  49. // Second query hydrates the same row from disk.
  50. $reloaded = $weeks->find($first->id);
  51. $this->assertNotNull($reloaded);
  52. $this->assertSame(0b01111, $reloaded->activeDaysMask);
  53. $this->assertSame(4.0, $reloaded->maxWorkingDays);
  54. }
  55. public function testUpdateActiveDaysClampsBitsOutsideMoFr(): void
  56. {
  57. [, $weeks, $sprintId] = $this->seedSprint();
  58. $first = $weeks->allForSprint($sprintId)[0];
  59. $result = $weeks->updateActiveDays($first->id, 0xFF);
  60. $this->assertSame(SprintWeek::MASK_ALL, $result['after']->activeDaysMask);
  61. $this->assertSame(5.0, $result['after']->maxWorkingDays);
  62. }
  63. public function testUpdateActiveDaysToEmptyMaskZeroesCount(): void
  64. {
  65. [, $weeks, $sprintId] = $this->seedSprint();
  66. $first = $weeks->allForSprint($sprintId)[0];
  67. $result = $weeks->updateActiveDays($first->id, 0);
  68. $this->assertSame(0, $result['after']->activeDaysMask);
  69. $this->assertSame(0.0, $result['after']->maxWorkingDays);
  70. $this->assertSame([], $result['after']->activeDays());
  71. }
  72. public function testSyncCountAppendsWeeksWithAllDaysActive(): void
  73. {
  74. [, $weeks, $sprintId] = $this->seedSprint();
  75. $diff = $weeks->syncCount($sprintId, '2026-01-05', 6);
  76. $this->assertCount(2, $diff['added']);
  77. foreach ($diff['added'] as $w) {
  78. $this->assertSame(SprintWeek::MASK_ALL, $w->activeDaysMask);
  79. $this->assertSame(5.0, $w->maxWorkingDays);
  80. }
  81. }
  82. public function testRealignDatesRewritesOffsetsAndPreservesMask(): void
  83. {
  84. [, $weeks, $sprintId] = $this->seedSprint();
  85. // Customise week 2's mask so we can verify it survives realignment.
  86. $week2 = $weeks->allForSprint($sprintId)[1];
  87. $weeks->updateActiveDays($week2->id, 0b00111); // Mo Di Mi only
  88. // Move the sprint start one week forward.
  89. $diffs = $weeks->realignDates($sprintId, '2026-01-12');
  90. $this->assertCount(4, $diffs, 'every existing week shifted one week');
  91. $reloaded = $weeks->allForSprint($sprintId);
  92. $this->assertSame('2026-01-12', $reloaded[0]->startDate);
  93. $this->assertSame('2026-01-19', $reloaded[1]->startDate);
  94. $this->assertSame('2026-01-26', $reloaded[2]->startDate);
  95. $this->assertSame('2026-02-02', $reloaded[3]->startDate);
  96. // Mask + maxWorkingDays untouched by realignment.
  97. $this->assertSame(0b00111, $reloaded[1]->activeDaysMask);
  98. $this->assertSame(3.0, $reloaded[1]->maxWorkingDays);
  99. }
  100. public function testRealignDatesNoOpWhenAlreadyAligned(): void
  101. {
  102. [, $weeks, $sprintId] = $this->seedSprint();
  103. $diffs = $weeks->realignDates($sprintId, '2026-01-05');
  104. $this->assertSame([], $diffs, 'no UPDATEs when offsets already match');
  105. }
  106. }