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,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.
}
}
}