update plan controller tests for auth

This commit is contained in:
Yisroel Baum 2026-04-24 13:31:44 +03:00
parent c649dbbcc2
commit 05374991c5
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -8,6 +8,7 @@ use App\Plan\UseCases\CreatePlan;
use App\ScheduledNode\UseCases\CreateScheduledNode; use App\ScheduledNode\UseCases\CreateScheduledNode;
use App\Text\CreateTextDto; use App\Text\CreateTextDto;
use App\User\UseCases\CreateUserDto; use App\User\UseCases\CreateUserDto;
use App\User\User;
use App\ValueObjects\EmailAddress; use App\ValueObjects\EmailAddress;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
@ -29,6 +30,7 @@ class PlanControllerTest extends TestCase
private FakeScheduledNodeRepository $scheduledNodeRepo; private FakeScheduledNodeRepository $scheduledNodeRepo;
private CreatePlan $createPlan; private CreatePlan $createPlan;
private PlanController $controller; private PlanController $controller;
private User $user;
public function setUp(): void public function setUp(): void
{ {
@ -38,7 +40,7 @@ class PlanControllerTest extends TestCase
$this->nodeRepo = new FakeNodeRepository(); $this->nodeRepo = new FakeNodeRepository();
$this->scheduledNodeRepo = new FakeScheduledNodeRepository(); $this->scheduledNodeRepo = new FakeScheduledNodeRepository();
$this->userRepo->create(new CreateUserDto( $this->user = $this->userRepo->create(new CreateUserDto(
email: new EmailAddress('test@test.com'), email: new EmailAddress('test@test.com'),
passwordHash: '', passwordHash: '',
)); ));
@ -69,6 +71,7 @@ class PlanControllerTest extends TestCase
return new ServerRequestFactory() return new ServerRequestFactory()
->createServerRequest('POST', 'http://localhost/api/plans') ->createServerRequest('POST', 'http://localhost/api/plans')
->withHeader('Content-Type', 'application/json') ->withHeader('Content-Type', 'application/json')
->withAttribute('user', $this->user)
->withBody($body); ->withBody($body);
} }
@ -76,7 +79,6 @@ class PlanControllerTest extends TestCase
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'name' => 'My Plan', 'name' => 'My Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
@ -92,29 +94,33 @@ class PlanControllerTest extends TestCase
$this->assertEquals('My Plan', $body['name']); $this->assertEquals('My Plan', $body['name']);
} }
public function test_create_plan_returns_400_when_user_id_missing(): void public function test_create_plan_returns_401_when_no_user(): void
{ {
$response = $this->controller->createPlan( $requestWithoutUser = new ServerRequestFactory()
$this->makeRequest([ ->createServerRequest('POST', 'http://localhost/api/plans')
->withHeader('Content-Type', 'application/json')
->withBody(
new StreamFactory()->createStream(json_encode([
'textId' => 0, 'textId' => 0,
'name' => 'My Plan', 'name' => 'My Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
'dateEnd' => '2025-01-01', 'dateEnd' => '2025-01-01',
]), ]))
);
$response = $this->controller->createPlan(
$requestWithoutUser,
new Response(), new Response(),
$this->createPlan, $this->createPlan,
); );
$this->assertEquals(400, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
$body = json_decode($response->getBody(), true);
$this->assertArrayHasKey('error', $body);
} }
public function test_create_plan_returns_400_when_text_id_missing(): void public function test_create_plan_returns_400_when_text_id_missing(): void
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'name' => 'My Plan', 'name' => 'My Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
'dateEnd' => '2025-01-01', 'dateEnd' => '2025-01-01',
@ -132,7 +138,6 @@ class PlanControllerTest extends TestCase
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
'dateEnd' => '2025-01-01', 'dateEnd' => '2025-01-01',
@ -150,7 +155,6 @@ class PlanControllerTest extends TestCase
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'name' => 'My Plan', 'name' => 'My Plan',
'dateEnd' => '2025-01-01', 'dateEnd' => '2025-01-01',
@ -168,7 +172,6 @@ class PlanControllerTest extends TestCase
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'name' => 'My Plan', 'name' => 'My Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
@ -186,7 +189,6 @@ class PlanControllerTest extends TestCase
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'name' => 'My Plan', 'name' => 'My Plan',
'dateStart' => '2025-01-02', 'dateStart' => '2025-01-02',
@ -201,30 +203,10 @@ class PlanControllerTest extends TestCase
$this->assertArrayHasKey('error', $body); $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);
}
public function test_create_plan_returns_404_when_text_not_found(): void public function test_create_plan_returns_404_when_text_not_found(): void
{ {
$response = $this->controller->createPlan( $response = $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 99, 'textId' => 99,
'name' => 'My Plan', 'name' => 'My Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
@ -243,7 +225,6 @@ class PlanControllerTest extends TestCase
{ {
$this->controller->createPlan( $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'name' => 'Persistent Plan', 'name' => 'Persistent Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',
@ -262,7 +243,6 @@ class PlanControllerTest extends TestCase
{ {
$this->controller->createPlan( $this->controller->createPlan(
$this->makeRequest([ $this->makeRequest([
'userId' => 0,
'textId' => 0, 'textId' => 0,
'name' => 'Scheduling Plan', 'name' => 'Scheduling Plan',
'dateStart' => '2025-01-01', 'dateStart' => '2025-01-01',