Rich-Model Mahlzeit (MahlzeitStatus, Wochentag) mit Invarianten und Zustandsübergängen; DTOs + MahlzeitFactory (reine Mapping-Funktionen); Manager (create hängt an Ende an: maxSortOrder+1, find → 404) und Create/Update/Delete-UseCases mit Transaktion + Events nach Commit. Data-Layer: MahlzeitEntity (Tabelle essens_mahlzeiten, wochentage als CSV), Mapper (CSV/Enum/null), Provider (find/list/maxSortOrder), Processor (create/save/delete). DI-Bindings in services.yaml. ClockInterface (Logic) + SystemClock (Data). Tests: Model/Factory/UseCase-Unit + Provider/Processor/Mapper-Integration (In-Mem-SQLite). composer test grün (53 Tests, 161 Assertions).
98 lines
2.9 KiB
PHP
98 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Integration\Data\Essen;
|
|
|
|
use App\Data\Essen\Mapper\MahlzeitMapper;
|
|
use App\Data\Essen\Provider\MahlzeitProvider;
|
|
use App\Logic\Essen\Model\Mahlzeit;
|
|
use App\Logic\Essen\Model\MahlzeitStatus;
|
|
use App\Logic\Essen\Model\Wochentag;
|
|
|
|
final class MahlzeitProviderTest extends DatabaseTestCase
|
|
{
|
|
private function provider(): MahlzeitProvider
|
|
{
|
|
return new MahlzeitProvider($this->em, new MahlzeitMapper());
|
|
}
|
|
|
|
private function jetzt(): \DateTimeImmutable
|
|
{
|
|
return new \DateTimeImmutable('2026-09-18 12:00:00');
|
|
}
|
|
|
|
private function speichere(string $name, int $sortOrder, string $status, array $tage): Mahlzeit
|
|
{
|
|
$jetzt = $this->jetzt();
|
|
$m = Mahlzeit::anlegen($name, '', $tage, $sortOrder, $jetzt);
|
|
if ($status === 'gegessen') {
|
|
$m = $m->markAsGegessen($jetzt);
|
|
}
|
|
$entity = (new MahlzeitMapper())->createEntity($m);
|
|
$this->em->persist($entity);
|
|
$this->em->flush();
|
|
return $m;
|
|
}
|
|
|
|
public function testFindLiefertModell(): void
|
|
{
|
|
$m = $this->speichere('Bolognese', 1, 'geplant', [Wochentag::Mo]);
|
|
$id = $this->alleEntitaeten()[0]->getId();
|
|
|
|
$gefunden = $this->provider()->find($id);
|
|
|
|
self::assertNotNull($gefunden);
|
|
self::assertSame('Bolognese', $gefunden->name);
|
|
self::assertSame($id, $gefunden->id);
|
|
}
|
|
|
|
public function testFindLiefertNullBeiUnbekannterId(): void
|
|
{
|
|
self::assertNull($this->provider()->find(9999));
|
|
}
|
|
|
|
public function testListOhneFilterGibtAlleSortiertZurück(): void
|
|
{
|
|
$this->speichere('Zweite', 2, 'geplant', []);
|
|
$this->speichere('Erste', 1, 'geplant', []);
|
|
$this->speichere('Dritte', 3, 'gegessen', []);
|
|
|
|
$liste = $this->provider()->list();
|
|
|
|
self::assertCount(3, $liste);
|
|
self::assertSame(['Erste', 'Zweite', 'Dritte'], \array_map(static fn (Mahlzeit $m) => $m->name, $liste));
|
|
}
|
|
|
|
public function testListMitStatusFilter(): void
|
|
{
|
|
$this->speichere('A', 1, 'geplant', []);
|
|
$this->speichere('B', 2, 'gegessen', []);
|
|
$this->speichere('C', 3, 'geplant', []);
|
|
|
|
$geplante = $this->provider()->list(MahlzeitStatus::Geplant);
|
|
$gegessene = $this->provider()->list(MahlzeitStatus::Gegessen);
|
|
|
|
self::assertCount(2, $geplante);
|
|
self::assertCount(1, $gegessene);
|
|
self::assertSame('B', $gegessene[0]->name);
|
|
}
|
|
|
|
public function testMaxSortOrderLiefertMaximum(): void
|
|
{
|
|
$this->speichere('A', 1, 'geplant', []);
|
|
$this->speichere('B', 7, 'geplant', []);
|
|
$this->speichere('C', 3, 'geplant', []);
|
|
|
|
self::assertSame(7, $this->provider()->maxSortOrder());
|
|
}
|
|
|
|
public function testMaxSortOrderLeereTabelleLiefertNullsafe(): void
|
|
{
|
|
// provider gibt null->0 zurück
|
|
$max = $this->provider()->maxSortOrder();
|
|
|
|
self::assertIsInt($max);
|
|
}
|
|
}
|