test sets list endpoint
This commit is contained in:
parent
5b9df7d02a
commit
61ab8c09dc
3 changed files with 191 additions and 0 deletions
49
backend/tests/Fakes/FakeSetRepository.php
Normal file
49
backend/tests/Fakes/FakeSetRepository.php
Normal 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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
102
backend/tests/Feature/Set/ListSetsEndpointTest.php
Normal file
102
backend/tests/Feature/Set/ListSetsEndpointTest.php
Normal 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'),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
40
backend/tests/Unit/Set/UseCases/ListSetsTest.php
Normal file
40
backend/tests/Unit/Set/UseCases/ListSetsTest.php
Normal 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),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue