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);
}
}
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Logic\Essen\Mapping;
use App\Logic\Common\Exception\BusinessRuleViolationException;
use App\Logic\Essen\Dto\MahlzeitCreateRequest;
use App\Logic\Essen\Dto\MahlzeitUpdateRequest;
use App\Logic\Essen\Mapping\MahlzeitFactory;
use App\Logic\Essen\Model\Mahlzeit;
use App\Logic\Essen\Model\MahlzeitStatus;
use App\Logic\Essen\Model\Wochentag;
use PHPUnit\Framework\TestCase;
final class MahlzeitFactoryTest extends TestCase
{
private function factory(): MahlzeitFactory
{
return new MahlzeitFactory();
}
private function jetzt(): \DateTimeImmutable
{
return new \DateTimeImmutable('2026-09-18 12:00:00');
}
public function testCreateFromCreateRequest(): void
{
$jetzt = $this->jetzt();
$req = new MahlzeitCreateRequest(
name: 'Pasta',
notiz: 'vegan',
wochentage: [Wochentag::Fr, Wochentag::Sa],
);
$m = $this->factory()->createFromCreateRequest($req, $jetzt);
self::assertSame(0, $m->id);
self::assertSame('Pasta', $m->name);
self::assertSame('vegan', $m->notiz);
self::assertSame([Wochentag::Fr, Wochentag::Sa], $m->wochentage());
self::assertSame(0, $m->sortOrder);
self::assertSame(MahlzeitStatus::Geplant, $m->status);
self::assertSame($jetzt, $m->erstelltAm);
self::assertSame($jetzt, $m->aktualisiertAm);
}
public function testCreateWithLeerenNameWirft(): void
{
$req = new MahlzeitCreateRequest(name: ' ');
$this->expectException(BusinessRuleViolationException::class);
$this->factory()->createFromCreateRequest($req, $this->jetzt());
}
public function testUpdateÄndertNurGelieferteFelder(): void
{
$vorher = new Mahlzeit(
id: 7,
name: 'Altes',
notiz: 'alte Notiz',
wochentage: [Wochentag::Mo],
sortOrder: 3,
status: MahlzeitStatus::Geplant,
erstelltAm: $this->jetzt(),
aktualisiertAm: $this->jetzt(),
gegessenAm: null,
);
$req = new MahlzeitUpdateRequest(name: 'Neues', wochentage: [Wochentag::Do]);
$nachher = $this->factory()->updateFromUpdateRequest($vorher, $req, $this->jetzt());
self::assertSame('Neues', $nachher->name);
self::assertSame([Wochentag::Do], $nachher->wochentage());
self::assertSame('alte Notiz', $nachher->notiz, 'nicht geliefertes Feld bleibt unverändert');
self::assertSame(3, $nachher->sortOrder);
self::assertSame(7, $nachher->id);
}
public function testUpdateMitNullFeldBelässtBestandwert(): void
{
$vorher = new Mahlzeit(7, 'Altes', 'Notiz', [Wochentag::Mo], 3, MahlzeitStatus::Geplant, $this->jetzt(), $this->jetzt(), null);
$nachher = $this->factory()->updateFromUpdateRequest($vorher, new MahlzeitUpdateRequest(), $this->jetzt());
self::assertSame('Altes', $nachher->name);
self::assertSame('Notiz', $nachher->notiz);
self::assertSame([Wochentag::Mo], $nachher->wochentage());
}
public function testUpdateMitLeerenNameWirft(): void
{
$vorher = new Mahlzeit(7, 'Altes', '', [], 0, MahlzeitStatus::Geplant, $this->jetzt(), $this->jetzt(), null);
$this->expectException(BusinessRuleViolationException::class);
$this->factory()->updateFromUpdateRequest($vorher, new MahlzeitUpdateRequest(name: ' '), $this->jetzt());
}
public function testUpdateLeereWochentageListeSetztKeineTage(): void
{
$vorher = new Mahlzeit(7, 'Altes', '', [Wochentag::Mo], 0, MahlzeitStatus::Geplant, $this->jetzt(), $this->jetzt(), null);
$nachher = $this->factory()->updateFromUpdateRequest($vorher, new MahlzeitUpdateRequest(wochentage: []), $this->jetzt());
self::assertSame([], $nachher->wochentage());
}
}
@@ -0,0 +1,157 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Logic\Essen\Model;
use App\Logic\Common\Exception\BusinessRuleViolationException;
use App\Logic\Essen\Model\Mahlzeit;
use App\Logic\Essen\Model\MahlzeitStatus;
use App\Logic\Essen\Model\Wochentag;
use PHPUnit\Framework\TestCase;
final class MahlzeitTest extends TestCase
{
private function jetzt(): \DateTimeImmutable
{
return new \DateTimeImmutable('2026-09-18 12:00:00');
}
private function geplant(): Mahlzeit
{
return Mahlzeit::anlegen(
name: 'Spaghetti Bolognese',
notiz: 'mit mehr Soße',
wochentage: [Wochentag::Mo, Wochentag::Di],
sortOrder: 1,
jetzt: $this->jetzt(),
);
}
public function testAnlegenErzeugtGeplanteMahlzeit(): void
{
$m = $this->geplant();
self::assertSame(0, $m->id);
self::assertSame('Spaghetti Bolognese', $m->name);
self::assertSame('mit mehr Soße', $m->notiz);
self::assertSame([Wochentag::Mo, Wochentag::Di], $m->wochentage());
self::assertSame(1, $m->sortOrder);
self::assertSame(MahlzeitStatus::Geplant, $m->status);
self::assertNull($m->gegessenAm);
self::assertTrue($m->istGeplant());
}
public function testAnlegenTrimmtNameUndNotiz(): void
{
$m = Mahlzeit::anlegen(' Bolognese ', ' Notiz ', [], 0, $this->jetzt());
self::assertSame('Bolognese', $m->name);
self::assertSame('Notiz', $m->notiz);
}
public function testMarkAsGegessenSetztStatusZeitpunktUndAktualisiertAm(): void
{
$m = $this->geplant();
$später = $this->jetzt()->modify('+2 days');
$gegessen = $m->markAsGegessen($später);
self::assertSame(MahlzeitStatus::Gegessen, $gegessen->status);
self::assertSame($später, $gegessen->gegessenAm);
self::assertSame($später, $gegessen->aktualisiertAm);
self::assertTrue($gegessen->istGegessen());
// Ursprung bleibt unverändert (Unveränderlichkeit)
self::assertSame(MahlzeitStatus::Geplant, $m->status);
}
public function testMarkAsGeplantSetztGegessenAmZurück(): void
{
$gegessen = $this->geplant()->markAsGegessen($this->jetzt());
$geplant = $gegessen->markAsGeplant($this->jetzt());
self::assertSame(MahlzeitStatus::Geplant, $geplant->status);
self::assertNull($geplant->gegessenAm);
self::assertTrue($geplant->istGeplant());
}
public function testMarkAsGegessenIstIdempotent(): void
{
$m = $this->geplant();
$einmal = $m->markAsGegessen($this->jetzt());
$zweimal = $einmal->markAsGegessen($this->jetzt());
self::assertSame($einmal->gegessenAm, $zweimal->gegessenAm);
self::assertSame(MahlzeitStatus::Gegessen, $zweimal->status);
}
public function testAktualisierenÄndertInhaltUndBelässtStatus(): void
{
$gegessen = $this->geplant()->markAsGegessen($this->jetzt());
$aktualisiert = $gegessen->aktualisieren(
name: 'Linsen',
notiz: 'scharf',
wochentage: [Wochentag::Mi],
sortOrder: $gegessen->sortOrder,
jetzt: $this->jetzt(),
);
self::assertSame('Linsen', $aktualisiert->name);
self::assertSame('scharf', $aktualisiert->notiz);
self::assertSame([Wochentag::Mi], $aktualisiert->wochentage());
self::assertSame(MahlzeitStatus::Gegessen, $aktualisiert->status);
self::assertNotNull($aktualisiert->gegessenAm);
}
public function testAktualisierenMitLeerenNameWirft(): void
{
$this->expectException(BusinessRuleViolationException::class);
$this->geplant()->aktualisieren(' ', '', [], 1, $this->jetzt());
}
public function testMitSortOrderNegativWirft(): void
{
$this->expectException(BusinessRuleViolationException::class);
$this->geplant()->mitSortOrder(-1);
}
public function testMitSortOrderÄndertNurPosition(): void
{
$m = $this->geplant()->mitSortOrder(5);
self::assertSame(5, $m->sortOrder);
self::assertSame('Spaghetti Bolognese', $m->name);
}
public function testMitIdSetztPersistenzId(): void
{
$m = $this->geplant()->mitId(42);
self::assertSame(42, $m->id);
}
public function testGegessenOhneZeitpunktWirft(): void
{
$this->expectException(BusinessRuleViolationException::class);
new Mahlzeit(1, 'X', '', [], 0, MahlzeitStatus::Gegessen, $this->jetzt(), $this->jetzt(), null);
}
public function testGeplanteMitGegessenAmWirft(): void
{
$this->expectException(BusinessRuleViolationException::class);
new Mahlzeit(1, 'X', '', [], 0, MahlzeitStatus::Geplant, $this->jetzt(), $this->jetzt(), $this->jetzt());
}
public function testUngültigerWochentagWirft(): void
{
$this->expectException(\TypeError::class);
Mahlzeit::anlegen('X', '', ['kein-tag'], 0, $this->jetzt());
}
}
@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Logic\Essen\UseCase;
use App\Logic\Common\ClockInterface;
use App\Logic\Common\EventDispatcherInterface;
use App\Logic\Common\TransactionManagerInterface;
use App\Logic\Essen\Dto\MahlzeitCreateRequest;
use App\Logic\Essen\Dto\MahlzeitCreateResponse;
use App\Logic\Essen\Event\MahlzeitCreatedEvent;
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
use App\Logic\Essen\Mapping\MahlzeitFactory;
use App\Logic\Essen\Model\Mahlzeit;
use App\Logic\Essen\Model\MahlzeitStatus;
use App\Logic\Essen\UseCase\MahlzeitCreateUseCase;
use PHPUnit\Framework\TestCase;
final class MahlzeitCreateUseCaseTest extends TestCase
{
private function jetzt(): \DateTimeImmutable
{
return new \DateTimeImmutable('2026-09-18 12:00:00');
}
private function mahlzeitMitId(int $id, string $name): Mahlzeit
{
$jetzt = $this->jetzt();
return new Mahlzeit($id, $name, '', [], 1, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
}
/** Stub: ruft den übergebenen Closure einfach aus (simuliert die Transaktion). */
private function tx(): TransactionManagerInterface
{
$tx = $this->createStub(TransactionManagerInterface::class);
$tx->method('execute')->willReturnCallback(static fn (\Closure $work): mixed => $work($work));
return $tx;
}
private function clock(): ClockInterface
{
$clock = $this->createStub(ClockInterface::class);
$clock->method('now')->willReturn($this->jetzt());
return $clock;
}
public function testExecutePersistiertUndLiefertResponse(): void
{
$gespeichert = $this->mahlzeitMitId(10, 'Bolognese');
$erfasstesModell = null;
$manager = $this->createMock(MahlzeitManagerInterface::class);
$manager->expects(self::once())
->method('create')
->willReturnCallback(function (Mahlzeit $m) use (&$erfasstesModell, $gespeichert) {
$erfasstesModell = $m;
return $gespeichert;
});
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())
->method('dispatch')
->with(self::equalTo(new MahlzeitCreatedEvent(id: 10, name: 'Bolognese')));
$useCase = new MahlzeitCreateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$response = $useCase->execute(new MahlzeitCreateRequest(name: 'Bolognese', notiz: 'süß'));
self::assertSame('Bolognese', $erfasstesModell->name, 'Übergebenes Modell trägt den Namen aus dem Request');
self::assertSame('süß', $erfasstesModell->notiz);
self::assertInstanceOf(MahlzeitCreateResponse::class, $response);
self::assertSame(10, $response->id);
self::assertSame('Bolognese', $response->mahlzeit->name);
}
public function testExecuteDispatchtEventNachCommit(): void
{
$mahlzeit = $this->mahlzeitMitId(1, 'X');
$manager = $this->createStub(MahlzeitManagerInterface::class);
$manager->method('create')->willReturn($mahlzeit);
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())
->method('dispatch')
->with(self::equalTo(new MahlzeitCreatedEvent(id: 1, name: 'X')));
$useCase = new MahlzeitCreateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$useCase->execute(new MahlzeitCreateRequest(name: 'X'));
}
public function testExecuteDispatchtKeinEventBeiFehler(): void
{
$manager = $this->createStub(MahlzeitManagerInterface::class);
$manager->method('create')->willThrowException(new \RuntimeException('DB down'));
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::never())->method('dispatch');
$useCase = new MahlzeitCreateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$this->expectException(\RuntimeException::class);
try {
$useCase->execute(new MahlzeitCreateRequest(name: 'X'));
} finally {
// Erwartete Exception oben.
}
}
}
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Logic\Essen\UseCase;
use App\Logic\Common\EventDispatcherInterface;
use App\Logic\Common\TransactionManagerInterface;
use App\Logic\Essen\Event\MahlzeitDeletedEvent;
use App\Logic\Essen\Exception\MahlzeitNotFoundException;
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
use App\Logic\Essen\Model\Mahlzeit;
use App\Logic\Essen\Model\MahlzeitStatus;
use App\Logic\Essen\UseCase\MahlzeitDeleteUseCase;
use PHPUnit\Framework\TestCase;
final class MahlzeitDeleteUseCaseTest extends TestCase
{
private function jetzt(): \DateTimeImmutable
{
return new \DateTimeImmutable('2026-09-18 12:00:00');
}
/** Stub: ruft den übergebenen Closure einfach aus (simuliert die Transaktion). */
private function tx(): TransactionManagerInterface
{
$tx = $this->createStub(TransactionManagerInterface::class);
$tx->method('execute')->willReturnCallback(static fn (\Closure $work): mixed => $work($work));
return $tx;
}
public function testExecuteLöschtUndDispatchtEvent(): void
{
$jetzt = $this->jetzt();
$mahlzeit = new Mahlzeit(7, 'Bolognese', '', [], 1, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
$manager = $this->createMock(MahlzeitManagerInterface::class);
$manager->expects(self::once())->method('find')->with(7)->willReturn($mahlzeit);
$manager->expects(self::once())->method('delete')->with(7);
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())
->method('dispatch')
->with(self::equalTo(new MahlzeitDeletedEvent(id: 7, name: 'Bolognese')));
$useCase = new MahlzeitDeleteUseCase($this->tx(), $manager, $events);
$useCase->execute(7);
}
public function testExecuteDispatchtKeinEventBeiFehler(): void
{
$manager = $this->createStub(MahlzeitManagerInterface::class);
$manager->method('find')->willThrowException(new MahlzeitNotFoundException(99));
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::never())->method('dispatch');
$useCase = new MahlzeitDeleteUseCase($this->tx(), $manager, $events);
$this->expectException(MahlzeitNotFoundException::class);
try {
$useCase->execute(99);
} finally {
// Erwartete Exception oben.
}
}
}
@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace App\Tests\Unit\Logic\Essen\UseCase;
use App\Logic\Common\ClockInterface;
use App\Logic\Common\EventDispatcherInterface;
use App\Logic\Common\TransactionManagerInterface;
use App\Logic\Essen\Dto\MahlzeitUpdateRequest;
use App\Logic\Essen\Dto\MahlzeitUpdateResponse;
use App\Logic\Essen\Event\MahlzeitUpdatedEvent;
use App\Logic\Essen\Exception\MahlzeitNotFoundException;
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
use App\Logic\Essen\Mapping\MahlzeitFactory;
use App\Logic\Essen\Model\Mahlzeit;
use App\Logic\Essen\Model\MahlzeitStatus;
use App\Logic\Essen\Model\Wochentag;
use App\Logic\Essen\UseCase\MahlzeitUpdateUseCase;
use PHPUnit\Framework\TestCase;
final class MahlzeitUpdateUseCaseTest extends TestCase
{
private function jetzt(): \DateTimeImmutable
{
return new \DateTimeImmutable('2026-09-18 12:00:00');
}
/** Stub: ruft den übergebenen Closure einfach aus (simuliert die Transaktion). */
private function tx(): TransactionManagerInterface
{
$tx = $this->createStub(TransactionManagerInterface::class);
$tx->method('execute')->willReturnCallback(static fn (\Closure $work): mixed => $work($work));
return $tx;
}
private function clock(): ClockInterface
{
$clock = $this->createStub(ClockInterface::class);
$clock->method('now')->willReturn($this->jetzt());
return $clock;
}
private function bestehend(): Mahlzeit
{
$jetzt = $this->jetzt();
return new Mahlzeit(5, 'Altes', 'alte Notiz', [Wochentag::Mo, Wochentag::Di], 3, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
}
public function testExecuteAktualisiertNameNotizUndWochentage(): void
{
$erfasstesModell = null;
$manager = $this->createMock(MahlzeitManagerInterface::class);
$manager->expects(self::once())->method('find')->with(5)->willReturn($this->bestehend());
$manager->expects(self::once())
->method('update')
->willReturnCallback(function (Mahlzeit $m) use (&$erfasstesModell) {
$erfasstesModell = $m;
return $m;
});
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())
->method('dispatch')
->with(self::equalTo(new MahlzeitUpdatedEvent(id: 5, name: 'Neues')));
$useCase = new MahlzeitUpdateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$response = $useCase->execute(5, new MahlzeitUpdateRequest(name: 'Neues', notiz: 'neu', wochentage: [Wochentag::Fr]));
self::assertSame('Neues', $erfasstesModell->name);
self::assertSame('neu', $erfasstesModell->notiz);
self::assertSame([Wochentag::Fr], $erfasstesModell->wochentage());
self::assertInstanceOf(MahlzeitUpdateResponse::class, $response);
self::assertSame(5, $response->id);
self::assertSame('Neues', $response->mahlzeit->name);
}
public function testExecuteLässtUndefinierteFelderUnerändert(): void
{
$alt = $this->bestehend();
$erfasstesModell = null;
$manager = $this->createMock(MahlzeitManagerInterface::class);
$manager->expects(self::once())->method('find')->with(5)->willReturn($alt);
$manager->expects(self::once())
->method('update')
->willReturnCallback(function (Mahlzeit $m) use (&$erfasstesModell) {
$erfasstesModell = $m;
return $m;
});
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())->method('dispatch');
$useCase = new MahlzeitUpdateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$useCase->execute(5, new MahlzeitUpdateRequest(name: null, notiz: null, wochentage: null));
self::assertSame('Altes', $erfasstesModell->name, 'nicht geliefertes Feld bleibt unverändert');
self::assertSame('alte Notiz', $erfasstesModell->notiz);
self::assertSame([Wochentag::Mo, Wochentag::Di], $erfasstesModell->wochentage());
}
public function testExecuteLeertWochentageMitEmptyArray(): void
{
$erfasstesModell = null;
$manager = $this->createMock(MahlzeitManagerInterface::class);
$manager->expects(self::once())->method('find')->with(5)->willReturn($this->bestehend());
$manager->expects(self::once())
->method('update')
->willReturnCallback(function (Mahlzeit $m) use (&$erfasstesModell) {
$erfasstesModell = $m;
return $m;
});
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::once())->method('dispatch');
$useCase = new MahlzeitUpdateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$useCase->execute(5, new MahlzeitUpdateRequest(wochentage: []));
self::assertSame([], $erfasstesModell->wochentage());
}
public function testExecuteWirft404WennNichtVorhanden(): void
{
$manager = $this->createStub(MahlzeitManagerInterface::class);
$manager->method('find')->willThrowException(new MahlzeitNotFoundException(99));
$events = $this->createMock(EventDispatcherInterface::class);
$events->expects(self::never())->method('dispatch');
$useCase = new MahlzeitUpdateUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
$this->expectException(MahlzeitNotFoundException::class);
try {
$useCase->execute(99, new MahlzeitUpdateRequest(name: 'X'));
} finally {
// Erwartete Exception oben.
}
}
}