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).
96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Integration\Data\Essen;
|
|
|
|
use App\Data\Essen\Mapper\MahlzeitMapper;
|
|
use App\Data\Essen\Processor\MahlzeitProcessor;
|
|
use App\Data\Essen\Provider\MahlzeitProvider;
|
|
use App\Logic\Essen\Exception\MahlzeitNotFoundException;
|
|
use App\Logic\Essen\Model\Mahlzeit;
|
|
use App\Logic\Essen\Model\MahlzeitStatus;
|
|
use App\Logic\Essen\Model\Wochentag;
|
|
|
|
final class MahlzeitProcessorTest extends DatabaseTestCase
|
|
{
|
|
private function processor(): MahlzeitProcessor
|
|
{
|
|
return new MahlzeitProcessor($this->em, new MahlzeitMapper());
|
|
}
|
|
|
|
private function jetzt(): \DateTimeImmutable
|
|
{
|
|
return new \DateTimeImmutable('2026-09-18 12:00:00');
|
|
}
|
|
|
|
public function testCreatePersistiertUndLiefertMitNeuerId(): void
|
|
{
|
|
$m = Mahlzeit::anlegen('Bolognese', 'süß', [Wochentag::Mo, Wochentag::Do], 0, $this->jetzt());
|
|
|
|
$gespeichert = $this->processor()->create($m);
|
|
|
|
self::assertGreaterThan(0, $gespeichert->id);
|
|
self::assertCount(1, $this->alleEntitaeten());
|
|
|
|
$neu = (new MahlzeitProvider($this->em, new MahlzeitMapper()))->find($gespeichert->id);
|
|
self::assertNotNull($neu);
|
|
self::assertSame('Bolognese', $neu->name);
|
|
self::assertSame('süß', $neu->notiz);
|
|
self::assertSame([Wochentag::Mo, Wochentag::Do], $neu->wochentage());
|
|
}
|
|
|
|
public function testSaveAktualisiertBestehendeZeile(): void
|
|
{
|
|
$m = Mahlzeit::anlegen('Altes', '', [Wochentag::Mo], 1, $this->jetzt());
|
|
$created = $this->processor()->create($m);
|
|
|
|
$geaendert = $created->aktualisieren(
|
|
name: 'Neues',
|
|
notiz: 'scharf',
|
|
wochentage: [Wochentag::Fr],
|
|
sortOrder: $created->sortOrder,
|
|
jetzt: $this->jetzt(),
|
|
);
|
|
|
|
$gespeichert = $this->processor()->save($geaendert);
|
|
|
|
self::assertSame($created->id, $gespeichert->id);
|
|
self::assertCount(1, $this->alleEntitaeten());
|
|
|
|
$neu = (new MahlzeitProvider($this->em, new MahlzeitMapper()))->find($created->id);
|
|
self::assertNotNull($neu);
|
|
self::assertSame('Neues', $neu->name);
|
|
self::assertSame('scharf', $neu->notiz);
|
|
self::assertSame([Wochentag::Fr], $neu->wochentage());
|
|
}
|
|
|
|
public function testSaveMitUnbekannterIdWirft404(): void
|
|
{
|
|
$m = Mahlzeit::anlegen('X', '', [], 0, $this->jetzt())->mitId(9999);
|
|
|
|
$this->expectException(MahlzeitNotFoundException::class);
|
|
|
|
$this->processor()->save($m);
|
|
}
|
|
|
|
public function testDeleteEntferntZeile(): void
|
|
{
|
|
$m = Mahlzeit::anlegen('Bolognese', '', [], 1, $this->jetzt());
|
|
$created = $this->processor()->create($m);
|
|
|
|
$this->processor()->delete($created);
|
|
|
|
self::assertCount(0, $this->alleEntitaeten());
|
|
}
|
|
|
|
public function testDeleteMitUnbekannterIdWirft404(): void
|
|
{
|
|
$m = Mahlzeit::anlegen('X', '', [], 0, $this->jetzt())->mitId(9999);
|
|
|
|
$this->expectException(MahlzeitNotFoundException::class);
|
|
|
|
$this->processor()->delete($m);
|
|
}
|
|
}
|