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).
147 lines
5.7 KiB
PHP
147 lines
5.7 KiB
PHP
<?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.
|
|
}
|
|
}
|
|
}
|