add tests for node controller
get nodes of text and create node
This commit is contained in:
parent
2121a0ba9d
commit
1c1f8b5a58
1 changed files with 152 additions and 0 deletions
152
tests/e2e/Controllers/NodeControllerTest.php
Normal file
152
tests/e2e/Controllers/NodeControllerTest.php
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\e2e\Controllers;
|
||||
|
||||
use App\Node\CreateNodeDto;
|
||||
use App\Node\NodeController;
|
||||
use App\Node\UseCases\CreateNode;
|
||||
use App\Text\CreateTextDto;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Slim\Psr7\Factory\ServerRequestFactory;
|
||||
use Slim\Psr7\Response;
|
||||
use Tests\Fakes\FakeNodeRepository;
|
||||
use Tests\Fakes\FakeTextRepository;
|
||||
|
||||
class NodeControllerTest extends TestCase
|
||||
{
|
||||
private FakeTextRepository $textRepo;
|
||||
private FakeNodeRepository $nodeRepo;
|
||||
private NodeController $controller;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->textRepo = new FakeTextRepository;
|
||||
$this->textRepo->create(new CreateTextDto(name: 'test text'));
|
||||
|
||||
$this->nodeRepo = new FakeNodeRepository;
|
||||
|
||||
$this->controller = new NodeController($this->nodeRepo, $this->textRepo);
|
||||
}
|
||||
|
||||
public function test_get_nodes_of_text_returns_flat_array(): void
|
||||
{
|
||||
$text = $this->textRepo->find(0);
|
||||
$this->nodeRepo->create(new CreateNodeDto(
|
||||
text: $text,
|
||||
title: 'Root Node',
|
||||
parentNode: null,
|
||||
));
|
||||
|
||||
$response = $this->controller->getNodesOfText(new Response(), 0);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals(
|
||||
json_encode([
|
||||
['id' => 0, 'title' => 'Root Node', 'parentNodeId' => null],
|
||||
]),
|
||||
$response->getBody(),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_get_nodes_of_text_returns_empty_array_when_no_nodes(): void
|
||||
{
|
||||
$response = $this->controller->getNodesOfText(new Response(), 0);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals(json_encode([]), $response->getBody());
|
||||
}
|
||||
|
||||
public function test_get_nodes_of_text_returns_404_for_unknown_text(): void
|
||||
{
|
||||
$response = $this->controller->getNodesOfText(new Response(), 99);
|
||||
|
||||
$this->assertEquals(404, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function test_get_nodes_includes_parent_node_id(): void
|
||||
{
|
||||
$text = $this->textRepo->find(0);
|
||||
$rootNode = $this->nodeRepo->create(new CreateNodeDto(
|
||||
text: $text,
|
||||
title: 'Root Node',
|
||||
parentNode: null,
|
||||
));
|
||||
$this->nodeRepo->create(new CreateNodeDto(
|
||||
text: $text,
|
||||
title: 'Child Node',
|
||||
parentNode: $rootNode,
|
||||
));
|
||||
|
||||
$response = $this->controller->getNodesOfText(new Response(), 0);
|
||||
$body = json_decode($response->getBody(), true);
|
||||
|
||||
$this->assertEquals(0, $body[1]['parentNodeId']);
|
||||
}
|
||||
|
||||
public function test_create_node_returns_created_node(): void
|
||||
{
|
||||
$text = $this->textRepo->find(0);
|
||||
$rootNode = $this->nodeRepo->create(new CreateNodeDto(
|
||||
text: $text,
|
||||
title: 'Root Node',
|
||||
parentNode: null,
|
||||
));
|
||||
|
||||
$request = (new ServerRequestFactory())
|
||||
->createServerRequest('POST', 'http://localhost/api/nodes')
|
||||
->withParsedBody([
|
||||
'textId' => 0,
|
||||
'title' => 'Child Node',
|
||||
'parentNodeId' => $rootNode->getId(),
|
||||
]);
|
||||
|
||||
$response = $this->controller->createNode(
|
||||
$request,
|
||||
new Response(),
|
||||
new CreateNode($this->nodeRepo, $this->textRepo),
|
||||
);
|
||||
|
||||
$this->assertEquals(201, $response->getStatusCode());
|
||||
$body = json_decode($response->getBody(), true);
|
||||
$this->assertEquals('Child Node', $body['title']);
|
||||
$this->assertEquals($rootNode->getId(), $body['parentNodeId']);
|
||||
$this->assertArrayHasKey('id', $body);
|
||||
}
|
||||
|
||||
public function test_create_node_returns_400_when_title_missing(): void
|
||||
{
|
||||
$request = (new ServerRequestFactory())
|
||||
->createServerRequest('POST', 'http://localhost/api/nodes')
|
||||
->withParsedBody([
|
||||
'textId' => 0,
|
||||
'parentNodeId' => null,
|
||||
]);
|
||||
|
||||
$response = $this->controller->createNode(
|
||||
$request,
|
||||
new Response(),
|
||||
new CreateNode($this->nodeRepo, $this->textRepo),
|
||||
);
|
||||
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function test_create_node_returns_404_when_text_not_found(): void
|
||||
{
|
||||
$request = (new ServerRequestFactory())
|
||||
->createServerRequest('POST', 'http://localhost/api/nodes')
|
||||
->withParsedBody([
|
||||
'textId' => 99,
|
||||
'title' => 'Some Node',
|
||||
'parentNodeId' => null,
|
||||
]);
|
||||
|
||||
$response = $this->controller->createNode(
|
||||
$request,
|
||||
new Response(),
|
||||
new CreateNode($this->nodeRepo, $this->textRepo),
|
||||
);
|
||||
|
||||
$this->assertEquals(404, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue