M2: Status & Wochentage – Schritt 1 (Logik): CheckOff/Uncheck UseCases + Status-Event + Tests

This commit is contained in:
2026-09-19 10:54:52 +02:00
parent 160b89f5a1
commit 3519ff08f2
6 changed files with 387 additions and 0 deletions
@@ -92,4 +92,34 @@ final class MahlzeitProcessorTest extends DatabaseTestCase
$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.
}
}
}