test level based scheduling

This commit is contained in:
Yisroel Baum 2026-08-11 22:54:55 +03:00
parent 99e1ba051c
commit a64df64b76
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
7 changed files with 415 additions and 94 deletions

View file

@ -22,8 +22,7 @@ class FakeElementRepository implements ElementRepository
$element = new Element( $element = new Element(
id: $id, id: $id,
name: $dto->name, name: $dto->name,
kind: $dto->kind, level: $dto->level,
set: $dto->set,
parentElement: $dto->parentElement, parentElement: $dto->parentElement,
position: $this->nextPosition($dto), position: $this->nextPosition($dto),
); );
@ -88,14 +87,32 @@ class FakeElementRepository implements ElementRepository
{ {
$parentElement = $dto->parentElement; $parentElement = $dto->parentElement;
if ($parentElement === null) { if ($parentElement === null) {
if ($dto->level->getDepth() !== 0) {
throw new DomainException(
'root element must use the first set level',
);
}
return; return;
} }
if ($parentElement->getSet()->getId() !== $dto->set->getId()) { if (
$parentElement->getSet()->getId()
!== $dto->level->getSet()->getId()
) {
throw new DomainException( throw new DomainException(
'parent element must belong to the same set', 'parent element must belong to the same set',
); );
} }
if (
$dto->level->getDepth()
!== $parentElement->getLevel()->getDepth() + 1
) {
throw new DomainException(
'child element must use the next set level',
);
}
} }
private function nextPosition(CreateElementDto $dto): int private function nextPosition(CreateElementDto $dto): int
@ -104,7 +121,10 @@ class FakeElementRepository implements ElementRepository
$maximumPosition = 0; $maximumPosition = 0;
foreach ($this->elements as $element) { foreach ($this->elements as $element) {
if ($element->getSet()->getId() !== $dto->set->getId()) { if (
$element->getSet()->getId()
!== $dto->level->getSet()->getId()
) {
continue; continue;
} }
@ -150,8 +170,7 @@ class FakeElementRepository implements ElementRepository
return new Element( return new Element(
id: $element->getId(), id: $element->getId(),
name: $element->getName(), name: $element->getName(),
kind: $element->getKind(), level: $element->getLevel(),
set: $element->getSet(),
parentElement: $parentElement === null parentElement: $parentElement === null
? null ? null
: $this->copy($parentElement), : $this->copy($parentElement),

View file

@ -0,0 +1,76 @@
<?php
namespace Tests\Fakes;
use App\Set\CreateSetLevelDto;
use App\Set\Set;
use App\Set\SetLevel;
use App\Set\SetLevelRepository;
use DomainException;
class FakeSetLevelRepository implements SetLevelRepository
{
/**
* @var array<int, SetLevel>
*/
private array $levels = [];
public function create(CreateSetLevelDto $dto): SetLevel
{
foreach ($this->levels as $level) {
if (
$level->getSet()->getId() === $dto->set->getId()
&& $level->getKind() === $dto->kind
) {
throw new DomainException(
'level kind must be unique within set',
);
}
}
$id = count($this->levels) + 1;
$level = new SetLevel(
id: $id,
set: $dto->set,
kind: $dto->kind,
depth: count($this->findBySet($dto->set)),
);
$this->levels[$id] = $level;
return $this->copy($level);
}
public function find(int $id): ?SetLevel
{
$level = $this->levels[$id] ?? null;
return $level === null ? null : $this->copy($level);
}
public function findBySet(Set $set): array
{
$levels = array_filter(
$this->levels,
function (SetLevel $level) use ($set): bool {
return $level->getSet()->getId() === $set->getId();
},
);
usort($levels, function (SetLevel $first, SetLevel $second): int {
return $first->getDepth() <=> $second->getDepth();
});
return array_map(function (SetLevel $level): SetLevel {
return $this->copy($level);
}, $levels);
}
private function copy(SetLevel $level): SetLevel
{
return new SetLevel(
id: $level->getId(),
set: $level->getSet(),
kind: $level->getKind(),
depth: $level->getDepth(),
);
}
}

View file

@ -6,6 +6,7 @@ use App\Auth\PasswordHasher;
use App\Element\Element; use App\Element\Element;
use App\Element\ElementRepository; use App\Element\ElementRepository;
use App\Set\SetRepository; use App\Set\SetRepository;
use App\Set\SetLevelRepository;
use App\Shared\ValueObject\EmailAddress; use App\Shared\ValueObject\EmailAddress;
use App\User\UserRepository; use App\User\UserRepository;
use Database\Seeders\UserSeeder; use Database\Seeders\UserSeeder;
@ -70,6 +71,12 @@ class DatabaseSeederTest extends TestCase
$sets = app(SetRepository::class)->all(); $sets = app(SetRepository::class)->all();
$elementRepository = app(ElementRepository::class); $elementRepository = app(ElementRepository::class);
$setLevelRepository = app(SetLevelRepository::class);
$expectedLevels = [
'Bible' => ['book', 'portion', 'chapter'],
'Course' => ['module', 'lesson'],
'Fitness Program' => ['phase', 'workout', 'exercise'],
];
$expectedElements = [ $expectedElements = [
'Bible' => [ 'Bible' => [
'Chapter 1:chapter:Creation', 'Chapter 1:chapter:Creation',
@ -100,9 +107,16 @@ class DatabaseSeederTest extends TestCase
], ],
]; ];
$this->assertDatabaseCount('set_levels', 8);
$this->assertDatabaseCount('elements', 21); $this->assertDatabaseCount('elements', 21);
foreach ($sets as $set) { foreach ($sets as $set) {
$this->assertSame(
$expectedLevels[$set->getName()],
array_map(function ($level): string {
return $level->getKind();
}, $setLevelRepository->findBySet($set)),
);
$signatures = array_map(function (Element $element): string { $signatures = array_map(function (Element $element): string {
$parentName = $element->getParentElement()?->getName() $parentName = $element->getParentElement()?->getName()
?? 'root'; ?? 'root';

View file

@ -9,7 +9,11 @@ use App\Element\ElementModel;
use App\Element\ElementRepository; use App\Element\ElementRepository;
use App\Http\Middleware\AuthMiddleware; use App\Http\Middleware\AuthMiddleware;
use App\Set\CreateSetDto; use App\Set\CreateSetDto;
use App\Set\CreateSetLevelDto;
use App\Set\Set; use App\Set\Set;
use App\Set\SetLevel;
use App\Set\SetLevelModel;
use App\Set\SetLevelRepository;
use App\Set\SetModel; use App\Set\SetModel;
use App\Set\SetRepository; use App\Set\SetRepository;
use App\Shared\ValueObject\EmailAddress; use App\Shared\ValueObject\EmailAddress;
@ -32,42 +36,45 @@ class ScheduleEndpointTest extends TestCase
$user = $this->createUser('reader@example.com'); $user = $this->createUser('reader@example.com');
$creator = $this->createUser('creator@example.com'); $creator = $this->createUser('creator@example.com');
$set = $this->createSet($creator, 'Bible'); $set = $this->createSet($creator, 'Bible');
$bookLevel = $this->createLevel($set, 'book');
$portionLevel = $this->createLevel($set, 'portion');
$chapterLevel = $this->createLevel($set, 'chapter');
$repository = app(ElementRepository::class); $repository = app(ElementRepository::class);
$genesis = $repository->create(new CreateElementDto( $genesis = $repository->create(new CreateElementDto(
set: $set,
name: 'Genesis', name: 'Genesis',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$creation = $repository->create(new CreateElementDto( $creation = $repository->create(new CreateElementDto(
set: $set,
name: 'Creation', name: 'Creation',
kind: 'portion', level: $portionLevel,
parentElement: $genesis, parentElement: $genesis,
)); ));
$chapterOne = $repository->create(new CreateElementDto( $repository->create(new CreateElementDto(
set: $set,
name: 'Chapter 1', name: 'Chapter 1',
kind: 'chapter', level: $chapterLevel,
parentElement: $creation, parentElement: $creation,
)); ));
$exodus = $repository->create(new CreateElementDto( $exodus = $repository->create(new CreateElementDto(
set: $set,
name: 'Exodus', name: 'Exodus',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$chapterTwo = $repository->create(new CreateElementDto( $shemot = $repository->create(new CreateElementDto(
set: $set, name: 'Shemot',
name: 'Chapter 1', level: $portionLevel,
kind: 'chapter',
parentElement: $exodus, parentElement: $exodus,
)); ));
$repository->create(new CreateElementDto(
name: 'Chapter 1',
level: $chapterLevel,
parentElement: $shemot,
));
$this->createSession($user, 'valid-token'); $this->createSession($user, 'valid-token');
$response = $this->credentialedPost('/api/schedules', [ $response = $this->credentialedPost('/api/schedules', [
'setId' => $set->getId(), 'setId' => $set->getId(),
'elementKind' => 'chapter', 'levelId' => $chapterLevel->getId(),
'startDate' => '2026-08-10', 'startDate' => '2026-08-10',
'targetDate' => '2026-08-12', 'targetDate' => '2026-08-12',
]); ]);
@ -114,6 +121,7 @@ class ScheduleEndpointTest extends TestCase
'kind' => 'chapter', 'kind' => 'chapter',
'path' => [ 'path' => [
'Exodus', 'Exodus',
'Shemot',
'Chapter 1', 'Chapter 1',
], ],
], ],
@ -144,6 +152,7 @@ class ScheduleEndpointTest extends TestCase
]); ]);
$this->assertDatabaseCount('schedule_assignments', 2); $this->assertDatabaseCount('schedule_assignments', 2);
$this->assertFalse(Schema::hasColumn('schedules', 'set_id')); $this->assertFalse(Schema::hasColumn('schedules', 'set_id'));
$this->assertFalse(Schema::hasColumn('schedules', 'set_level_id'));
$this->assertFalse(Schema::hasColumn( $this->assertFalse(Schema::hasColumn(
'schedule_assignments', 'schedule_assignments',
'element_id', 'element_id',
@ -159,10 +168,10 @@ class ScheduleEndpointTest extends TestCase
$user = $this->createUser('reader@example.com'); $user = $this->createUser('reader@example.com');
$otherUser = $this->createUser('other@example.com'); $otherUser = $this->createUser('other@example.com');
$set = $this->createSet($user, 'Course'); $set = $this->createSet($user, 'Course');
$lessonLevel = $this->createLevel($set, 'lesson');
app(ElementRepository::class)->create(new CreateElementDto( app(ElementRepository::class)->create(new CreateElementDto(
set: $set,
name: 'Welcome', name: 'Welcome',
kind: 'lesson', level: $lessonLevel,
parentElement: null, parentElement: null,
)); ));
$this->createSession($user, 'valid-token'); $this->createSession($user, 'valid-token');
@ -170,13 +179,13 @@ class ScheduleEndpointTest extends TestCase
$this->credentialedPost('/api/schedules', [ $this->credentialedPost('/api/schedules', [
'setId' => $set->getId(), 'setId' => $set->getId(),
'elementKind' => 'lesson', 'levelId' => $lessonLevel->getId(),
'startDate' => '2026-08-01', 'startDate' => '2026-08-01',
'targetDate' => '2026-08-01', 'targetDate' => '2026-08-01',
])->assertCreated(); ])->assertCreated();
$this->credentialedPost('/api/schedules', [ $this->credentialedPost('/api/schedules', [
'setId' => $set->getId(), 'setId' => $set->getId(),
'elementKind' => 'lesson', 'levelId' => $lessonLevel->getId(),
'startDate' => '2026-09-01', 'startDate' => '2026-09-01',
'targetDate' => '2026-09-01', 'targetDate' => '2026-09-01',
])->assertCreated(); ])->assertCreated();
@ -186,7 +195,7 @@ class ScheduleEndpointTest extends TestCase
'other-token', 'other-token',
)->postJson('/api/schedules', [ )->postJson('/api/schedules', [
'setId' => $set->getId(), 'setId' => $set->getId(),
'elementKind' => 'lesson', 'levelId' => $lessonLevel->getId(),
'startDate' => '2026-10-01', 'startDate' => '2026-10-01',
'targetDate' => '2026-10-01', 'targetDate' => '2026-10-01',
])->assertCreated(); ])->assertCreated();
@ -223,23 +232,23 @@ class ScheduleEndpointTest extends TestCase
{ {
$user = $this->createUser('reader@example.com'); $user = $this->createUser('reader@example.com');
$set = $this->createSet($user, 'Original set'); $set = $this->createSet($user, 'Original set');
$groupLevel = $this->createLevel($set, 'group');
$taskLevel = $this->createLevel($set, 'task');
$repository = app(ElementRepository::class); $repository = app(ElementRepository::class);
$parent = $repository->create(new CreateElementDto( $parent = $repository->create(new CreateElementDto(
set: $set,
name: 'Original parent', name: 'Original parent',
kind: 'group', level: $groupLevel,
parentElement: null, parentElement: null,
)); ));
$element = $repository->create(new CreateElementDto( $element = $repository->create(new CreateElementDto(
set: $set,
name: 'Original item', name: 'Original item',
kind: 'task', level: $taskLevel,
parentElement: $parent, parentElement: $parent,
)); ));
$this->createSession($user, 'valid-token'); $this->createSession($user, 'valid-token');
$createdResponse = $this->credentialedPost('/api/schedules', [ $createdResponse = $this->credentialedPost('/api/schedules', [
'setId' => $set->getId(), 'setId' => $set->getId(),
'elementKind' => 'task', 'levelId' => $taskLevel->getId(),
'startDate' => '2026-08-10', 'startDate' => '2026-08-10',
'targetDate' => '2026-08-10', 'targetDate' => '2026-08-10',
])->assertCreated(); ])->assertCreated();
@ -247,12 +256,14 @@ class ScheduleEndpointTest extends TestCase
SetModel::query()->whereKey($set->getId())->update([ SetModel::query()->whereKey($set->getId())->update([
'name' => 'Changed set', 'name' => 'Changed set',
]); ]);
SetLevelModel::query()->whereKey($taskLevel->getId())->update([
'kind' => 'changed-kind',
]);
ElementModel::query()->whereKey($parent->getId())->update([ ElementModel::query()->whereKey($parent->getId())->update([
'name' => 'Changed parent', 'name' => 'Changed parent',
]); ]);
ElementModel::query()->whereKey($element->getId())->update([ ElementModel::query()->whereKey($element->getId())->update([
'name' => 'Changed item', 'name' => 'Changed item',
'kind' => 'changed-kind',
]); ]);
$this->credentialedGet('/api/schedules/1') $this->credentialedGet('/api/schedules/1')
@ -273,10 +284,10 @@ class ScheduleEndpointTest extends TestCase
$owner = $this->createUser('owner@example.com'); $owner = $this->createUser('owner@example.com');
$viewer = $this->createUser('viewer@example.com'); $viewer = $this->createUser('viewer@example.com');
$set = $this->createSet($owner, 'Course'); $set = $this->createSet($owner, 'Course');
$lessonLevel = $this->createLevel($set, 'lesson');
app(ElementRepository::class)->create(new CreateElementDto( app(ElementRepository::class)->create(new CreateElementDto(
set: $set,
name: 'Welcome', name: 'Welcome',
kind: 'lesson', level: $lessonLevel,
parentElement: null, parentElement: null,
)); ));
$this->createSession($owner, 'owner-token'); $this->createSession($owner, 'owner-token');
@ -288,7 +299,7 @@ class ScheduleEndpointTest extends TestCase
'owner-token', 'owner-token',
)->postJson('/api/schedules', [ )->postJson('/api/schedules', [
'setId' => $set->getId(), 'setId' => $set->getId(),
'elementKind' => 'lesson', 'levelId' => $lessonLevel->getId(),
'startDate' => '2026-08-01', 'startDate' => '2026-08-01',
'targetDate' => '2026-08-01', 'targetDate' => '2026-08-01',
])->assertCreated(); ])->assertCreated();
@ -309,7 +320,7 @@ class ScheduleEndpointTest extends TestCase
$this->credentialedPost('/api/schedules', [ $this->credentialedPost('/api/schedules', [
'setId' => 999, 'setId' => 999,
'elementKind' => 'chapter', 'levelId' => 1,
'startDate' => '2026-08-12', 'startDate' => '2026-08-12',
'targetDate' => '2026-08-10', 'targetDate' => '2026-08-10',
])->assertNotFound()->assertExactJson([ ])->assertNotFound()->assertExactJson([
@ -340,6 +351,14 @@ class ScheduleEndpointTest extends TestCase
)); ));
} }
private function createLevel(Set $set, string $kind): SetLevel
{
return app(SetLevelRepository::class)->create(new CreateSetLevelDto(
set: $set,
kind: $kind,
));
}
private function createSession(User $user, string $token): void private function createSession(User $user, string $token): void
{ {
$createdAt = new DateTimeImmutable( $createdAt = new DateTimeImmutable(

View file

@ -8,7 +8,10 @@ use App\Element\CreateElementDto;
use App\Element\ElementRepository; use App\Element\ElementRepository;
use App\Http\Middleware\AuthMiddleware; use App\Http\Middleware\AuthMiddleware;
use App\Set\CreateSetDto; use App\Set\CreateSetDto;
use App\Set\CreateSetLevelDto;
use App\Set\Set; use App\Set\Set;
use App\Set\SetLevel;
use App\Set\SetLevelRepository;
use App\Set\SetRepository; use App\Set\SetRepository;
use App\Shared\ValueObject\EmailAddress; use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto; use App\User\CreateUserDto;
@ -29,28 +32,27 @@ class GetSetLayoutEndpointTest extends TestCase
$user = $this->createUser(); $user = $this->createUser();
$set = $this->createSet($user, 'Bible'); $set = $this->createSet($user, 'Bible');
$repository = app(ElementRepository::class); $repository = app(ElementRepository::class);
$bookLevel = $this->createLevel($set, 'book');
$portionLevel = $this->createLevel($set, 'portion');
$chapterLevel = $this->createLevel($set, 'chapter');
$genesis = $repository->create(new CreateElementDto( $genesis = $repository->create(new CreateElementDto(
set: $set,
name: 'Genesis', name: 'Genesis',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$repository->create(new CreateElementDto( $repository->create(new CreateElementDto(
set: $set,
name: 'Exodus', name: 'Exodus',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$creation = $repository->create(new CreateElementDto( $creation = $repository->create(new CreateElementDto(
set: $set,
name: 'Creation', name: 'Creation',
kind: 'portion', level: $portionLevel,
parentElement: $genesis, parentElement: $genesis,
)); ));
$chapter = $repository->create(new CreateElementDto( $chapter = $repository->create(new CreateElementDto(
set: $set,
name: 'Chapter 1', name: 'Chapter 1',
kind: 'chapter', level: $chapterLevel,
parentElement: $creation, parentElement: $creation,
)); ));
$this->createSession($user); $this->createSession($user);
@ -62,21 +64,44 @@ class GetSetLayoutEndpointTest extends TestCase
'id' => $set->getId(), 'id' => $set->getId(),
'name' => 'Bible', 'name' => 'Bible',
], ],
'levels' => [
[
'id' => $bookLevel->getId(),
'kind' => 'book',
'depth' => 0,
'elementCount' => 2,
],
[
'id' => $portionLevel->getId(),
'kind' => 'portion',
'depth' => 1,
'elementCount' => 1,
],
[
'id' => $chapterLevel->getId(),
'kind' => 'chapter',
'depth' => 2,
'elementCount' => 1,
],
],
'elements' => [ 'elements' => [
[ [
'id' => $genesis->getId(), 'id' => $genesis->getId(),
'name' => 'Genesis', 'name' => 'Genesis',
'kind' => 'book', 'kind' => 'book',
'levelId' => $bookLevel->getId(),
'children' => [ 'children' => [
[ [
'id' => $creation->getId(), 'id' => $creation->getId(),
'name' => 'Creation', 'name' => 'Creation',
'kind' => 'portion', 'kind' => 'portion',
'levelId' => $portionLevel->getId(),
'children' => [ 'children' => [
[ [
'id' => $chapter->getId(), 'id' => $chapter->getId(),
'name' => 'Chapter 1', 'name' => 'Chapter 1',
'kind' => 'chapter', 'kind' => 'chapter',
'levelId' => $chapterLevel->getId(),
'children' => [], 'children' => [],
], ],
], ],
@ -87,6 +112,7 @@ class GetSetLayoutEndpointTest extends TestCase
'id' => $genesis->getId() + 1, 'id' => $genesis->getId() + 1,
'name' => 'Exodus', 'name' => 'Exodus',
'kind' => 'book', 'kind' => 'book',
'levelId' => $bookLevel->getId(),
'children' => [], 'children' => [],
], ],
], ],
@ -97,6 +123,7 @@ class GetSetLayoutEndpointTest extends TestCase
{ {
$user = $this->createUser(); $user = $this->createUser();
$set = $this->createSet($user, 'Empty set'); $set = $this->createSet($user, 'Empty set');
$taskLevel = $this->createLevel($set, 'task');
$this->createSession($user); $this->createSession($user);
$response = $this->credentialedGet("/api/sets/{$set->getId()}"); $response = $this->credentialedGet("/api/sets/{$set->getId()}");
@ -106,6 +133,12 @@ class GetSetLayoutEndpointTest extends TestCase
'id' => $set->getId(), 'id' => $set->getId(),
'name' => 'Empty set', 'name' => 'Empty set',
], ],
'levels' => [[
'id' => $taskLevel->getId(),
'kind' => 'task',
'depth' => 0,
'elementCount' => 0,
]],
'elements' => [], 'elements' => [],
]); ]);
} }
@ -147,6 +180,14 @@ class GetSetLayoutEndpointTest extends TestCase
)); ));
} }
private function createLevel(Set $set, string $kind): SetLevel
{
return app(SetLevelRepository::class)->create(new CreateSetLevelDto(
set: $set,
kind: $kind,
));
}
private function createSession(User $user): void private function createSession(User $user): void
{ {
$createdAt = new DateTimeImmutable( $createdAt = new DateTimeImmutable(

View file

@ -8,92 +8,110 @@ use App\Exceptions\NotFoundException;
use App\Schedule\UseCases\CreateSchedule\CreateSchedule; use App\Schedule\UseCases\CreateSchedule\CreateSchedule;
use App\Schedule\UseCases\CreateSchedule\CreateScheduleRequest; use App\Schedule\UseCases\CreateSchedule\CreateScheduleRequest;
use App\Set\CreateSetDto; use App\Set\CreateSetDto;
use App\Set\CreateSetLevelDto;
use App\Set\Set;
use App\Set\SetLevel;
use App\Shared\ValueObject\EmailAddress; use App\Shared\ValueObject\EmailAddress;
use App\User\User; use App\User\User;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeElementRepository; use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeScheduleRepository; use Tests\Fakes\FakeScheduleRepository;
use Tests\Fakes\FakeSetRepository; use Tests\Fakes\FakeSetRepository;
use Tests\Fakes\FakeSetLevelRepository;
class CreateScheduleTest extends TestCase class CreateScheduleTest extends TestCase
{ {
public function test_it_schedules_every_matching_element_in_tree_order(): void public function test_it_schedules_every_element_on_the_selected_level(): void
{ {
$user = $this->user(); $user = $this->user();
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$elementRepository = new FakeElementRepository; $elementRepository = new FakeElementRepository;
$scheduleRepository = new FakeScheduleRepository; $scheduleRepository = new FakeScheduleRepository;
$set = $setRepository->create(new CreateSetDto( $set = $setRepository->create(new CreateSetDto(
name: 'Bible', name: 'Bible',
creator: $user, creator: $user,
)); ));
$bookLevel = $this->createLevel(
$setLevelRepository,
$set,
'book',
);
$portionLevel = $this->createLevel(
$setLevelRepository,
$set,
'portion',
);
$chapterLevel = $this->createLevel(
$setLevelRepository,
$set,
'chapter',
);
$genesis = $elementRepository->create(new CreateElementDto( $genesis = $elementRepository->create(new CreateElementDto(
set: $set,
name: 'Genesis', name: 'Genesis',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$exodus = $elementRepository->create(new CreateElementDto( $exodus = $elementRepository->create(new CreateElementDto(
set: $set,
name: 'Exodus', name: 'Exodus',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$exodusChapterOne = $elementRepository->create(
new CreateElementDto(
set: $set,
name: 'Exodus 1',
kind: 'chapter',
parentElement: $exodus,
),
);
$creation = $elementRepository->create(new CreateElementDto( $creation = $elementRepository->create(new CreateElementDto(
set: $set,
name: 'Creation', name: 'Creation',
kind: 'portion', level: $portionLevel,
parentElement: $genesis, parentElement: $genesis,
)); ));
$shemot = $elementRepository->create(new CreateElementDto(
name: 'Shemot',
level: $portionLevel,
parentElement: $exodus,
));
$genesisChapterOne = $elementRepository->create( $genesisChapterOne = $elementRepository->create(
new CreateElementDto( new CreateElementDto(
set: $set,
name: 'Genesis 1', name: 'Genesis 1',
kind: 'chapter', level: $chapterLevel,
parentElement: $creation, parentElement: $creation,
), ),
); );
$genesisChapterTwo = $elementRepository->create( $genesisChapterTwo = $elementRepository->create(
new CreateElementDto( new CreateElementDto(
set: $set,
name: 'Genesis 2', name: 'Genesis 2',
kind: 'chapter', level: $chapterLevel,
parentElement: $creation, parentElement: $creation,
), ),
); );
$genesisChapterThree = $elementRepository->create( $genesisChapterThree = $elementRepository->create(
new CreateElementDto( new CreateElementDto(
set: $set,
name: 'Genesis 3', name: 'Genesis 3',
kind: 'chapter', level: $chapterLevel,
parentElement: $creation, parentElement: $creation,
), ),
); );
$exodusChapterOne = $elementRepository->create(
new CreateElementDto(
name: 'Exodus 1',
level: $chapterLevel,
parentElement: $shemot,
),
);
$exodusChapterTwo = $elementRepository->create( $exodusChapterTwo = $elementRepository->create(
new CreateElementDto( new CreateElementDto(
set: $set,
name: 'Exodus 2', name: 'Exodus 2',
kind: 'chapter', level: $chapterLevel,
parentElement: $exodus, parentElement: $shemot,
), ),
); );
$schedule = (new CreateSchedule( $schedule = (new CreateSchedule(
$setRepository, $setRepository,
$setLevelRepository,
$elementRepository, $elementRepository,
$scheduleRepository, $scheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $user, user: $user,
setId: $set->getId(), setId: $set->getId(),
elementKind: 'chapter', levelId: $chapterLevel->getId(),
startDate: '2026-08-10', startDate: '2026-08-10',
targetDate: '2026-08-12', targetDate: '2026-08-12',
)); ));
@ -139,29 +157,35 @@ class CreateScheduleTest extends TestCase
{ {
$user = $this->user(); $user = $this->user();
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$elementRepository = new FakeElementRepository; $elementRepository = new FakeElementRepository;
$set = $setRepository->create(new CreateSetDto( $set = $setRepository->create(new CreateSetDto(
name: 'Course', name: 'Course',
creator: $user, creator: $user,
)); ));
$lessonLevel = $this->createLevel(
$setLevelRepository,
$set,
'lesson',
);
foreach (['First', 'Second', 'Third'] as $name) { foreach (['First', 'Second', 'Third'] as $name) {
$elementRepository->create(new CreateElementDto( $elementRepository->create(new CreateElementDto(
set: $set,
name: $name, name: $name,
kind: 'lesson', level: $lessonLevel,
parentElement: null, parentElement: null,
)); ));
} }
$schedule = (new CreateSchedule( $schedule = (new CreateSchedule(
$setRepository, $setRepository,
$setLevelRepository,
$elementRepository, $elementRepository,
new FakeScheduleRepository, new FakeScheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $user, user: $user,
setId: $set->getId(), setId: $set->getId(),
elementKind: 'lesson', levelId: $lessonLevel->getId(),
startDate: '2026-08-10', startDate: '2026-08-10',
targetDate: '2026-08-16', targetDate: '2026-08-16',
)); ));
@ -178,26 +202,32 @@ class CreateScheduleTest extends TestCase
{ {
$user = $this->user(); $user = $this->user();
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$elementRepository = new FakeElementRepository; $elementRepository = new FakeElementRepository;
$set = $setRepository->create(new CreateSetDto( $set = $setRepository->create(new CreateSetDto(
name: 'Project', name: 'Project',
creator: $user, creator: $user,
)); ));
$milestoneLevel = $this->createLevel(
$setLevelRepository,
$set,
'milestone',
);
$elementRepository->create(new CreateElementDto( $elementRepository->create(new CreateElementDto(
set: $set,
name: 'Ship it', name: 'Ship it',
kind: 'milestone', level: $milestoneLevel,
parentElement: null, parentElement: null,
)); ));
$schedule = (new CreateSchedule( $schedule = (new CreateSchedule(
$setRepository, $setRepository,
$setLevelRepository,
$elementRepository, $elementRepository,
new FakeScheduleRepository, new FakeScheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $user, user: $user,
setId: $set->getId(), setId: $set->getId(),
elementKind: 'milestone', levelId: $milestoneLevel->getId(),
startDate: '2020-01-01', startDate: '2020-01-01',
targetDate: '2030-01-01', targetDate: '2030-01-01',
)); ));
@ -217,18 +247,19 @@ class CreateScheduleTest extends TestCase
(new CreateSchedule( (new CreateSchedule(
new FakeSetRepository, new FakeSetRepository,
new FakeSetLevelRepository,
new FakeElementRepository, new FakeElementRepository,
new FakeScheduleRepository, new FakeScheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $this->user(), user: $this->user(),
setId: 999, setId: 999,
elementKind: 'chapter', levelId: 1,
startDate: '2026-08-10', startDate: '2026-08-10',
targetDate: '2026-08-12', targetDate: '2026-08-12',
)); ));
} }
public function test_it_rejects_an_unavailable_element_kind(): void public function test_it_rejects_a_missing_level(): void
{ {
$user = $this->user(); $user = $this->user();
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
@ -238,18 +269,85 @@ class CreateScheduleTest extends TestCase
)); ));
$this->expectException(BadRequestException::class); $this->expectException(BadRequestException::class);
$this->expectExceptionMessage( $this->expectExceptionMessage('levelId is required');
'elementKind is not available for set',
);
(new CreateSchedule( (new CreateSchedule(
$setRepository, $setRepository,
new FakeSetLevelRepository,
new FakeElementRepository, new FakeElementRepository,
new FakeScheduleRepository, new FakeScheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $user, user: $user,
setId: $set->getId(), setId: $set->getId(),
elementKind: 'Chapter', levelId: null,
startDate: '2026-08-10',
targetDate: '2026-08-12',
));
}
public function test_it_rejects_a_level_from_another_set(): void
{
$user = $this->user();
$setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$bible = $setRepository->create(new CreateSetDto(
name: 'Bible',
creator: $user,
));
$course = $setRepository->create(new CreateSetDto(
name: 'Course',
creator: $user,
));
$moduleLevel = $this->createLevel(
$setLevelRepository,
$course,
'module',
);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('level is not available for set');
(new CreateSchedule(
$setRepository,
$setLevelRepository,
new FakeElementRepository,
new FakeScheduleRepository,
))->execute(new CreateScheduleRequest(
user: $user,
setId: $bible->getId(),
levelId: $moduleLevel->getId(),
startDate: '2026-08-10',
targetDate: '2026-08-12',
));
}
public function test_it_rejects_a_level_without_elements(): void
{
$user = $this->user();
$setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$set = $setRepository->create(new CreateSetDto(
name: 'Bible',
creator: $user,
));
$chapterLevel = $this->createLevel(
$setLevelRepository,
$set,
'chapter',
);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('level has no elements');
(new CreateSchedule(
$setRepository,
$setLevelRepository,
new FakeElementRepository,
new FakeScheduleRepository,
))->execute(new CreateScheduleRequest(
user: $user,
setId: $set->getId(),
levelId: $chapterLevel->getId(),
startDate: '2026-08-10', startDate: '2026-08-10',
targetDate: '2026-08-12', targetDate: '2026-08-12',
)); ));
@ -259,22 +357,29 @@ class CreateScheduleTest extends TestCase
{ {
$user = $this->user(); $user = $this->user();
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$set = $setRepository->create(new CreateSetDto( $set = $setRepository->create(new CreateSetDto(
name: 'Bible', name: 'Bible',
creator: $user, creator: $user,
)); ));
$chapterLevel = $this->createLevel(
$setLevelRepository,
$set,
'chapter',
);
$this->expectException(BadRequestException::class); $this->expectException(BadRequestException::class);
$this->expectExceptionMessage('startDate must be a valid date'); $this->expectExceptionMessage('startDate must be a valid date');
(new CreateSchedule( (new CreateSchedule(
$setRepository, $setRepository,
$setLevelRepository,
new FakeElementRepository, new FakeElementRepository,
new FakeScheduleRepository, new FakeScheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $user, user: $user,
setId: $set->getId(), setId: $set->getId(),
elementKind: 'chapter', levelId: $chapterLevel->getId(),
startDate: '2026-02-30', startDate: '2026-02-30',
targetDate: '2026-08-12', targetDate: '2026-08-12',
)); ));
@ -284,10 +389,16 @@ class CreateScheduleTest extends TestCase
{ {
$user = $this->user(); $user = $this->user();
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$set = $setRepository->create(new CreateSetDto( $set = $setRepository->create(new CreateSetDto(
name: 'Bible', name: 'Bible',
creator: $user, creator: $user,
)); ));
$chapterLevel = $this->createLevel(
$setLevelRepository,
$set,
'chapter',
);
$this->expectException(BadRequestException::class); $this->expectException(BadRequestException::class);
$this->expectExceptionMessage( $this->expectExceptionMessage(
@ -296,17 +407,29 @@ class CreateScheduleTest extends TestCase
(new CreateSchedule( (new CreateSchedule(
$setRepository, $setRepository,
$setLevelRepository,
new FakeElementRepository, new FakeElementRepository,
new FakeScheduleRepository, new FakeScheduleRepository,
))->execute(new CreateScheduleRequest( ))->execute(new CreateScheduleRequest(
user: $user, user: $user,
setId: $set->getId(), setId: $set->getId(),
elementKind: 'chapter', levelId: $chapterLevel->getId(),
startDate: '2026-08-12', startDate: '2026-08-12',
targetDate: '2026-08-10', targetDate: '2026-08-10',
)); ));
} }
private function createLevel(
FakeSetLevelRepository $repository,
Set $set,
string $kind,
): SetLevel {
return $repository->create(new CreateSetLevelDto(
set: $set,
kind: $kind,
));
}
private function user(): User private function user(): User
{ {
return new User( return new User(

View file

@ -5,6 +5,7 @@ namespace Tests\Unit\Set\UseCases;
use App\Element\CreateElementDto; use App\Element\CreateElementDto;
use App\Exceptions\NotFoundException; use App\Exceptions\NotFoundException;
use App\Set\CreateSetDto; use App\Set\CreateSetDto;
use App\Set\CreateSetLevelDto;
use App\Set\UseCases\GetSetLayout\ElementLayoutNode; use App\Set\UseCases\GetSetLayout\ElementLayoutNode;
use App\Set\UseCases\GetSetLayout\GetSetLayout; use App\Set\UseCases\GetSetLayout\GetSetLayout;
use App\Set\UseCases\GetSetLayout\GetSetLayoutRequest; use App\Set\UseCases\GetSetLayout\GetSetLayoutRequest;
@ -13,6 +14,7 @@ use App\User\User;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeElementRepository; use Tests\Fakes\FakeElementRepository;
use Tests\Fakes\FakeSetRepository; use Tests\Fakes\FakeSetRepository;
use Tests\Fakes\FakeSetLevelRepository;
class GetSetLayoutTest extends TestCase class GetSetLayoutTest extends TestCase
{ {
@ -24,6 +26,7 @@ class GetSetLayoutTest extends TestCase
passwordHash: 'hashed-password', passwordHash: 'hashed-password',
); );
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$elementRepository = new FakeElementRepository; $elementRepository = new FakeElementRepository;
$bible = $setRepository->create(new CreateSetDto( $bible = $setRepository->create(new CreateSetDto(
name: 'Bible', name: 'Bible',
@ -33,51 +36,69 @@ class GetSetLayoutTest extends TestCase
name: 'Course', name: 'Course',
creator: $creator, creator: $creator,
)); ));
$genesis = $elementRepository->create(new CreateElementDto( $bookLevel = $setLevelRepository->create(new CreateSetLevelDto(
set: $bible, set: $bible,
name: 'Genesis',
kind: 'book', kind: 'book',
));
$portionLevel = $setLevelRepository->create(new CreateSetLevelDto(
set: $bible,
kind: 'portion',
));
$chapterLevel = $setLevelRepository->create(new CreateSetLevelDto(
set: $bible,
kind: 'chapter',
));
$moduleLevel = $setLevelRepository->create(new CreateSetLevelDto(
set: $course,
kind: 'module',
));
$genesis = $elementRepository->create(new CreateElementDto(
name: 'Genesis',
level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$elementRepository->create(new CreateElementDto( $elementRepository->create(new CreateElementDto(
set: $bible,
name: 'Exodus', name: 'Exodus',
kind: 'book', level: $bookLevel,
parentElement: null, parentElement: null,
)); ));
$creation = $elementRepository->create(new CreateElementDto( $creation = $elementRepository->create(new CreateElementDto(
set: $bible,
name: 'Creation', name: 'Creation',
kind: 'portion', level: $portionLevel,
parentElement: $genesis, parentElement: $genesis,
)); ));
$elementRepository->create(new CreateElementDto( $elementRepository->create(new CreateElementDto(
set: $bible,
name: 'Noah', name: 'Noah',
kind: 'portion', level: $portionLevel,
parentElement: $genesis, parentElement: $genesis,
)); ));
$elementRepository->create(new CreateElementDto( $elementRepository->create(new CreateElementDto(
set: $bible,
name: 'Chapter 1', name: 'Chapter 1',
kind: 'chapter', level: $chapterLevel,
parentElement: $creation, parentElement: $creation,
)); ));
$elementRepository->create(new CreateElementDto( $elementRepository->create(new CreateElementDto(
set: $course,
name: 'Foundations', name: 'Foundations',
kind: 'module', level: $moduleLevel,
parentElement: null, parentElement: null,
)); ));
$layout = (new GetSetLayout( $layout = (new GetSetLayout(
$setRepository, $setRepository,
$setLevelRepository,
$elementRepository, $elementRepository,
))->execute(new GetSetLayoutRequest( ))->execute(new GetSetLayoutRequest(
setId: $bible->getId(), setId: $bible->getId(),
)); ));
$this->assertSame('Bible', $layout->getSet()->getName()); $this->assertSame('Bible', $layout->getSet()->getName());
$this->assertSame(
['book:2', 'portion:2', 'chapter:1'],
array_map(function ($level): string {
return $level->getLevel()->getKind()
. ":{$level->getElementCount()}";
}, $layout->getLevels()),
);
$this->assertSame( $this->assertSame(
['Genesis', 'Exodus'], ['Genesis', 'Exodus'],
$this->nodeNames($layout->getElements()), $this->nodeNames($layout->getElements()),
@ -103,19 +124,26 @@ class GetSetLayoutTest extends TestCase
passwordHash: 'hashed-password', passwordHash: 'hashed-password',
); );
$setRepository = new FakeSetRepository; $setRepository = new FakeSetRepository;
$setLevelRepository = new FakeSetLevelRepository;
$set = $setRepository->create(new CreateSetDto( $set = $setRepository->create(new CreateSetDto(
name: 'Empty set', name: 'Empty set',
creator: $creator, creator: $creator,
)); ));
$setLevelRepository->create(new CreateSetLevelDto(
set: $set,
kind: 'task',
));
$layout = (new GetSetLayout( $layout = (new GetSetLayout(
$setRepository, $setRepository,
$setLevelRepository,
new FakeElementRepository, new FakeElementRepository,
))->execute(new GetSetLayoutRequest( ))->execute(new GetSetLayoutRequest(
setId: $set->getId(), setId: $set->getId(),
)); ));
$this->assertSame([], $layout->getElements()); $this->assertSame([], $layout->getElements());
$this->assertSame(0, $layout->getLevels()[0]->getElementCount());
} }
public function test_it_rejects_an_unknown_set(): void public function test_it_rejects_an_unknown_set(): void
@ -125,6 +153,7 @@ class GetSetLayoutTest extends TestCase
(new GetSetLayout( (new GetSetLayout(
new FakeSetRepository, new FakeSetRepository,
new FakeSetLevelRepository,
new FakeElementRepository, new FakeElementRepository,
))->execute(new GetSetLayoutRequest( ))->execute(new GetSetLayoutRequest(
setId: 999, setId: 999,