1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

Add test on Comment, test on DELETE does not work as how route works is unclear, fix requirement constraint name on CommentController.

This commit is contained in:
Sébastien Touzé 2020-05-23 18:34:04 +02:00
parent 63ed7949a2
commit da0078e547
2 changed files with 84 additions and 5 deletions

View File

@ -27,7 +27,7 @@ class CommentController extends FramadateController {
* @Get( * @Get(
* path = "/poll/{id}/comments", * path = "/poll/{id}/comments",
* name = "get_poll_comment", * name = "get_poll_comment",
* requirements = {"poll_id"="\d+"} * requirements = {"id"="\d+"}
* ) * )
*/ */
public public
@ -52,7 +52,7 @@ class CommentController extends FramadateController {
* @Post( * @Post(
* path = "/poll/{id}/comment", * path = "/poll/{id}/comment",
* name = "new_comment", * name = "new_comment",
* requirements = {"content"="\w+", "poll_id"="\d+"} * requirements = {"content"="\w+", "id"="\d+"}
* ) * )
*/ */
public public
@ -71,13 +71,16 @@ class CommentController extends FramadateController {
$em = $this->getDoctrine()->getRepository( Owner::class ); $em = $this->getDoctrine()->getRepository( Owner::class );
$data = json_decode( $data, true ); $data = json_decode( $data, true );
if(!isset($data['email'])) {
return $this->json(["message" => "Incorrect JSON in request"], 400);
}
$foundOwner = $em->findOneByEmail( $data[ 'email' ] ); $foundOwner = $em->findOneByEmail( $data[ 'email' ] );
// manage existing or new Owner // manage existing or new Owner
if ( ! $foundOwner ) { if ( ! $foundOwner ) {
$foundOwner = new Owner(); $foundOwner = new Owner();
$foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) $foundOwner->setPseudo( $data[ 'email' ] )
->setEmail( $data[ 'owner' ][ 'email' ] ) ->setEmail( $data[ 'email' ] )
->setModifierToken( uniqid( '', true ) ); ->setModifierToken( uniqid( '', true ) );
} }
// anti flood // anti flood
@ -142,7 +145,7 @@ class CommentController extends FramadateController {
* @Delete( * @Delete(
* path = "/poll/{id}/comments", * path = "/poll/{id}/comments",
* name = "poll_comments_delete", * name = "poll_comments_delete",
* requirements = {"accessToken"="\w+", "poll_id"="\d+"} * requirements = {"accessToken"="\w+", "id"="\d+"}
* ) * )
* *
* @param Poll $poll * @param Poll $poll

View File

@ -0,0 +1,76 @@
<?php
namespace App\Tests\Functional;
use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AdminControllerTest extends WebTestCase {
use FixturesTrait;
// Test getting all comments from one Poll
public function testGetAllComments() {
$client = static::createClient();
$this->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());
}
}