XlsxSprintImporterTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Services\Import;
  4. use App\Domain\Import\ParsedSheet;
  5. use App\Domain\TaskAssignment;
  6. use App\Services\Import\XlsxSprintImporter;
  7. use App\Tests\TestCase;
  8. /**
  9. * Phase 20 — parser smoke test against the real sample workbook
  10. * (doc/Tool_Sprint Planning.xlsx). Skipped when the host PHP lacks the
  11. * extensions PhpSpreadsheet needs (ext-dom, ext-zip etc.); the production
  12. * Docker image and standard CI runners both have them.
  13. */
  14. final class XlsxSprintImporterTest extends TestCase
  15. {
  16. private const FIXTURE = __DIR__ . '/../../../doc/Tool_Sprint Planning.xlsx';
  17. protected function setUp(): void
  18. {
  19. foreach (['dom', 'zip', 'xmlreader', 'simplexml', 'gd'] as $ext) {
  20. if (!extension_loaded($ext)) {
  21. $this->markTestSkipped("ext-{$ext} not loaded; PhpSpreadsheet cannot run on this host.");
  22. }
  23. }
  24. if (!is_file(self::FIXTURE)) {
  25. $this->markTestSkipped('Sample workbook not present at ' . self::FIXTURE);
  26. }
  27. }
  28. public function testParsesEverySheet(): void
  29. {
  30. $parser = new XlsxSprintImporter();
  31. $sheets = $parser->parse(self::FIXTURE);
  32. $this->assertCount(3, $sheets);
  33. $this->assertSame(['Sprint 1', 'Sprint 2', 'Sprint 3'], array_map(fn(ParsedSheet $s) => $s->sheetName, $sheets));
  34. }
  35. public function testSprint1ShapeAndCounts(): void
  36. {
  37. $parser = new XlsxSprintImporter();
  38. $sheets = $parser->parse(self::FIXTURE);
  39. $s = $sheets[0];
  40. $this->assertSame(5, count($s->weeks), '5 weeks');
  41. $this->assertSame(15, count($s->workers), '15 workers');
  42. $this->assertGreaterThan(20, count($s->tasks), 'more than 20 tasks');
  43. $this->assertEqualsWithDelta(0.2, $s->reserveFraction, 1e-9, 'reserve fraction = 0.2');
  44. $kws = array_map(fn($w) => $w->kw, $s->weeks);
  45. $this->assertSame([13, 14, 15, 16, 17], $kws, 'KWs 13..17 in order');
  46. $maxDays = array_map(fn($w) => $w->maxWorkingDays, $s->weeks);
  47. $this->assertSame([2, 4, 4, 5, 2], $maxDays);
  48. }
  49. public function testSprint2ColourMappingMatchesSpreadsheet(): void
  50. {
  51. $parser = new XlsxSprintImporter();
  52. $sheets = $parser->parse(self::FIXTURE);
  53. $s2 = $sheets[1];
  54. $this->assertSame('Sprint 2', $s2->sheetName);
  55. $statusCounts = [
  56. TaskAssignment::STATUS_ZUGEWIESEN => 0,
  57. TaskAssignment::STATUS_GESTARTET => 0,
  58. TaskAssignment::STATUS_ABGESCHLOSSEN => 0,
  59. TaskAssignment::STATUS_ABGEBROCHEN => 0,
  60. ];
  61. foreach ($s2->tasks as $t) {
  62. foreach ($t->assignments as $a) {
  63. $statusCounts[$a->status]++;
  64. }
  65. }
  66. // From the openpyxl colour audit on the sample:
  67. // 17× FFFFFF00 + 6× FFFFEB9C + 4× FFFFC000 = 27 yellow/orange -> gestartet
  68. // 4× FF00B050 + 1× FFC6EFCE = 5 green -> abgeschlossen
  69. // the only red-coded cells in the workbook are zero.
  70. $this->assertSame(27, $statusCounts[TaskAssignment::STATUS_GESTARTET], 'yellow + orange cells = 27');
  71. $this->assertSame(5, $statusCounts[TaskAssignment::STATUS_ABGESCHLOSSEN], 'green cells = 5');
  72. $this->assertSame(0, $statusCounts[TaskAssignment::STATUS_ABGEBROCHEN], 'no red cells in sample');
  73. }
  74. public function testSprint2SkipsArbeitstageGapAndDefinesSixteenWorkers(): void
  75. {
  76. // Sprint 2's Arbeitstage block has a blank row at C13 between Titus and
  77. // Suzan; the parser should resume past the gap and end up with 16 workers.
  78. $parser = new XlsxSprintImporter();
  79. $sheets = $parser->parse(self::FIXTURE);
  80. $s2 = $sheets[1];
  81. $this->assertCount(16, $s2->workers, 'Sprint 2 has 16 workers (gap row tolerated)');
  82. $names = array_map(fn($w) => $w->name, $s2->workers);
  83. $this->assertContains('Suzan', $names);
  84. $this->assertContains('Nicole', $names);
  85. }
  86. public function testRoundTripsViaToArrayFromArray(): void
  87. {
  88. $parser = new XlsxSprintImporter();
  89. $sheets = $parser->parse(self::FIXTURE);
  90. foreach ($sheets as $orig) {
  91. $clone = ParsedSheet::fromArray($orig->toArray());
  92. $this->assertSame($orig->sheetName, $clone->sheetName);
  93. $this->assertSame(count($orig->weeks), count($clone->weeks));
  94. $this->assertSame(count($orig->workers), count($clone->workers));
  95. $this->assertSame(count($orig->tasks), count($clone->tasks));
  96. $this->assertEqualsWithDelta($orig->reserveFraction, $clone->reserveFraction, 1e-9);
  97. }
  98. }
  99. }