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); } public function test_get_one_text(): void { $response = $this->controller->getText( new Response(), 0, ); $this->assertEquals( json_encode([ 'id' => 0, 'name' => 'test text', ]), $response->getBody() ); } public function test_get_all_texts(): void { $this->textRepo->create(new CreateTextDto( name: 'test text 2', user: $this->user, )); $response = $this->controller->getTexts(new Response()); $this->assertEquals( json_encode([ [ 'id' => 0, 'name' => 'test text', ], [ 'id' => 1, 'name' => 'test text 2', ], ]), $response->getBody() ); } public function test_create_text(): void { $request = new ServerRequestFactory() ->createServerRequest('POST', 'http://localhost/texts') ->withParsedBody(['name' => 'my new text']) ->withAttribute('user', $this->user); $response = $this->controller->createText( $request, new Response(), new CreateText( $this->textRepo, new FakeNodeRepository(), ), ); $this->assertEquals( json_encode([ 'id' => 1, 'name' => 'my new text', ]), $response->getBody() ); } public function test_create_text_returns_400_when_name_missing(): void { $request = new ServerRequestFactory() ->createServerRequest('POST', 'http://localhost/texts') ->withParsedBody([]) ->withAttribute('user', $this->user); $response = $this->controller->createText( $request, new Response(), new CreateText( $this->textRepo, new FakeNodeRepository(), ), ); $this->assertEquals(400, $response->getStatusCode()); $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); } }