diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index 18765d3..b2ef502 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -27,7 +27,7 @@ class CommentController extends FramadateController { * @Get( * path = "/poll/{id}/comments", * name = "get_poll_comment", - * requirements = {"poll_id"="\d+"} + * requirements = {"id"="\d+"} * ) */ public @@ -52,7 +52,7 @@ class CommentController extends FramadateController { * @Post( * path = "/poll/{id}/comment", * name = "new_comment", - * requirements = {"content"="\w+", "poll_id"="\d+"} + * requirements = {"content"="\w+", "id"="\d+"} * ) */ public @@ -71,13 +71,16 @@ class CommentController extends FramadateController { $em = $this->getDoctrine()->getRepository( Owner::class ); $data = json_decode( $data, true ); + if(!isset($data['email'])) { + return $this->json(["message" => "Incorrect JSON in request"], 400); + } $foundOwner = $em->findOneByEmail( $data[ 'email' ] ); // manage existing or new Owner if ( ! $foundOwner ) { $foundOwner = new Owner(); - $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) - ->setEmail( $data[ 'owner' ][ 'email' ] ) + $foundOwner->setPseudo( $data[ 'email' ] ) + ->setEmail( $data[ 'email' ] ) ->setModifierToken( uniqid( '', true ) ); } // anti flood @@ -142,7 +145,7 @@ class CommentController extends FramadateController { * @Delete( * path = "/poll/{id}/comments", * name = "poll_comments_delete", - * requirements = {"accessToken"="\w+", "poll_id"="\d+"} + * requirements = {"accessToken"="\w+", "id"="\d+"} * ) * * @param Poll $poll diff --git a/tests/Functional/CommentControllerTest.php b/tests/Functional/CommentControllerTest.php new file mode 100644 index 0000000..cd5eda9 --- /dev/null +++ b/tests/Functional/CommentControllerTest.php @@ -0,0 +1,76 @@ +loadFixtures(array( + 'App\DataFixtures\AppPollFixtures', + 'App\DataFixtures\CommentFixtures', + )); + + $client->request('GET', '/api/v1/poll/1/comments'); + + $response = $client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + $body = $response->getContent(); + $json = json_decode($body, true); + $this->assertEquals(5, count($json['data'])); + + } + + public function testNewComment() { + $client = static::createClient(); + + $this->loadFixtures(array( + 'App\DataFixtures\AppPollFixtures', + 'App\DataFixtures\CommentFixtures', + )); + + $data = [ + 'text' => "Mon nouveau commentaire de test !", + 'email' => "email@host.plop" + ]; + + $client->request('POST', '/api/v1/poll/1/comment', [ + 'body' => json_encode($data) + ], + [], + [ + 'CONTENT_TYPE' => 'application/json', + 'HTTP_ACCEPT' => 'application/json', + ], + json_encode($data) + ); + + $response = $client->getResponse(); + $this->assertEquals(201, $response->getStatusCode()); + $body = $response->getContent(); + $json = json_decode($body, true); + $this->assertEquals("email@host.plop", $json['data']['your_comment']['pseudo']); + } + + public function testDeleteComments() { + $client = static::createClient(); + + $this->loadFixtures(array( + 'App\DataFixtures\AppPollFixtures', + 'App\DataFixtures\CommentFixtures', + )); + + + $client->request('DELETE', '/api/v1/poll/1/comments'); + $response = $client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + + } + +}