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