diff --git a/tests/Unit/Text/UseCases/CreateTextTest.php b/tests/Unit/Text/UseCases/CreateTextTest.php index 279d6ab..bcc61a1 100644 --- a/tests/Unit/Text/UseCases/CreateTextTest.php +++ b/tests/Unit/Text/UseCases/CreateTextTest.php @@ -7,22 +7,36 @@ use App\Text\Text; use App\Text\TextRepository; use App\Text\UseCases\CreateText; use App\Text\UseCases\CreateTextRequest; +use App\User\UseCases\CreateUserDto; +use App\User\User; +use App\ValueObjects\EmailAddress; use PHPUnit\Framework\TestCase; use Tests\Fakes\FakeNodeRepository; use Tests\Fakes\FakeTextRepository; +use Tests\Fakes\FakeUserRepository; class CreateTextTest extends TestCase { private FakeTextRepository $textRepo; + private FakeUserRepository $userRepo; + private FakeNodeRepository $nodeRepo; private CreateText $useCase; + private User $user; + protected function setUp(): void { $this->textRepo = new FakeTextRepository(); $this->nodeRepo = new FakeNodeRepository(); + $this->userRepo = new FakeUserRepository(); + $this->user = $this->userRepo->create(new CreateUserDto( + email: new EmailAddress('a@b.com'), + passwordHash: '', + isAdmin: false, + )); $this->useCase = new CreateText( $this->textRepo, $this->nodeRepo, @@ -33,6 +47,7 @@ class CreateTextTest extends TestCase { $text = $this->useCase->execute(new CreateTextRequest( name: 'test', + user: $this->user, )); $this->assertInstanceOf(TextRepository::class, $this->textRepo); $this->assertInstanceOf(Text::class, $text); @@ -43,6 +58,7 @@ class CreateTextTest extends TestCase { $text = $this->useCase->execute(new CreateTextRequest( name: 'my text', + user: $this->user, )); $nodes = $this->nodeRepo->findByTextId($text->getId()); @@ -53,6 +69,17 @@ class CreateTextTest extends TestCase $this->assertNull($rootNode->getParentNode()); } + public function test_text_belongs_to_user(): void + { + $text = $this->useCase->execute(new CreateTextRequest( + name: 'my text', + user: $this->user, + )); + + $this->assertSame($this->user, $text->getUser()); + $this->assertEquals($this->user->getId(), $text->getUser()->getId()); + } + public function test_throws_if_name_is_null(): void { $this->expectException(BadRequestException::class); @@ -60,6 +87,18 @@ class CreateTextTest extends TestCase $this->useCase->execute(new CreateTextRequest( name: null, + user: $this->user, + )); + } + + public function test_throws_if_user_is_null(): void + { + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('user is required'); + + $this->useCase->execute(new CreateTextRequest( + name: 'name', + user: null, )); } } diff --git a/tests/e2e/Controllers/TextControllerTest.php b/tests/e2e/Controllers/TextControllerTest.php index ec5af99..16ddcf1 100644 --- a/tests/e2e/Controllers/TextControllerTest.php +++ b/tests/e2e/Controllers/TextControllerTest.php @@ -5,23 +5,38 @@ namespace Tests\e2e\Controllers; use App\Text\CreateTextDto; use App\Text\TextController; use App\Text\UseCases\CreateText; +use App\User\UseCases\CreateUserDto; +use App\User\User; +use App\ValueObjects\EmailAddress; use PHPUnit\Framework\TestCase; use Slim\Psr7\Factory\ServerRequestFactory; use Slim\Psr7\Response; use Tests\Fakes\FakeNodeRepository; use Tests\Fakes\FakeTextRepository; +use Tests\Fakes\FakeUserRepository; class TextControllerTest extends TestCase { private FakeTextRepository $textRepo; + private FakeUserRepository $userRepo; + private TextController $controller; + private User $user; + public function setUp(): void { $this->textRepo = new FakeTextRepository(); + $this->userRepo = new FakeUserRepository(); + $this->user = $this->userRepo->create(new CreateUserDto( + email: new EmailAddress('a@b.com'), + passwordHash: '', + isAdmin: false, + )); $this->textRepo->create(new CreateTextDto( name: 'test text', + user: $this->user, )); $this->controller = new TextController($this->textRepo); } @@ -45,6 +60,7 @@ class TextControllerTest extends TestCase { $this->textRepo->create(new CreateTextDto( name: 'test text 2', + user: $this->user, )); $response = $this->controller->getTexts(new Response()); $this->assertEquals( @@ -66,7 +82,8 @@ class TextControllerTest extends TestCase { $request = new ServerRequestFactory() ->createServerRequest('POST', 'http://localhost/texts') - ->withParsedBody(['name' => 'my new text']); + ->withParsedBody(['name' => 'my new text']) + ->withAttribute('user', $this->user); $response = $this->controller->createText( $request, @@ -89,7 +106,8 @@ class TextControllerTest extends TestCase { $request = new ServerRequestFactory() ->createServerRequest('POST', 'http://localhost/texts') - ->withParsedBody([]); + ->withParsedBody([]) + ->withAttribute('user', $this->user); $response = $this->controller->createText( $request, @@ -104,4 +122,79 @@ class TextControllerTest extends TestCase $body = json_decode($response->getBody(), true); $this->assertArrayHasKey('error', $body); } + + public function test_create_text_persists_user_from_session(): void + { + $request = new ServerRequestFactory() + ->createServerRequest('POST', 'http://localhost/texts') + ->withParsedBody(['name' => 'my new text']) + ->withAttribute('user', $this->user); + + $this->controller->createText( + $request, + new Response(), + new CreateText( + $this->textRepo, + new FakeNodeRepository(), + ), + ); + + $stored = $this->textRepo->find(1); + $this->assertNotNull($stored); + $this->assertEquals( + $this->user->getId(), + $stored->getUser()->getId() + ); + } + + public function test_create_text_ignores_user_id_in_body(): void + { + $otherUser = $this->userRepo->create(new CreateUserDto( + email: new EmailAddress('other@b.com'), + passwordHash: '', + isAdmin: false, + )); + $request = new ServerRequestFactory() + ->createServerRequest('POST', 'http://localhost/texts') + ->withParsedBody([ + 'name' => 'my new text', + 'userId' => $otherUser->getId(), + ]) + ->withAttribute('user', $this->user); + + $this->controller->createText( + $request, + new Response(), + new CreateText( + $this->textRepo, + new FakeNodeRepository(), + ), + ); + + $stored = $this->textRepo->find(1); + $this->assertEquals( + $this->user->getId(), + $stored->getUser()->getId() + ); + } + + public function test_create_text_returns_401_when_unauthenticated(): void + { + $request = new ServerRequestFactory() + ->createServerRequest('POST', 'http://localhost/texts') + ->withParsedBody(['name' => 'my new text']); + + $response = $this->controller->createText( + $request, + new Response(), + new CreateText( + $this->textRepo, + new FakeNodeRepository(), + ), + ); + + $this->assertEquals(401, $response->getStatusCode()); + $body = json_decode($response->getBody(), true); + $this->assertArrayHasKey('error', $body); + } }