Attainly/backend/tests/Feature/Database/DatabaseSeederTest.php

135 lines
4.3 KiB
PHP

<?php
namespace Tests\Feature\Database;
use App\Auth\PasswordHasher;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Set\SetRepository;
use App\Set\SetLevelRepository;
use App\Shared\ValueObject\EmailAddress;
use App\User\UserRepository;
use Database\Seeders\UserSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class DatabaseSeederTest extends TestCase
{
use RefreshDatabase;
public function test_it_seeds_the_development_user_idempotently(): void
{
$this->seed();
$this->seed();
$this->assertDatabaseHas('users', [
'email' => 'user@example.com',
]);
$this->assertDatabaseMissing('users', [
'email' => 'user@example.com',
'passwordHash' => 'password',
]);
$this->assertDatabaseCount('users', 1);
$user = app(UserRepository::class)->findByEmail(
new EmailAddress('user@example.com'),
);
$this->assertNotNull($user);
$this->assertTrue(app(PasswordHasher::class)->verify(
'password',
$user->getPasswordHash(),
));
}
public function test_it_seeds_the_available_sets_idempotently(): void
{
$this->seed();
$this->seed();
$sets = app(SetRepository::class)->all();
$this->assertDatabaseCount('sets', 3);
$this->assertSame(
['Bible', 'Course', 'Fitness Program'],
array_map(function ($set): string {
return $set->getName();
}, $sets),
);
foreach ($sets as $set) {
$this->assertSame(
UserSeeder::EMAIL,
$set->getCreator()->getEmail()->value(),
);
}
}
public function test_it_seeds_elements_for_every_set_idempotently(): void
{
$this->seed();
$this->seed();
$sets = app(SetRepository::class)->all();
$elementRepository = app(ElementRepository::class);
$setLevelRepository = app(SetLevelRepository::class);
$expectedLevels = [
'Bible' => ['book', 'portion', 'chapter'],
'Course' => ['module', 'lesson'],
'Fitness Program' => ['phase', 'workout', 'exercise'],
];
$expectedElements = [
'Bible' => [
'Chapter 1:chapter:Creation',
'Chapter 2:chapter:Creation',
'Creation:portion:Genesis',
'Exodus:book:root',
'Genesis:book:root',
'Noah:portion:Genesis',
'Shemot:portion:Exodus',
],
'Course' => [
'Applied Practice:module:root',
'Core Concepts:lesson:Foundations',
'Final Review:lesson:Applied Practice',
'Foundations:module:root',
'Guided Exercise:lesson:Applied Practice',
'Welcome:lesson:Foundations',
],
'Fitness Program' => [
'Build Phase:phase:root',
'Foundation Phase:phase:root',
'Full Body Circuit:workout:Build Phase',
'Hip Flow:exercise:Mobility Day',
'Mobility Day:workout:Foundation Phase',
'Push-up:exercise:Strength Day',
'Squat:exercise:Strength Day',
'Strength Day:workout:Foundation Phase',
],
];
$this->assertDatabaseCount('set_levels', 8);
$this->assertDatabaseCount('elements', 21);
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 {
$parentName = $element->getParentElement()?->getName()
?? 'root';
return "{$element->getName()}:{$element->getKind()}"
. ":{$parentName}";
}, $elementRepository->findBySet($set));
sort($signatures);
$this->assertSame(
$expectedElements[$set->getName()],
$signatures,
);
}
}
}