test set persistence
This commit is contained in:
parent
783c522f7e
commit
4ac01460fd
2 changed files with 136 additions and 0 deletions
107
backend/tests/Feature/Set/EloquentSetRepositoryTest.php
Normal file
107
backend/tests/Feature/Set/EloquentSetRepositoryTest.php
Normal 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',
|
||||
));
|
||||
}
|
||||
}
|
||||
29
backend/tests/Unit/Set/SetTest.php
Normal file
29
backend/tests/Unit/Set/SetTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Set;
|
||||
|
||||
use App\Set\Set;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SetTest extends TestCase
|
||||
{
|
||||
public function test_it_exposes_its_identity_name_and_creator(): void
|
||||
{
|
||||
$creator = new User(
|
||||
id: 7,
|
||||
email: new EmailAddress('creator@example.com'),
|
||||
passwordHash: 'hashed-password',
|
||||
);
|
||||
$set = new Set(
|
||||
id: 42,
|
||||
name: 'Bible',
|
||||
creator: $creator,
|
||||
);
|
||||
|
||||
$this->assertSame(42, $set->getId());
|
||||
$this->assertSame('Bible', $set->getName());
|
||||
$this->assertSame($creator, $set->getCreator());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue