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, )); } }