92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?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,
|
|
));
|
|
}
|
|
}
|