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:
@@ -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.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user