218 lines
6.5 KiB
PHP
218 lines
6.5 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 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;
|
|
|
|
class BulkCreateNodesControllerTest extends TestCase
|
|
{
|
|
private FakeTextRepository $textRepo;
|
|
private FakeNodeRepository $nodeRepo;
|
|
private BulkCreateNodes $useCase;
|
|
private NodeController $controller;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->textRepo = new FakeTextRepository;
|
|
$text = $this->textRepo->create(new CreateTextDto(name: 'test text'));
|
|
|
|
$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): ServerRequestInterface
|
|
{
|
|
$body = (new StreamFactory())->createStream(json_encode($data));
|
|
return (new ServerRequestFactory())
|
|
->createServerRequest('POST', 'http://localhost/api/nodes/bulk')
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withBody($body);
|
|
}
|
|
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
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',
|
|
]),
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
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,
|
|
]),
|
|
new Response(),
|
|
$this->useCase,
|
|
);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
}
|
|
}
|