test sets list endpoint

This commit is contained in:
Yisroel Baum 2026-08-03 20:24:01 +03:00
parent 5b9df7d02a
commit 61ab8c09dc
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 191 additions and 0 deletions

View file

@ -0,0 +1,49 @@
<?php
namespace Tests\Fakes;
use App\Set\CreateSetDto;
use App\Set\Set;
use App\Set\SetRepository;
class FakeSetRepository implements SetRepository
{
/**
* @var array<int, Set>
*/
private array $sets = [];
public function create(CreateSetDto $dto): Set
{
$id = count($this->sets) + 1;
$set = new Set(
id: $id,
name: $dto->name,
creator: $dto->creator,
);
$this->sets[$id] = $set;
return $this->copy($set);
}
public function all(): array
{
$sets = array_values($this->sets);
usort($sets, function (Set $first, Set $second): int {
return $first->getName() <=> $second->getName();
});
return array_map(function (Set $set): Set {
return $this->copy($set);
}, $sets);
}
private function copy(Set $set): Set
{
return new Set(
id: $set->getId(),
name: $set->getName(),
creator: $set->getCreator(),
);
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace Tests\Feature\Set;
use App\Auth\CreateSessionDto;
use App\Auth\SessionRepository;
use App\Http\Middleware\AuthMiddleware;
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 DateTimeImmutable;
use DateTimeZone;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ListSetsEndpointTest extends TestCase
{
use RefreshDatabase;
public function test_it_returns_every_set_without_creator_details(): void
{
$authenticatedUser = $this->createUser('reader@example.com');
$otherUser = $this->createUser('creator@example.com');
$repository = app(SetRepository::class);
$course = $repository->create(new CreateSetDto(
name: 'Course',
creator: $authenticatedUser,
));
$bible = $repository->create(new CreateSetDto(
name: 'Bible',
creator: $otherUser,
));
$this->createSession($authenticatedUser);
$response = $this->withCredentials()
->withUnencryptedCookie(
AuthMiddleware::COOKIE_NAME,
'valid-token',
)->getJson('/api/sets');
$response->assertOk()->assertExactJson([
'sets' => [
[
'id' => $bible->getId(),
'name' => 'Bible',
],
[
'id' => $course->getId(),
'name' => 'Course',
],
],
]);
}
public function test_it_returns_an_empty_catalog(): void
{
$authenticatedUser = $this->createUser('reader@example.com');
$this->createSession($authenticatedUser);
$response = $this->withCredentials()
->withUnencryptedCookie(
AuthMiddleware::COOKIE_NAME,
'valid-token',
)->getJson('/api/sets');
$response->assertOk()->assertExactJson(['sets' => []]);
}
public function test_it_rejects_an_unauthenticated_request(): void
{
$response = $this->getJson('/api/sets');
$response
->assertStatus(401)
->assertExactJson(['error' => 'unauthenticated']);
}
private function createUser(string $email): User
{
return app(UserRepository::class)->create(new CreateUserDto(
email: new EmailAddress($email),
passwordHash: 'hashed-password',
));
}
private function createSession(User $user): void
{
$createdAt = new DateTimeImmutable(
'2026-08-03T12:00:00',
new DateTimeZone('UTC'),
);
app(SessionRepository::class)->create(new CreateSessionDto(
token: 'valid-token',
user: $user,
createdAt: $createdAt,
expiresAt: $createdAt->modify('+7 days'),
));
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Tests\Unit\Set\UseCases;
use App\Set\CreateSetDto;
use App\Set\UseCases\ListSets\ListSets;
use App\Shared\ValueObject\EmailAddress;
use App\User\User;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeSetRepository;
class ListSetsTest extends TestCase
{
public function test_it_lists_every_available_set_alphabetically(): void
{
$creator = new User(
id: 7,
email: new EmailAddress('creator@example.com'),
passwordHash: 'hashed-password',
);
$repository = new FakeSetRepository;
$repository->create(new CreateSetDto(
name: 'Course',
creator: $creator,
));
$repository->create(new CreateSetDto(
name: 'Bible',
creator: $creator,
));
$sets = (new ListSets($repository))->execute();
$this->assertSame(
['Bible', 'Course'],
array_map(function ($set): string {
return $set->getName();
}, $sets),
);
}
}