Skip to content

Commit

Permalink
VSVGVQ-11 Add unit test for QuestionController.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc Wollants committed May 23, 2018
1 parent 6741dd0 commit 92a5575
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Question/Controllers/QuestionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function save(Request $request): Response
return $response;
}

/**
* @return Response
*/
public function getAll(): Response
{
$questions = $this->questionRepository->getAll();
Expand Down
111 changes: 111 additions & 0 deletions tests/Question/Controllers/QuestionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

namespace VSV\GVQ_API\Question\Controllers;

use League\Uri\Uri;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use VSV\GVQ_API\Question\Models\Answer;
use VSV\GVQ_API\Question\Models\Answers;
use VSV\GVQ_API\Question\Models\Category;
use VSV\GVQ_API\Question\Models\Question;
use VSV\GVQ_API\Question\Models\Questions;
use VSV\GVQ_API\Question\Repositories\QuestionRepository;
use VSV\GVQ_API\Question\Serializers\ExpectedJsonTrait;
use VSV\GVQ_API\Question\Serializers\QuestionSerializer;
use VSV\GVQ_API\Question\Serializers\QuestionsSerializer;
use VSV\GVQ_API\Question\ValueObjects\Language;
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString;
use VSV\GVQ_API\Question\ValueObjects\Year;

class QuestionControllerTest extends TestCase
{
Expand Down Expand Up @@ -57,6 +66,7 @@ public function it_saves_a_question(): void
->expects($this->once())
->method('save')
->with($question);

$expectedResponse = new Response('{"id":"'.$question->getId()->toString().'"}');
$expectedResponse->headers->set('Content-Type', 'application/json');

Expand All @@ -71,4 +81,105 @@ public function it_saves_a_question(): void
$actualResponse->headers->get('Content-Type')
);
}

/**
* @test
*/
public function it_can_get_all_questions(): void
{
// TODO: Replace with ModelsFactory.
$feedback = new NotEmptyString(
'La voie publique située entre les deux lignes blanches continues est un site spécial franchissable.'
);
$questions = new Questions(
new Question(
Uuid::fromString('448c6bd8-0075-4302-a4de-fe34d1554b8d'),
new Language('fr'),
new Year(2018),
new Category(
Uuid::fromString('1289d4b5-e88e-4b3c-9223-eb2c7c49f4d0'),
new NotEmptyString('EHBO/Ongeval/Verzekering')
),
new NotEmptyString(
'La voiture devant vous roule très lentement. Pouvez-vous la dépasser par la gauche?'
),
Uri::createFromString(
'https://vragendatabank.com/styles/verkeersquiz_430x1/s3/01.07.jpg?itok=6-35lj-4'
),
new Answers(
new Answer(
Uuid::fromString('73e6a2d0-3a50-4089-b84a-208092aeca8e'),
new NotEmptyString('Oui, mais uniquement en agglomération.'),
false
),
new Answer(
Uuid::fromString('96bbb677-0839-46ae-9554-bcb709e49cab'),
new NotEmptyString('Non, on ne peut jamais rouler sur une voie ferrée.'),
false
),
new Answer(
Uuid::fromString('53780149-4ef9-405f-b4f4-45e55fde3d67'),
new NotEmptyString('Non.'),
true
)
),
$feedback
),
new Question(
Uuid::fromString('5ffcac55-74e3-4836-a890-3e89a8a1cc15'),
new Language('fr'),
new Year(2018),
new Category(
Uuid::fromString('a7910bf1-05f9-4bdb-8dee-1256cbfafc0b'),
new NotEmptyString('Algemene verkeersregels')
),
new NotEmptyString(
'Qui peut stationner devant ce garage?'
),
Uri::createFromString(
'https://vragendatabank.com/styles/verkeersquiz_430x1/s3/01.07.jpg?itok=6ablj-4'
),
new Answers(
new Answer(
Uuid::fromString('c4d5fa4d-b5bc-4d92-a201-a84abb0e3253'),
new NotEmptyString('Les habitants de cette maison.'),
false
),
new Answer(
Uuid::fromString('1ae8ea74-87f9-4e65-9458-d605888c3a54'),
new NotEmptyString('Personne.'),
false
),
new Answer(
Uuid::fromString('a33daadb-be3f-4625-b1ae-368611680bde'),
new NotEmptyString('Les habitants de cette maison et leurs visiteurs.'),
true
)
),
new NotEmptyString(
'Il est interdit de stationner devant l’entrée des propriétés.'
)
)
);

$this->questionRepository
->expects($this->once())
->method('getAll')
->willReturn($questions);

$questionsJson = $this->getExpectedJson(__DIR__.'/../Serializers/Samples/questions.json');
$expectedResponse = new Response($questionsJson);
$expectedResponse->headers->set('Content-Type', 'application/json');

$actualResponse = $this->questionController->getAll();

$this->assertEquals(
$expectedResponse->getContent(),
$actualResponse->getContent()
);
$this->assertEquals(
$expectedResponse->headers->get('Content-Type'),
$actualResponse->headers->get('Content-Type')
);
}
}

0 comments on commit 92a5575

Please sign in to comment.