Attainly/backend/tests/Feature/Set/EloquentSetRepositoryTest.php

93 lines
2.7 KiB
PHP

<?php
namespace Tests\Feature\Set;
use App\Set\CreateSetDto;
use App\Set\SetRepository;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\User;
use App\User\UserRepository;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class EloquentSetRepositoryTest extends TestCase
{
use RefreshDatabase;
public function test_it_creates_sets_with_their_creator(): void
{
$creator = $this->createUser('creator@example.com');
$set = app(SetRepository::class)->create(new CreateSetDto(
name: 'Bible',
creator: $creator,
));
$this->assertGreaterThan(0, $set->getId());
$this->assertSame('Bible', $set->getName());
$this->assertSame($creator->getId(), $set->getCreator()->getId());
$this->assertDatabaseHas('sets', [
'id' => $set->getId(),
'name' => 'Bible',
'creator_id' => $creator->getId(),
]);
}
public function test_it_lists_every_set_alphabetically(): void
{
$firstCreator = $this->createUser('first@example.com');
$secondCreator = $this->createUser('second@example.com');
$repository = app(SetRepository::class);
$repository->create(new CreateSetDto(
name: 'Fitness Program',
creator: $firstCreator,
));
$repository->create(new CreateSetDto(
name: 'Bible',
creator: $secondCreator,
));
$repository->create(new CreateSetDto(
name: 'Course',
creator: $firstCreator,
));
$sets = $repository->all();
$this->assertSame(
['Bible', 'Course', 'Fitness Program'],
array_map(function ($set): string {
return $set->getName();
}, $sets),
);
$this->assertSame(
$secondCreator->getId(),
$sets[0]->getCreator()->getId(),
);
}
public function test_it_rejects_duplicate_set_names(): void
{
$creator = $this->createUser('creator@example.com');
$repository = app(SetRepository::class);
$repository->create(new CreateSetDto(
name: 'Bible',
creator: $creator,
));
$this->expectException(QueryException::class);
$repository->create(new CreateSetDto(
name: 'Bible',
creator: $creator,
));
}
private function createUser(string $email): User
{
return app(UserRepository::class)->create(new CreateUserDto(
email: new EmailAddress($email),
passwordHash: 'hashed-password',
));
}
}