test set persistence

This commit is contained in:
Yisroel Baum 2026-08-03 20:22:05 +03:00
parent 783c522f7e
commit 4ac01460fd
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,107 @@
<?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 Illuminate\Support\Facades\DB;
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,
));
}
public function test_it_prevents_deleting_a_set_creator(): void
{
$creator = $this->createUser('creator@example.com');
app(SetRepository::class)->create(new CreateSetDto(
name: 'Bible',
creator: $creator,
));
$this->expectException(QueryException::class);
DB::table('users')->where('id', $creator->getId())->delete();
}
private function createUser(string $email): User
{
return app(UserRepository::class)->create(new CreateUserDto(
email: new EmailAddress($email),
passwordHash: 'hashed-password',
));
}
}