From 7e8850b1b2db6a578544c5cf922b54e91da0824f Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Tue, 11 Aug 2026 22:46:29 +0300 Subject: [PATCH] test explicit set levels --- .../Set/EloquentSetLevelRepositoryTest.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 backend/tests/Feature/Set/EloquentSetLevelRepositoryTest.php diff --git a/backend/tests/Feature/Set/EloquentSetLevelRepositoryTest.php b/backend/tests/Feature/Set/EloquentSetLevelRepositoryTest.php new file mode 100644 index 0000000..29f1c40 --- /dev/null +++ b/backend/tests/Feature/Set/EloquentSetLevelRepositoryTest.php @@ -0,0 +1,92 @@ +createSet('Bible'); + $course = $this->createSet('Course'); + $repository = app(SetLevelRepository::class); + + $book = $repository->create(new CreateSetLevelDto( + set: $bible, + kind: 'book', + )); + $portion = $repository->create(new CreateSetLevelDto( + set: $bible, + kind: 'portion', + )); + $module = $repository->create(new CreateSetLevelDto( + set: $course, + kind: 'module', + )); + + $this->assertSame(0, $book->getDepth()); + $this->assertSame(1, $portion->getDepth()); + $this->assertSame(0, $module->getDepth()); + $this->assertSame( + ['book', 'portion'], + array_map(function ($level): string { + return $level->getKind(); + }, $repository->findBySet($bible)), + ); + $this->assertSame( + $portion->getId(), + $repository->find($portion->getId())?->getId(), + ); + $this->assertDatabaseHas('set_levels', [ + 'set_id' => $bible->getId(), + 'kind' => 'portion', + 'depth' => 1, + ]); + } + + public function testItRejectsARepeatedKindWithinASet(): void + { + $set = $this->createSet('Bible'); + $repository = app(SetLevelRepository::class); + $repository->create(new CreateSetLevelDto( + set: $set, + kind: 'book', + )); + + $this->expectException(DomainException::class); + $this->expectExceptionMessage( + 'level kind must be unique within set', + ); + + $repository->create(new CreateSetLevelDto( + set: $set, + kind: 'book', + )); + } + + private function createSet(string $name): Set + { + $user = app(UserRepository::class)->create(new CreateUserDto( + email: new EmailAddress(strtolower($name).'@example.com'), + passwordHash: 'hashed-password', + )); + + return app(SetRepository::class)->create(new CreateSetDto( + name: $name, + creator: $user, + )); + } +}