Attainly/backend/tests/Feature/Set/GetSetLayoutEndpointTest.php
2026-08-08 23:37:52 +03:00

172 lines
5 KiB
PHP

<?php
namespace Tests\Feature\Set;
use App\Auth\CreateSessionDto;
use App\Auth\SessionRepository;
use App\Element\CreateElementDto;
use App\Element\ElementRepository;
use App\Http\Middleware\AuthMiddleware;
use App\Set\CreateSetDto;
use App\Set\Set;
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 Illuminate\Testing\TestResponse;
use Tests\TestCase;
class GetSetLayoutEndpointTest extends TestCase
{
use RefreshDatabase;
public function test_it_returns_a_sets_ordered_element_layout(): void
{
$user = $this->createUser();
$set = $this->createSet($user, 'Bible');
$repository = app(ElementRepository::class);
$genesis = $repository->create(new CreateElementDto(
set: $set,
name: 'Genesis',
kind: 'book',
parentElement: null,
));
$repository->create(new CreateElementDto(
set: $set,
name: 'Exodus',
kind: 'book',
parentElement: null,
));
$creation = $repository->create(new CreateElementDto(
set: $set,
name: 'Creation',
kind: 'portion',
parentElement: $genesis,
));
$chapter = $repository->create(new CreateElementDto(
set: $set,
name: 'Chapter 1',
kind: 'chapter',
parentElement: $creation,
));
$this->createSession($user);
$response = $this->credentialedGet("/api/sets/{$set->getId()}");
$response->assertOk()->assertExactJson([
'set' => [
'id' => $set->getId(),
'name' => 'Bible',
],
'elements' => [
[
'id' => $genesis->getId(),
'name' => 'Genesis',
'kind' => 'book',
'children' => [
[
'id' => $creation->getId(),
'name' => 'Creation',
'kind' => 'portion',
'children' => [
[
'id' => $chapter->getId(),
'name' => 'Chapter 1',
'kind' => 'chapter',
'children' => [],
],
],
],
],
],
[
'id' => $genesis->getId() + 1,
'name' => 'Exodus',
'kind' => 'book',
'children' => [],
],
],
]);
}
public function test_it_returns_an_empty_element_layout(): void
{
$user = $this->createUser();
$set = $this->createSet($user, 'Empty set');
$this->createSession($user);
$response = $this->credentialedGet("/api/sets/{$set->getId()}");
$response->assertOk()->assertExactJson([
'set' => [
'id' => $set->getId(),
'name' => 'Empty set',
],
'elements' => [],
]);
}
public function test_it_returns_not_found_for_an_unknown_set(): void
{
$user = $this->createUser();
$this->createSession($user);
$response = $this->credentialedGet('/api/sets/999');
$response->assertNotFound()->assertExactJson([
'error' => 'set not found',
]);
}
public function test_it_rejects_an_unauthenticated_request(): void
{
$response = $this->getJson('/api/sets/1');
$response->assertStatus(401)->assertExactJson([
'error' => 'unauthenticated',
]);
}
private function createUser(): User
{
return app(UserRepository::class)->create(new CreateUserDto(
email: new EmailAddress('reader@example.com'),
passwordHash: 'hashed-password',
));
}
private function createSet(User $user, string $name): Set
{
return app(SetRepository::class)->create(new CreateSetDto(
name: $name,
creator: $user,
));
}
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'),
));
}
private function credentialedGet(string $uri): TestResponse
{
return $this->withCredentials()
->withUnencryptedCookie(
AuthMiddleware::COOKIE_NAME,
'valid-token',
)->getJson($uri);
}
}