M3: Reorder + Inspiration – Schritt 1 (Logik): Reorder-UseCase, Inspiration-Query (ADR-006), Adopt-UseCase + Unit-Tests
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?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\Event\MahlzeitAdoptedEvent;
|
||||
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\MahlzeitAdoptUseCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MahlzeitAdoptUseCaseTest extends TestCase
|
||||
{
|
||||
private function tx(): TransactionManagerInterface
|
||||
{
|
||||
$tx = $this->createStub(TransactionManagerInterface::class);
|
||||
$tx->method('execute')->willReturnCallback(static fn (\Closure $work): mixed => $work());
|
||||
return $tx;
|
||||
}
|
||||
|
||||
private function clock(): ClockInterface
|
||||
{
|
||||
$clock = $this->createStub(ClockInterface::class);
|
||||
$clock->method('now')->willReturn(new \DateTimeImmutable('2026-09-18 12:00:00'));
|
||||
return $clock;
|
||||
}
|
||||
|
||||
public function testExecuteKopiertQuelleAlsNeuenGeplantEintrag(): void
|
||||
{
|
||||
$jetzt = new \DateTimeImmutable('2026-09-18 12:00:00');
|
||||
$quelle = new Mahlzeit(7, 'Bolognese', 'mit Parmesan', [Wochentag::Mo], 3, MahlzeitStatus::Gegessen, $jetzt, $jetzt, $jetzt);
|
||||
|
||||
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||
$manager->expects(self::once())->method('find')->with(7)->willReturn($quelle);
|
||||
$manager->expects(self::once())->method('create')->willReturnCallback(function (Mahlzeit $m) {
|
||||
// Neue, geplante Kopie: gleiche Inhalte, aber ohne gegessenAm und ohne ID.
|
||||
self::assertSame('Bolognese', $m->name);
|
||||
self::assertSame('mit Parmesan', $m->notiz);
|
||||
self::assertSame([Wochentag::Mo], $m->wochentage);
|
||||
self::assertSame(MahlzeitStatus::Geplant, $m->status);
|
||||
self::assertNull($m->gegessenAm);
|
||||
self::assertSame(0, $m->sortOrder);
|
||||
self::assertSame(0, $m->id);
|
||||
// Persistenz vergibt die neue ID.
|
||||
return $m->mitId(42);
|
||||
});
|
||||
|
||||
$events = $this->createMock(EventDispatcherInterface::class);
|
||||
$events->expects(self::once())->method('dispatch')->with(
|
||||
self::equalTo(new MahlzeitAdoptedEvent(id: 42, name: 'Bolognese', quellId: 7)),
|
||||
);
|
||||
|
||||
$useCase = new MahlzeitAdoptUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
|
||||
|
||||
$response = $useCase->execute(7);
|
||||
|
||||
self::assertSame(42, $response->id);
|
||||
self::assertSame('Bolognese', $response->name);
|
||||
self::assertSame(MahlzeitStatus::Geplant, $response->status);
|
||||
self::assertNull($response->gegessenAm);
|
||||
}
|
||||
|
||||
public function testExecuteUnbekannteQuelleWirft404UndLegtNichtsAn(): void
|
||||
{
|
||||
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||
$manager->method('find')->willThrowException(new MahlzeitNotFoundException(99));
|
||||
$manager->expects(self::never())->method('create');
|
||||
|
||||
$events = $this->createMock(EventDispatcherInterface::class);
|
||||
$events->expects(self::never())->method('dispatch');
|
||||
|
||||
$useCase = new MahlzeitAdoptUseCase($this->tx(), $manager, new MahlzeitFactory(), $events, $this->clock());
|
||||
|
||||
$this->expectException(MahlzeitNotFoundException::class);
|
||||
|
||||
try {
|
||||
$useCase->execute(99);
|
||||
} finally {
|
||||
// Erwartete Exception oben.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?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\Dto\MahlzeitReorderRequest;
|
||||
use App\Logic\Essen\Event\MahlzeitReorderedEvent;
|
||||
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\MahlzeitReorderUseCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MahlzeitReorderUseCaseTest extends TestCase
|
||||
{
|
||||
private function tx(): TransactionManagerInterface
|
||||
{
|
||||
$tx = $this->createStub(TransactionManagerInterface::class);
|
||||
$tx->method('execute')->willReturnCallback(static fn (\Closure $work): mixed => $work());
|
||||
return $tx;
|
||||
}
|
||||
|
||||
private function mahlzeit(int $id, int $sortOrder): Mahlzeit
|
||||
{
|
||||
$jetzt = new \DateTimeImmutable('2026-09-18 12:00:00');
|
||||
return new Mahlzeit($id, 'Gericht' . $id, '', [], $sortOrder, MahlzeitStatus::Geplant, $jetzt, $jetzt, null);
|
||||
}
|
||||
|
||||
public function testExecuteRenommertSortOrderUndDispatchtEvent(): void
|
||||
{
|
||||
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||
$manager->method('find')->willReturnMap([
|
||||
[2, $this->mahlzeit(2, 2)],
|
||||
[1, $this->mahlzeit(1, 1)],
|
||||
[3, $this->mahlzeit(3, 3)],
|
||||
]);
|
||||
|
||||
$gespeicherte = [];
|
||||
$manager->expects(self::exactly(2))->method('update')->willReturnCallback(function (Mahlzeit $m) use (&$gespeicherte) {
|
||||
$gespeicherte[$m->id] = $m->sortOrder;
|
||||
return $m;
|
||||
});
|
||||
|
||||
$events = $this->createMock(EventDispatcherInterface::class);
|
||||
$events->expects(self::once())->method('dispatch')->with(
|
||||
self::equalTo(new MahlzeitReorderedEvent([2, 1, 3])),
|
||||
);
|
||||
|
||||
$useCase = new MahlzeitReorderUseCase($this->tx(), $manager, $events);
|
||||
$useCase->execute(new MahlzeitReorderRequest([2, 1, 3]));
|
||||
|
||||
// id 2 → Position 1, id 1 → Position 2; id 3 (Position 3) bleibt unverändert.
|
||||
self::assertSame([2 => 1, 1 => 2], $gespeicherte);
|
||||
}
|
||||
|
||||
public function testExecuteSpruengtBereitsKorrektePosition(): void
|
||||
{
|
||||
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||
$manager->method('find')->willReturnMap([
|
||||
[1, $this->mahlzeit(1, 1)],
|
||||
[2, $this->mahlzeit(2, 2)],
|
||||
]);
|
||||
$manager->expects(self::never())->method('update');
|
||||
|
||||
$events = $this->createMock(EventDispatcherInterface::class);
|
||||
$events->expects(self::once())->method('dispatch');
|
||||
|
||||
$useCase = new MahlzeitReorderUseCase($this->tx(), $manager, $events);
|
||||
$useCase->execute(new MahlzeitReorderRequest([1, 2]));
|
||||
}
|
||||
|
||||
public function testExecuteLeereListeIstNoOp(): void
|
||||
{
|
||||
$manager = $this->createMock(MahlzeitManagerInterface::class);
|
||||
$manager->expects(self::never())->method('find');
|
||||
$manager->expects(self::never())->method('update');
|
||||
|
||||
$events = $this->createMock(EventDispatcherInterface::class);
|
||||
$events->expects(self::never())->method('dispatch');
|
||||
|
||||
$useCase = new MahlzeitReorderUseCase($this->tx(), $manager, $events);
|
||||
$useCase->execute(new MahlzeitReorderRequest([]));
|
||||
}
|
||||
|
||||
public function testExecuteUnbekannteIdWirft404(): 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 MahlzeitReorderUseCase($this->tx(), $manager, $events);
|
||||
|
||||
$this->expectException(MahlzeitNotFoundException::class);
|
||||
|
||||
try {
|
||||
$useCase->execute(new MahlzeitReorderRequest([99]));
|
||||
} finally {
|
||||
// Erwartete Exception oben.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user