test set layout browsing
This commit is contained in:
parent
936855efab
commit
eb7ac7d261
8 changed files with 619 additions and 3 deletions
171
backend/tests/Feature/Set/GetSetLayoutEndpointTest.php
Normal file
171
backend/tests/Feature/Set/GetSetLayoutEndpointTest.php
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
<?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 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): \Illuminate\Testing\TestResponse
|
||||
{
|
||||
return $this->withCredentials()
|
||||
->withUnencryptedCookie(
|
||||
AuthMiddleware::COOKIE_NAME,
|
||||
'valid-token',
|
||||
)->getJson($uri);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue