Add tests for Pool, some are not fully working. Fix a crash on new_poll when request is incorrect.

This commit is contained in:
Sébastien Touzé 2020-05-23 16:00:31 +02:00
parent bbad384474
commit 275a836464
2 changed files with 42 additions and 2 deletions

View File

@ -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() )

View File

@ -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']);
}
}