test explicit set levels
This commit is contained in:
parent
0e8d45a408
commit
7e8850b1b2
1 changed files with 92 additions and 0 deletions
92
backend/tests/Feature/Set/EloquentSetLevelRepositoryTest.php
Normal file
92
backend/tests/Feature/Set/EloquentSetLevelRepositoryTest.php
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Set;
|
||||||
|
|
||||||
|
use App\Set\CreateSetDto;
|
||||||
|
use App\Set\CreateSetLevelDto;
|
||||||
|
use App\Set\Set;
|
||||||
|
use App\Set\SetLevelRepository;
|
||||||
|
use App\Set\SetRepository;
|
||||||
|
use App\Shared\ValueObject\EmailAddress;
|
||||||
|
use App\User\CreateUserDto;
|
||||||
|
use App\User\UserRepository;
|
||||||
|
use DomainException;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class EloquentSetLevelRepositoryTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function testItCreatesOrderedLevelsWithinEachSet(): void
|
||||||
|
{
|
||||||
|
$bible = $this->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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue