221 lines
7.1 KiB
PHP
221 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\e2e\Controllers;
|
|
|
|
use App\Node\CreateNodeDto;
|
|
use App\Plan\PlanController;
|
|
use App\Plan\UseCases\CreatePlan;
|
|
use App\ScheduledNode\UseCases\CreateScheduledNode;
|
|
use App\Text\CreateTextDto;
|
|
use App\User\UseCases\CreateUserDto;
|
|
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\FakePlanRepository;
|
|
use Tests\Fakes\FakeScheduledNodeRepository;
|
|
use Tests\Fakes\FakeTextRepository;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
|
|
class PlanControllerTest extends TestCase
|
|
{
|
|
private FakePlanRepository $planRepo;
|
|
private FakeUserRepository $userRepo;
|
|
private FakeTextRepository $textRepo;
|
|
private FakeNodeRepository $nodeRepo;
|
|
private FakeScheduledNodeRepository $scheduledNodeRepo;
|
|
private CreatePlan $createPlan;
|
|
private PlanController $controller;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->planRepo = new FakePlanRepository();
|
|
$this->userRepo = new FakeUserRepository();
|
|
$this->textRepo = new FakeTextRepository();
|
|
$this->nodeRepo = new FakeNodeRepository();
|
|
$this->scheduledNodeRepo = new FakeScheduledNodeRepository();
|
|
|
|
$this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('test@test.com'),
|
|
));
|
|
$text = $this->textRepo->create(new CreateTextDto('testname'));
|
|
$this->nodeRepo->create(new CreateNodeDto(
|
|
text: $text,
|
|
title: 'Root Node',
|
|
parentNode: null,
|
|
));
|
|
|
|
$createScheduledNode = new CreateScheduledNode(
|
|
scheduledNodeRepo: $this->scheduledNodeRepo,
|
|
planRepo: $this->planRepo,
|
|
);
|
|
$this->createPlan = new CreatePlan(
|
|
$this->planRepo,
|
|
$this->userRepo,
|
|
$this->textRepo,
|
|
$this->nodeRepo,
|
|
$createScheduledNode,
|
|
);
|
|
$this->controller = new PlanController();
|
|
}
|
|
|
|
private function makeRequest(array $data): ServerRequestInterface
|
|
{
|
|
$body = new StreamFactory()->createStream(json_encode($data));
|
|
return new ServerRequestFactory()
|
|
->createServerRequest('POST', 'http://localhost/api/plans')
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withBody($body);
|
|
}
|
|
|
|
public function test_create_plan_returns_201_with_id_and_name(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 0,
|
|
'textId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateStart' => '2025-01-01',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(201, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('id', $body);
|
|
$this->assertEquals('My Plan', $body['name']);
|
|
}
|
|
|
|
public function test_create_plan_returns_400_when_user_id_missing(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'textId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateStart' => '2025-01-01',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_plan_returns_400_when_text_id_missing(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateStart' => '2025-01-01',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_plan_returns_400_when_name_missing(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 0,
|
|
'textId' => 0,
|
|
'dateStart' => '2025-01-01',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_plan_returns_400_when_date_start_missing(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 0,
|
|
'textId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_plan_returns_400_when_date_end_missing(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 0,
|
|
'textId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateStart' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_plan_returns_400_when_date_end_before_start(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 0,
|
|
'textId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateStart' => '2025-01-02',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
|
|
public function test_create_plan_returns_404_when_user_not_found(): void
|
|
{
|
|
$response = $this->controller->createPlan(
|
|
$this->makeRequest([
|
|
'userId' => 99,
|
|
'textId' => 0,
|
|
'name' => 'My Plan',
|
|
'dateStart' => '2025-01-01',
|
|
'dateEnd' => '2025-01-01',
|
|
]),
|
|
new Response(),
|
|
$this->createPlan,
|
|
);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertArrayHasKey('error', $body);
|
|
}
|
|
}
|