M1: CRUD-Slice 'Mahlzeit' Schritt 1 – Logik + Daten + Tests

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).
This commit is contained in:
2026-09-19 08:55:54 +02:00
parent cb3cbfb394
commit 1589088379
42 changed files with 2493 additions and 138 deletions
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Tests\Integration\Data\Essen;
use App\Data\Essen\Entity\MahlzeitEntity;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Basis für DB-Integrationstests auf In-Memory-SQLite:
* Kernel booten, Schema erzeugen, sauberen EntityManager liefern.
*/
abstract class DatabaseTestCase extends KernelTestCase
{
protected EntityManagerInterface $em;
protected function setUp(): void
{
parent::setUp();
$container = static::getContainer();
$this->em = $container->get('doctrine')->getManager();
$schemaTool = new SchemaTool($this->em);
$schemaTool->dropSchema($this->em->getMetadataFactory()->getAllMetadata());
$schemaTool->createSchema($this->em->getMetadataFactory()->getAllMetadata());
}
protected function tearDown(): void
{
$this->em->close();
parent::tearDown();
}
/** @return list<MahlzeitEntity> */
protected function alleEntitaeten(): array
{
return $this->em->getRepository(MahlzeitEntity::class)->findAll();
}
}
@@ -0,0 +1,151 @@
<?php
declare(strict_types=1);
namespace App\Tests\Integration\Data\Essen;
use App\Data\Essen\Entity\MahlzeitEntity;
use App\Data\Essen\Mapper\MahlzeitMapper;
use App\Logic\Essen\Model\Mahlzeit;
use App\Logic\Essen\Model\MahlzeitStatus;
use App\Logic\Essen\Model\Wochentag;
use PHPUnit\Framework\TestCase;
final class MahlzeitMapperTest extends TestCase
{
private function mapper(): MahlzeitMapper
{
return new MahlzeitMapper();
}
private function jetzt(): \DateTimeImmutable
{
return new \DateTimeImmutable('2026-09-18 12:00:00');
}
public function testModelZuEntityMapptAlleFelder(): void
{
$jetzt = $this->jetzt();
$gegessenAm = $jetzt->modify('+1 day');
$model = new Mahlzeit(
id: 0,
name: 'Bolognese',
notiz: 'süß',
wochentage: [Wochentag::Mo, Wochentag::Do, Wochentag::Fr],
sortOrder: 2,
status: MahlzeitStatus::Gegessen,
erstelltAm: $jetzt,
aktualisiertAm: $jetzt,
gegessenAm: $gegessenAm,
);
$entity = $this->mapper()->createEntity($model);
self::assertSame('Bolognese', $entity->getName());
self::assertSame('süß', $entity->getNotiz());
self::assertSame('mo,do,fr', $entity->getWochentage());
self::assertSame(2, $entity->getSortOrder());
self::assertSame('gegessen', $entity->getStatus());
self::assertSame($gegessenAm, $entity->getGegessenAm());
self::assertSame($jetzt, $entity->getErstelltAm());
}
public function testLeereWochentageWerdenNull(): void
{
$jetzt = $this->jetzt();
$model = new Mahlzeit(1, 'X', '', [], 0, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
$entity = $this->mapper()->createEntity($model);
self::assertNull($entity->getWochentage());
}
public function testLeereNotizWirdNull(): void
{
$jetzt = $this->jetzt();
$model = new Mahlzeit(1, 'X', '', [], 0, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
$entity = $this->mapper()->createEntity($model);
self::assertNull($entity->getNotiz());
}
public function testEntityZuModelMapptAlleFelder(): void
{
$jetzt = $this->jetzt();
$gegessenAm = $jetzt->modify('+1 day');
$entity = new MahlzeitEntity();
$entity->setName('Linsen');
$entity->setNotiz('scharf');
$entity->setWochentage('mi,sa');
$entity->setSortOrder(4);
$entity->setStatus('gegessen');
$entity->setGegessenAm($gegessenAm);
$entity->setErstelltAm($jetzt);
$entity->setAktualisiertAm($jetzt);
$model = $this->mapper()->toModel($entity);
self::assertSame('Linsen', $model->name);
self::assertSame('scharf', $model->notiz);
self::assertSame([Wochentag::Mi, Wochentag::Sa], $model->wochentage());
self::assertSame(4, $model->sortOrder);
self::assertSame(MahlzeitStatus::Gegessen, $model->status);
self::assertSame($gegessenAm, $model->gegessenAm);
self::assertTrue($model->istGegessen());
}
public function testEntityMitLeerenWochentageLiefertLeeresArray(): void
{
$jetzt = $this->jetzt();
$entity = new MahlzeitEntity();
$entity->setName('X');
$entity->setNotiz(null);
$entity->setWochentage(null);
$entity->setSortOrder(0);
$entity->setStatus('geplant');
$entity->setGegessenAm(null);
$entity->setErstelltAm($jetzt);
$entity->setAktualisiertAm($jetzt);
$model = $this->mapper()->toModel($entity);
self::assertSame([], $model->wochentage());
self::assertSame('', $model->notiz);
self::assertTrue($model->istGeplant());
}
public function testUngültigeWochentagswerteWerdenGefiltert(): void
{
$jetzt = $this->jetzt();
$entity = new MahlzeitEntity();
$entity->setName('X');
$entity->setWochentage('mo,xx,di');
$entity->setSortOrder(0);
$entity->setStatus('geplant');
$entity->setErstelltAm($jetzt);
$entity->setAktualisiertAm($jetzt);
$model = $this->mapper()->toModel($entity);
self::assertSame([Wochentag::Mo, Wochentag::Di], $model->wochentage());
}
public function testUpdateEntitySchreibtNeueWerte(): void
{
$jetzt = $this->jetzt();
$entity = new MahlzeitEntity();
$entity->setSortOrder(0);
$entity->setErstelltAm($jetzt);
$entity->setAktualisiertAm($jetzt);
$model = new Mahlzeit(5, 'Neu', 'notiz', [Wochentag::So], 9, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
$this->mapper()->updateEntity($model, $entity);
self::assertSame('Neu', $entity->getName());
self::assertSame('so', $entity->getWochentage());
self::assertSame(9, $entity->getSortOrder());
}
}
@@ -0,0 +1,95 @@
<?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);
}
}
@@ -0,0 +1,97 @@
<?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);
}
}