From 275a8364643ac66e1c5155d2b484a23820f89597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Touz=C3=A9?= Date: Sat, 23 May 2020 16:00:31 +0200 Subject: [PATCH] Add tests for Pool, some are not fully working. Fix a crash on new_poll when request is incorrect. --- src/Controller/PollController.php | 7 ++++- tests/Functional/PollControllerTest.php | 37 ++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 31ff4a3..5a3355a 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -10,6 +10,7 @@ use FOS\RestBundle\Controller\Annotations\Get; use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Put; use FOS\RestBundle\Controller\Annotations\Route; +use JMS\Serializer\Exception\RuntimeException; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerInterface; use Swift_Mailer; @@ -142,7 +143,11 @@ class PollController extends FramadateController { $data = $request->getContent(); $serializer = SerializerBuilder::create()->build(); - $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); + try { + $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); + } catch(RuntimeException $e) { + return $this->json(["message" => "Incorrect JSON in request"], 400); + } $newpoll ->setAdminKey( $newpoll->generateAdminKey() ) ->setCreationDate( new DateTime() ) diff --git a/tests/Functional/PollControllerTest.php b/tests/Functional/PollControllerTest.php index 577a192..1efe4a1 100644 --- a/tests/Functional/PollControllerTest.php +++ b/tests/Functional/PollControllerTest.php @@ -123,8 +123,43 @@ class PollControllerTest extends WebTestCase { // Test Post new poll public function testPostOnePoll() { - //TODO + $client = static::createClient(); + $this->loadFixtures(array( + 'App\DataFixtures\AppPollFixtures' + )); + + $client->request('POST', '/api/v1/poll/', [ + 'json' => [ + "title" => "Fromage ou dessert ? ", + "description" => "Votre plat préféré", + "creation_date" => "2048-04-25T16:19:48+02:00", + "expiracy_date" => "2048-04-25T16:19:48+02:00", + "kind" => "text", + "allowed_answers" => [ + "yes" + ], + "modification_policy" => "nobody", + "mail_on_vote" => true, + "choices" => [ + [ + "id" => 1, + "name" => "fromage" + ], + [ + "id" => 2, + "name" => "dessert" + ] + ], + "default_expiracy_days_from_now" => 365 + ] + ]); + + $response = $client->getResponse(); + $this->assertEquals(201, $response->getStatusCode()); + + $data = $response->getContent(); + $this->assertIsNumeric($data['poll']['id']); } }