315 lines
9.2 KiB
PHP
315 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\e2e\Controllers;
|
|
|
|
use App\Node\CreateNodeDto;
|
|
use App\Node\NodeController;
|
|
use App\Node\UseCases\BulkCreateNodes;
|
|
use App\Text\CreateTextDto;
|
|
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\Factory\StreamFactory;
|
|
use Slim\Psr7\Response;
|
|
use Tests\Fakes\FakeNodeRepository;
|
|
use Tests\Fakes\FakeTextRepository;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
|
|
class BulkCreateNodesControllerTest extends TestCase
|
|
{
|
|
private FakeTextRepository $textRepo;
|
|
private FakeNodeRepository $nodeRepo;
|
|
private BulkCreateNodes $useCase;
|
|
private NodeController $controller;
|
|
private User $user;
|
|
private User $otherUser;
|
|
private User $admin;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$userRepo = new FakeUserRepository();
|
|
$this->user = $userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('a@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
));
|
|
$this->otherUser = $userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('other@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
));
|
|
$this->admin = $userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('admin@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: true,
|
|
));
|
|
$this->textRepo = new FakeTextRepository();
|
|
$text = $this->textRepo->create(new CreateTextDto(
|
|
name: 'test text',
|
|
user: $this->user,
|
|
));
|
|
|
|
$this->nodeRepo = new FakeNodeRepository();
|
|
$this->nodeRepo->create(new CreateNodeDto(
|
|
text: $text,
|
|
title: 'Root Node',
|
|
parentNode: null,
|
|
));
|
|
$this->useCase = new BulkCreateNodes(
|
|
$this->nodeRepo,
|
|
$this->textRepo
|
|
);
|
|
$this->controller = new NodeController(
|
|
$this->nodeRepo,
|
|
$this->textRepo
|
|
);
|
|
}
|
|
|
|
private function makeRequest(
|
|
array $data,
|
|
User $user,
|
|
): ServerRequestInterface {
|
|
$body = new StreamFactory()->createStream(json_encode($data));
|
|
$request = new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/api/nodes/bulk')
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withBody($body);
|
|
return $request->withAttribute('user', $user);
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_201_with_created_nodes(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 3,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(201, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertIsArray($body);
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_correct_count(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 10,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertCount(10, $body);
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_correct_titles(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Chapter',
|
|
'count' => 3,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertEquals('Chapter 1', $body[0]['title']);
|
|
$this->assertEquals('Chapter 2', $body[1]['title']);
|
|
$this->assertEquals('Chapter 3', $body[2]['title']);
|
|
}
|
|
|
|
public function test_bulk_create_nodes_response_includes_id_and_parent_node_id(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 2,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('id', $body[0]);
|
|
$this->assertEquals(0, $body[0]['parentNodeId']);
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_400_when_title_prefix_missing(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'count' => 5,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_400_when_count_is_zero(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 0,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_400_when_count_is_missing(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_400_when_parent_node_id_missing(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 5,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_404_when_text_not_found(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 99,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 5,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_404_when_parent_node_not_found(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 99,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 5,
|
|
],
|
|
$this->user,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_returns_403_when_not_owner(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 3,
|
|
],
|
|
$this->otherUser,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
}
|
|
|
|
public function test_bulk_create_nodes_allows_admin_on_any_text(): void
|
|
{
|
|
$response = $this->controller->bulkCreateNodes(
|
|
$this->makeRequest(
|
|
[
|
|
'textId' => 0,
|
|
'parentNodeId' => 0,
|
|
'titlePrefix' => 'Page',
|
|
'count' => 2,
|
|
],
|
|
$this->admin,
|
|
),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(201, $response->getStatusCode());
|
|
}
|
|
}
|