M2: Status & Wochentage – Schritt 1 (Logik): CheckOff/Uncheck UseCases + Status-Event + Tests
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Logic\Essen\Event;
|
||||||
|
|
||||||
|
use App\Logic\Essen\Model\MahlzeitStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Domain-Event: der Status einer Mahlzeit wurde geändert (nach erfolgreichem Commit).
|
||||||
|
*
|
||||||
|
* Trägt den neuen Status + den (ggf. leeren) gegessenAm-Zeitpunkt, damit ein
|
||||||
|
* Subscriber (z. B. Archiv, Statistik) ohne erneuten DB-Zugriff reagieren kann.
|
||||||
|
*/
|
||||||
|
final readonly class MahlzeitStatusChangedEvent
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $id,
|
||||||
|
public string $name,
|
||||||
|
public MahlzeitStatus $status,
|
||||||
|
public ?\DateTimeImmutable $gegessenAm,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Logic\Essen\UseCase;
|
||||||
|
|
||||||
|
use App\Logic\Common\ClockInterface;
|
||||||
|
use App\Logic\Common\EventDispatcherInterface;
|
||||||
|
use App\Logic\Common\TransactionManagerInterface;
|
||||||
|
use App\Logic\Essen\Dto\MahlzeitResponse;
|
||||||
|
use App\Logic\Essen\Event\MahlzeitStatusChangedEvent;
|
||||||
|
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use-Case: eine Mahlzeit abhaken (geplant → gegessen).
|
||||||
|
*
|
||||||
|
* Idempotent: Eine bereits gegessene Mahlzeit bleibt unverändert (derselbe
|
||||||
|
* gegessenAm-Zeitpunkt bleibt erhalten). Wirft `MahlzeitNotFoundException`
|
||||||
|
* (→ 404), wenn die ID unbekannt ist.
|
||||||
|
*/
|
||||||
|
final readonly class MahlzeitCheckOffUseCase
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private TransactionManagerInterface $tx,
|
||||||
|
private MahlzeitManagerInterface $mahlzeiten,
|
||||||
|
private EventDispatcherInterface $events,
|
||||||
|
private ClockInterface $clock,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(int $id): MahlzeitResponse
|
||||||
|
{
|
||||||
|
$jetzt = $this->clock->now();
|
||||||
|
$bestehend = $this->mahlzeiten->find($id);
|
||||||
|
$markiert = $bestehend->markAsGegessen($jetzt);
|
||||||
|
|
||||||
|
$gespeichert = $this->tx->execute(fn () => $this->mahlzeiten->update($markiert));
|
||||||
|
|
||||||
|
$this->events->dispatch(new MahlzeitStatusChangedEvent(
|
||||||
|
id: $id,
|
||||||
|
name: $gespeichert->name,
|
||||||
|
status: $gespeichert->status,
|
||||||
|
gegessenAm: $gespeichert->gegessenAm,
|
||||||
|
));
|
||||||
|
|
||||||
|
return MahlzeitResponse::fromModel($gespeichert);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Logic\Essen\UseCase;
|
||||||
|
|
||||||
|
use App\Logic\Common\ClockInterface;
|
||||||
|
use App\Logic\Common\EventDispatcherInterface;
|
||||||
|
use App\Logic\Common\TransactionManagerInterface;
|
||||||
|
use App\Logic\Essen\Dto\MahlzeitResponse;
|
||||||
|
use App\Logic\Essen\Event\MahlzeitStatusChangedEvent;
|
||||||
|
use App\Logic\Essen\Manager\MahlzeitManagerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use-Case: eine Mahlzeit wieder aufheben (gegessen → geplant).
|
||||||
|
*
|
||||||
|
* Setzt `gegessenAm` zurück. Idempotent: Eine bereits geplante Mahlzeit bleibt
|
||||||
|
* unverändert. Wirft `MahlzeitNotFoundException` (→ 404), wenn die ID unbekannt ist.
|
||||||
|
*/
|
||||||
|
final readonly class MahlzeitUncheckUseCase
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private TransactionManagerInterface $tx,
|
||||||
|
private MahlzeitManagerInterface $mahlzeiten,
|
||||||
|
private EventDispatcherInterface $events,
|
||||||
|
private ClockInterface $clock,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(int $id): MahlzeitResponse
|
||||||
|
{
|
||||||
|
$jetzt = $this->clock->now();
|
||||||
|
$bestehend = $this->mahlzeiten->find($id);
|
||||||
|
$markiert = $bestehend->markAsGeplant($jetzt);
|
||||||
|
|
||||||
|
$gespeichert = $this->tx->execute(fn () => $this->mahlzeiten->update($markiert));
|
||||||
|
|
||||||
|
$this->events->dispatch(new MahlzeitStatusChangedEvent(
|
||||||
|
id: $id,
|
||||||
|
name: $gespeichert->name,
|
||||||
|
status: $gespeichert->status,
|
||||||
|
gegessenAm: $gespeichert->gegessenAm,
|
||||||
|
));
|
||||||
|
|
||||||
|
return MahlzeitResponse::fromModel($gespeichert);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -92,4 +92,34 @@ final class MahlzeitProcessorTest extends DatabaseTestCase
|
|||||||
|
|
||||||
$this->processor()->delete($m);
|
$this->processor()->delete($m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSavePersistiertAbhakenAlsGegessen(): void
|
||||||
|
{
|
||||||
|
$jetzt = $this->jetzt();
|
||||||
|
$created = $this->processor()->create(Mahlzeit::anlegen('Bolognese', '', [Wochentag::Mo], 1, $jetzt));
|
||||||
|
|
||||||
|
$gegessen = $created->markAsGegessen($jetzt);
|
||||||
|
$this->processor()->save($gegessen);
|
||||||
|
|
||||||
|
$neu = (new MahlzeitProvider($this->em, new MahlzeitMapper()))->find($created->id);
|
||||||
|
self::assertNotNull($neu);
|
||||||
|
self::assertSame(MahlzeitStatus::Gegessen, $neu->status);
|
||||||
|
self::assertSame($jetzt, $neu->gegessenAm);
|
||||||
|
self::assertTrue($neu->istGegessen());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSavePersistiertAufhebenAlsGeplant(): void
|
||||||
|
{
|
||||||
|
$jetzt = $this->jetzt();
|
||||||
|
$created = $this->processor()->create(Mahlzeit::anlegen('Bolognese', '', [Wochentag::Mo], 1, $jetzt)->markAsGegessen($jetzt));
|
||||||
|
|
||||||
|
$geplant = $created->markAsGeplant($jetzt);
|
||||||
|
$this->processor()->save($geplant);
|
||||||
|
|
||||||
|
$neu = (new MahlzeitProvider($this->em, new MahlzeitMapper()))->find($created->id);
|
||||||
|
self::assertNotNull($neu);
|
||||||
|
self::assertSame(MahlzeitStatus::Geplant, $neu->status);
|
||||||
|
self::assertNull($neu->gegessenAm);
|
||||||
|
self::assertTrue($neu->istGeplant());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,120 @@
|
|||||||
|
<?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\MahlzeitResponse;
|
||||||
|
use App\Logic\Essen\Event\MahlzeitStatusChangedEvent;
|
||||||
|
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\Model\Wochentag;
|
||||||
|
use App\Logic\Essen\UseCase\MahlzeitCheckOffUseCase;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class MahlzeitCheckOffUseCaseTest 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 geplant(): Mahlzeit
|
||||||
|
{
|
||||||
|
$jetzt = $this->jetzt();
|
||||||
|
return new Mahlzeit(7, 'Bolognese', '', [Wochentag::Mo], 1, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecuteMarkiertAlsGegessenUndDispatchtEvent(): void
|
||||||
|
{
|
||||||
|
$erfasstesModell = null;
|
||||||
|
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||||
|
$manager->expects(self::once())->method('find')->with(7)->willReturn($this->geplant());
|
||||||
|
$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 MahlzeitStatusChangedEvent(
|
||||||
|
id: 7,
|
||||||
|
name: 'Bolognese',
|
||||||
|
status: MahlzeitStatus::Gegessen,
|
||||||
|
gegessenAm: $this->jetzt(),
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
|
$useCase = new MahlzeitCheckOffUseCase($this->tx(), $manager, $events, $this->clock());
|
||||||
|
|
||||||
|
$response = $useCase->execute(7);
|
||||||
|
|
||||||
|
self::assertSame(MahlzeitStatus::Gegessen, $erfasstesModell->status);
|
||||||
|
self::assertNotNull($erfasstesModell->gegessenAm);
|
||||||
|
self::assertEquals($this->jetzt(), $erfasstesModell->aktualisiertAm);
|
||||||
|
self::assertInstanceOf(MahlzeitResponse::class, $response);
|
||||||
|
self::assertSame(7, $response->id);
|
||||||
|
self::assertSame(MahlzeitStatus::Gegessen, $response->status);
|
||||||
|
self::assertNotNull($response->gegessenAm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecuteIstIdempotentFuerBereitsGegessene(): void
|
||||||
|
{
|
||||||
|
$jetzt = $this->jetzt();
|
||||||
|
$bereitsGegessen = new Mahlzeit(7, 'Bolognese', '', [Wochentag::Mo], 1, MahlzeitStatus::Gegessen, $jetzt, $jetzt, $jetzt);
|
||||||
|
|
||||||
|
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||||
|
$manager->expects(self::once())->method('find')->with(7)->willReturn($bereitsGegessen);
|
||||||
|
$manager->expects(self::once())->method('update')->willReturnArgument(0);
|
||||||
|
|
||||||
|
$events = $this->createMock(EventDispatcherInterface::class);
|
||||||
|
$events->expects(self::once())->method('dispatch');
|
||||||
|
|
||||||
|
$useCase = new MahlzeitCheckOffUseCase($this->tx(), $manager, $events, $this->clock());
|
||||||
|
|
||||||
|
$response = $useCase->execute(7);
|
||||||
|
|
||||||
|
// Ursprünglicher gegessenAm-Zeitpunkt bleibt erhalten (markAsGegessen ist idempotent).
|
||||||
|
self::assertSame($jetzt, $response->gegessenAm);
|
||||||
|
self::assertSame(MahlzeitStatus::Gegessen, $response->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecuteWirft404UndDispatchtKeinEvent(): 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 MahlzeitCheckOffUseCase($this->tx(), $manager, $events, $this->clock());
|
||||||
|
|
||||||
|
$this->expectException(MahlzeitNotFoundException::class);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$useCase->execute(99);
|
||||||
|
} finally {
|
||||||
|
// Erwartete Exception oben.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
<?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\MahlzeitResponse;
|
||||||
|
use App\Logic\Essen\Event\MahlzeitStatusChangedEvent;
|
||||||
|
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\Model\Wochentag;
|
||||||
|
use App\Logic\Essen\UseCase\MahlzeitUncheckUseCase;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class MahlzeitUncheckUseCaseTest 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 gegessen(): Mahlzeit
|
||||||
|
{
|
||||||
|
$jetzt = $this->jetzt();
|
||||||
|
return new Mahlzeit(7, 'Bolognese', '', [Wochentag::Mo], 1, MahlzeitStatus::Gegessen, $jetzt, $jetzt, $jetzt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecuteSetztZurueckAufGeplantUndDispatchtEvent(): void
|
||||||
|
{
|
||||||
|
$erfasstesModell = null;
|
||||||
|
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||||
|
$manager->expects(self::once())->method('find')->with(7)->willReturn($this->gegessen());
|
||||||
|
$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 MahlzeitStatusChangedEvent(
|
||||||
|
id: 7,
|
||||||
|
name: 'Bolognese',
|
||||||
|
status: MahlzeitStatus::Geplant,
|
||||||
|
gegessenAm: null,
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
|
$useCase = new MahlzeitUncheckUseCase($this->tx(), $manager, $events, $this->clock());
|
||||||
|
|
||||||
|
$response = $useCase->execute(7);
|
||||||
|
|
||||||
|
self::assertSame(MahlzeitStatus::Geplant, $erfasstesModell->status);
|
||||||
|
self::assertNull($erfasstesModell->gegessenAm, 'gegessenAm wird zurückgesetzt');
|
||||||
|
self::assertInstanceOf(MahlzeitResponse::class, $response);
|
||||||
|
self::assertSame(7, $response->id);
|
||||||
|
self::assertSame(MahlzeitStatus::Geplant, $response->status);
|
||||||
|
self::assertNull($response->gegessenAm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecuteIstIdempotentFuerBereitsGeplante(): void
|
||||||
|
{
|
||||||
|
$jetzt = $this->jetzt();
|
||||||
|
$bereitsGeplant = new Mahlzeit(7, 'Bolognese', '', [Wochentag::Mo], 1, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
|
||||||
|
|
||||||
|
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||||
|
$manager->expects(self::once())->method('find')->with(7)->willReturn($bereitsGeplant);
|
||||||
|
$manager->expects(self::once())->method('update')->willReturnArgument(0);
|
||||||
|
|
||||||
|
$events = $this->createMock(EventDispatcherInterface::class);
|
||||||
|
$events->expects(self::once())->method('dispatch');
|
||||||
|
|
||||||
|
$useCase = new MahlzeitUncheckUseCase($this->tx(), $manager, $events, $this->clock());
|
||||||
|
|
||||||
|
$response = $useCase->execute(7);
|
||||||
|
|
||||||
|
self::assertSame(MahlzeitStatus::Geplant, $response->status);
|
||||||
|
self::assertNull($response->gegessenAm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecuteWirft404UndDispatchtKeinEvent(): 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 MahlzeitUncheckUseCase($this->tx(), $manager, $events, $this->clock());
|
||||||
|
|
||||||
|
$this->expectException(MahlzeitNotFoundException::class);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$useCase->execute(99);
|
||||||
|
} finally {
|
||||||
|
// Erwartete Exception oben.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user