two type hints introduced earlier on this branch referenced classes by their fully-qualified names inline. hoist them to the top-of-file use block per backend-context.md PHP rules.
296 lines
8.5 KiB
PHP
296 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\e2e\Controllers;
|
|
|
|
use App\Text\CreateTextDto;
|
|
use App\Text\TextController;
|
|
use App\Text\UseCases\CreateText;
|
|
use App\User\UseCases\CreateUserDto;
|
|
use App\User\User;
|
|
use App\ValueObjects\EmailAddress;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Slim\Psr7\Factory\ServerRequestFactory;
|
|
use Slim\Psr7\Response;
|
|
use Tests\Fakes\FakeNodeRepository;
|
|
use Tests\Fakes\FakeTextRepository;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
|
|
class TextControllerTest extends TestCase
|
|
{
|
|
private FakeTextRepository $textRepo;
|
|
|
|
private FakeUserRepository $userRepo;
|
|
|
|
private TextController $controller;
|
|
|
|
private User $user;
|
|
|
|
private User $otherUser;
|
|
|
|
private User $admin;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->textRepo = new FakeTextRepository();
|
|
$this->userRepo = new FakeUserRepository();
|
|
$this->user = $this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('a@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
));
|
|
$this->otherUser = $this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('other@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
));
|
|
$this->admin = $this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('admin@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: true,
|
|
));
|
|
$this->textRepo->create(new CreateTextDto(
|
|
name: 'test text',
|
|
user: $this->user,
|
|
));
|
|
$this->controller = new TextController($this->textRepo);
|
|
}
|
|
|
|
private function makeRequest(?User $user): ServerRequestInterface
|
|
{
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('GET', 'http://localhost/texts');
|
|
if ($user !== null) {
|
|
$request = $request->withAttribute('user', $user);
|
|
}
|
|
return $request;
|
|
}
|
|
|
|
public function test_get_one_text(): void
|
|
{
|
|
$response = $this->controller->getText(
|
|
$this->makeRequest($this->user),
|
|
new Response(),
|
|
0,
|
|
);
|
|
$this->assertEquals(
|
|
json_encode([
|
|
'id' => 0,
|
|
'name' => 'test text',
|
|
]),
|
|
$response->getBody()
|
|
);
|
|
}
|
|
|
|
public function test_get_text_returns_404_when_not_found(): void
|
|
{
|
|
$response = $this->controller->getText(
|
|
$this->makeRequest($this->user),
|
|
new Response(),
|
|
99,
|
|
);
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_get_text_returns_403_when_not_owner(): void
|
|
{
|
|
$response = $this->controller->getText(
|
|
$this->makeRequest($this->otherUser),
|
|
new Response(),
|
|
0,
|
|
);
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_get_text_allows_admin_to_read_any_text(): void
|
|
{
|
|
$response = $this->controller->getText(
|
|
$this->makeRequest($this->admin),
|
|
new Response(),
|
|
0,
|
|
);
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
$this->assertEquals(
|
|
json_encode([
|
|
'id' => 0,
|
|
'name' => 'test text',
|
|
]),
|
|
$response->getBody()
|
|
);
|
|
}
|
|
|
|
public function test_get_all_texts_returns_every_text(): void
|
|
{
|
|
$this->textRepo->create(new CreateTextDto(
|
|
name: 'other users text',
|
|
user: $this->otherUser,
|
|
));
|
|
$response = $this->controller->getAllTexts(new Response());
|
|
$this->assertEquals(
|
|
json_encode([
|
|
[
|
|
'id' => 0,
|
|
'name' => 'test text',
|
|
],
|
|
[
|
|
'id' => 1,
|
|
'name' => 'other users text',
|
|
],
|
|
]),
|
|
$response->getBody()
|
|
);
|
|
}
|
|
|
|
public function test_get_my_texts_returns_only_own_texts(): void
|
|
{
|
|
$this->textRepo->create(new CreateTextDto(
|
|
name: 'other users text',
|
|
user: $this->otherUser,
|
|
));
|
|
$this->textRepo->create(new CreateTextDto(
|
|
name: 'second of mine',
|
|
user: $this->user,
|
|
));
|
|
$response = $this->controller->getMyTexts(
|
|
$this->makeRequest($this->user),
|
|
new Response(),
|
|
);
|
|
$this->assertEquals(
|
|
json_encode([
|
|
[
|
|
'id' => 0,
|
|
'name' => 'test text',
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'name' => 'second of mine',
|
|
],
|
|
]),
|
|
$response->getBody()
|
|
);
|
|
}
|
|
|
|
public function test_get_my_texts_returns_empty_when_user_has_none(): void
|
|
{
|
|
$response = $this->controller->getMyTexts(
|
|
$this->makeRequest($this->otherUser),
|
|
new Response(),
|
|
);
|
|
$this->assertEquals(json_encode([]), $response->getBody());
|
|
}
|
|
|
|
public function test_create_text(): void
|
|
{
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/texts')
|
|
->withParsedBody(['name' => 'my new text'])
|
|
->withAttribute('user', $this->user);
|
|
|
|
$response = $this->controller->createText(
|
|
$request,
|
|
new Response(),
|
|
new CreateText(
|
|
$this->textRepo,
|
|
new FakeNodeRepository(),
|
|
),
|
|
);
|
|
$this->assertEquals(
|
|
json_encode([
|
|
'id' => 1,
|
|
'name' => 'my new text',
|
|
]),
|
|
$response->getBody()
|
|
);
|
|
}
|
|
|
|
public function test_create_text_returns_400_when_name_missing(): void
|
|
{
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/texts')
|
|
->withParsedBody([])
|
|
->withAttribute('user', $this->user);
|
|
|
|
$response = $this->controller->createText(
|
|
$request,
|
|
new Response(),
|
|
new CreateText(
|
|
$this->textRepo,
|
|
new FakeNodeRepository(),
|
|
),
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_text_persists_user_from_session(): void
|
|
{
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/texts')
|
|
->withParsedBody(['name' => 'my new text'])
|
|
->withAttribute('user', $this->user);
|
|
|
|
$this->controller->createText(
|
|
$request,
|
|
new Response(),
|
|
new CreateText(
|
|
$this->textRepo,
|
|
new FakeNodeRepository(),
|
|
),
|
|
);
|
|
|
|
$stored = $this->textRepo->find(1);
|
|
$this->assertNotNull($stored);
|
|
$this->assertEquals(
|
|
$this->user->getId(),
|
|
$stored->getUser()->getId()
|
|
);
|
|
}
|
|
|
|
public function test_create_text_ignores_user_id_in_body(): void
|
|
{
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/texts')
|
|
->withParsedBody([
|
|
'name' => 'my new text',
|
|
'userId' => $this->otherUser->getId(),
|
|
])
|
|
->withAttribute('user', $this->user);
|
|
|
|
$this->controller->createText(
|
|
$request,
|
|
new Response(),
|
|
new CreateText(
|
|
$this->textRepo,
|
|
new FakeNodeRepository(),
|
|
),
|
|
);
|
|
|
|
$stored = $this->textRepo->find(1);
|
|
$this->assertEquals(
|
|
$this->user->getId(),
|
|
$stored->getUser()->getId()
|
|
);
|
|
}
|
|
|
|
public function test_create_text_returns_401_when_unauthenticated(): void
|
|
{
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/texts')
|
|
->withParsedBody(['name' => 'my new text']);
|
|
|
|
$response = $this->controller->createText(
|
|
$request,
|
|
new Response(),
|
|
new CreateText(
|
|
$this->textRepo,
|
|
new FakeNodeRepository(),
|
|
),
|
|
);
|
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
}
|