SprintWeekRepositoryTest.php 4.4 KB

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