Skip to content

Commit 2439a8b

Browse files
authored
Merge pull request #136 from metinbaris/Create-unit-tests-for-AppFixtures.php
Create unit tests for app fixtures.php
2 parents 6f367a6 + 8411725 commit 2439a8b

File tree

8 files changed

+258
-38
lines changed

8 files changed

+258
-38
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea
1+
.idea
2+
.vscode
3+
.DS_Store

api/config/fixtures/quizzes/html-quiz/quiz.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
$question1 = require_once 'questions/question_1.php';
4-
$question2 = require_once 'questions/question_2.php';
3+
$question1 = require 'questions/question_1.php';
4+
$question2 = require 'questions/question_2.php';
55

66
/**
77
* @var array{title:string, slug: string, questions: array{array{content:string, quiz: string, answers: array{array{content: string, is_correct: boolean, display_order: integer}} }}} $quiz

api/config/fixtures/quizzes/javascript-quiz/quiz.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
$question1 = require_once 'questions/question_1.php';
4-
$question2 = require_once 'questions/question_2.php';
5-
$question3 = require_once 'questions/question_3.php';
6-
$question4 = require_once 'questions/question_4.php';
7-
$question5 = require_once 'questions/question_5.php';
8-
$question6 = require_once 'questions/question_6.php';
3+
$question1 = require 'questions/question_1.php';
4+
$question2 = require 'questions/question_2.php';
5+
$question3 = require 'questions/question_3.php';
6+
$question4 = require 'questions/question_4.php';
7+
$question5 = require 'questions/question_5.php';
8+
$question6 = require 'questions/question_6.php';
99

1010
/**
1111
* @var array{title:string, slug: string, questions: array{array{content:string, quiz: string, answers: array{array{content: string, is_correct: boolean, display_order: integer}} }}} $quiz

api/config/fixtures/quizzes/python-quiz/quiz.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
$question1 = require_once 'questions/question_1.php';
4-
$question2 = require_once 'questions/question_2.php';
5-
$question3 = require_once 'questions/question_3.php';
6-
$question4 = require_once 'questions/question_4.php';
7-
$question5 = require_once 'questions/question_5.php';
3+
$question1 = require 'questions/question_1.php';
4+
$question2 = require 'questions/question_2.php';
5+
$question3 = require 'questions/question_3.php';
6+
$question4 = require 'questions/question_4.php';
7+
$question5 = require 'questions/question_5.php';
88

99
/**
1010
* @var array{title:string, slug: string, questions: array{array{content:string, quiz: string, answers: array{array{content: string, is_correct: boolean, display_order: integer}} }}} $quiz

api/src/DataFixtures/AppFixtures.php

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,85 @@
1010

1111
class AppFixtures extends Fixture
1212
{
13+
/** @var ObjectManager */
14+
private $objectManager;
15+
16+
/**
17+
* @param ObjectManager $manager
18+
*/
1319
public function load(ObjectManager $manager): void
1420
{
21+
$this->objectManager = $manager;
22+
23+
$this->createQuizzes();
24+
}
25+
26+
/**
27+
* @return array<int, Quiz> $createdQuizzes
28+
*/
29+
public function createQuizzes(): array
30+
{
31+
$createdQuizzes = [];
1532
$dataSets = $this->getDataSets();
33+
1634
foreach ($dataSets as $quizData) {
17-
$quiz = $this->createQuiz($manager, $quizData);
35+
$quiz = $this->createQuiz($quizData);
1836
$questions = $quizData['questions'];
1937

20-
foreach ($questions as $questionData) {
21-
$question = $this->createQuestion($manager, $questionData, $quiz);
22-
$answers = $questionData['answers'];
23-
foreach ($answers as $answerData) {
24-
$this->createAnswer($manager, $answerData, $question);
25-
}
26-
}
38+
$this->createQuestions($questions, $quiz);
2739

28-
$manager->flush();
40+
$this->objectManager->flush();
41+
$createdQuizzes[] = $quiz;
2942
}
43+
44+
return $createdQuizzes;
45+
}
46+
47+
/**
48+
* @param array{array{content:string, quiz: string, answers: array{array{content: string, is_correct: boolean, display_order: integer}} }} $questions
49+
* @param Quiz $quiz
50+
* @return array<int, Question> $createdQuestions
51+
*/
52+
public function createQuestions(array $questions, Quiz $quiz): array
53+
{
54+
$createdQuestions = [];
55+
foreach ($questions as $questionData) {
56+
$question = $this->createQuestion($questionData, $quiz);
57+
$answers = $questionData['answers'];
58+
59+
$this->createAnswers($answers, $question);
60+
61+
$createdQuestions[] = $question;
62+
}
63+
64+
return $createdQuestions;
65+
}
66+
67+
/**
68+
* @param array{array{content: string, is_correct: boolean, display_order: integer}} $answers
69+
* @param Question $question
70+
* @return array<int, Answer> $createdAnswers
71+
*/
72+
public function createAnswers(array $answers, Question $question): array
73+
{
74+
$createdAnswers = [];
75+
foreach ($answers as $answerData) {
76+
$answer = $this->createAnswer($answerData, $question);
77+
$createdAnswers[] = $answer;
78+
}
79+
80+
return $createdAnswers;
3081
}
3182

3283
/**
3384
* @return array{array{title:string, slug: string, questions: array{array{content:string, quiz: string, answers: array{array{content: string, is_correct: boolean, display_order: integer}} }}}}
3485
*/
35-
protected function getDataSets(): array
86+
public function getDataSets(): array
3687
{
3788
$quizData = [];
3889
$filePaths = $this->getFilePaths();
3990
foreach ($filePaths as $filePath) {
40-
$quizData[] = require_once dirname(__DIR__) . '/../' . $filePath;
91+
$quizData[] = require dirname(__DIR__) . '/../' . $filePath;
4192
}
4293

4394
/**
@@ -49,53 +100,51 @@ protected function getDataSets(): array
49100
/**
50101
* @return string[]
51102
*/
52-
protected function getFilePaths(): array
103+
public function getFilePaths(): array
53104
{
54105
return [
55106
'config/fixtures/quizzes/html-quiz/quiz.php',
107+
'config/fixtures/quizzes/javascript-quiz/quiz.php',
56108
'config/fixtures/quizzes/python-quiz/quiz.php'
57109
];
58110
}
59111

60112
/**
61-
* @param ObjectManager $manager
62113
* @param array{title: string, slug: string} $data
63114
* @return Quiz
64115
*/
65-
protected function createQuiz(ObjectManager $manager, array $data): Quiz
116+
public function createQuiz(array $data): Quiz
66117
{
67118
$entity = new Quiz();
68119
$entity->setTitle($data['title'])
69120
->setSlug($data['slug']);
70121

71-
$manager->persist($entity);
122+
$this->objectManager->persist($entity);
72123

73124
return $entity;
74125
}
75126

76127
/**
77-
* @param ObjectManager $manager
78128
* @param array{content: string} $data
79129
* @param Quiz $quiz
80130
* @return Question
81131
*/
82-
protected function createQuestion(ObjectManager $manager, array $data, Quiz $quiz): Question
132+
public function createQuestion(array $data, Quiz $quiz): Question
83133
{
84134
$entity = new Question();
85135
$entity->setContent($data['content'])
86136
->setQuiz($quiz);
87-
$manager->persist($entity);
137+
$this->objectManager->persist($entity);
88138

89139
return $entity;
90140
}
91141

92142
/**
93-
* @param ObjectManager $manager
94143
* @param array{content: string, display_order: integer, is_correct: boolean} $data
95144
* @param Question $question
96145
* @return Answer
97146
*/
98-
protected function createAnswer(ObjectManager $manager, array $data, Question $question): Answer
147+
public function createAnswer(array $data, Question $question): Answer
99148
{
100149
$entity = new Answer();
101150
$entity
@@ -104,7 +153,7 @@ protected function createAnswer(ObjectManager $manager, array $data, Question $q
104153
->setIsCorrect($data['is_correct'])
105154
->setQuestion($question);
106155

107-
$manager->persist($entity);
156+
$this->objectManager->persist($entity);
108157

109158
return $entity;
110159
}

api/src/Entity/Answer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function setContent(string $content): self
5555
return $this;
5656
}
5757

58-
public function isIsCorrect(): ?bool
58+
public function getIsCorrect(): ?bool
5959
{
6060
return $this->is_correct;
6161
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\unit\config\fixtures\data_fixtures;
6+
7+
use App\DataFixtures\AppFixtures;
8+
use App\Entity\Answer;
9+
use App\Entity\Question;
10+
use App\Entity\Quiz;
11+
use Doctrine\Persistence\ObjectManager;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
use ReflectionClass;
15+
16+
final class AppFixturesTest extends TestCase
17+
{
18+
/** @var AppFixtures */
19+
private $appFixtures;
20+
21+
/** @var MockObject|ObjectManager */
22+
private $objectManager;
23+
24+
protected function setUp(): void
25+
{
26+
$objectManager = $this->getMockBuilder(ObjectManager::class)
27+
->onlyMethods(['flush', 'persist'])
28+
->getMockForAbstractClass();
29+
$this->objectManager = $objectManager;
30+
31+
$appFixtures = new AppFixtures;
32+
$appFixtures = $this->invokeProperty($appFixtures, 'objectManager', $objectManager);
33+
$this->appFixtures = $appFixtures;
34+
}
35+
36+
/**
37+
* @param object $object
38+
* @param string $propertyName
39+
* @param mixed $parameter
40+
*/
41+
public function invokeProperty(object &$object, string $propertyName, mixed $parameter): object
42+
{
43+
$reflection = new ReflectionClass(get_class($object));
44+
$property = $reflection->getProperty($propertyName);
45+
$property->setAccessible(true);
46+
$property->setValue($object, $parameter);
47+
48+
return $object;
49+
}
50+
51+
public function testLoad()
52+
{
53+
$loadFunction = $this->appFixtures->load($this->objectManager);
54+
55+
self::assertNull($loadFunction);
56+
}
57+
58+
public function testCreateQuizzes()
59+
{
60+
$createdQuizzes = $this->appFixtures->createQuizzes();
61+
62+
self::assertIsArray($createdQuizzes);
63+
64+
foreach ($createdQuizzes as $quiz) {
65+
self::assertInstanceOf(Quiz::class, $quiz);
66+
}
67+
}
68+
69+
public function testCreateQuestions()
70+
{
71+
$html = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes/html-quiz/quiz.php';
72+
73+
$quiz = new Quiz;
74+
$quiz->setTitle('Test title');
75+
$quiz->setSlug('test-slug');
76+
77+
$createdQuestions = $this->appFixtures->createQuestions($html['questions'], $quiz);
78+
79+
self::assertIsArray($createdQuestions);
80+
81+
foreach ($createdQuestions as $question) {
82+
self::assertInstanceOf(Question::class, $question);
83+
}
84+
}
85+
86+
public function testCreateAnswers()
87+
{
88+
$question1 = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes/html-quiz/questions/question_1.php';
89+
$question = new Question;
90+
$question->setContent('Test content');
91+
92+
$createdAnswers = $this->appFixtures->createAnswers($question1[0]['answers'], $question);
93+
94+
self::assertIsArray($createdAnswers);
95+
96+
foreach ($createdAnswers as $answer) {
97+
self::assertInstanceOf(Answer::class, $answer);
98+
}
99+
}
100+
101+
public function testGetDataSets()
102+
{
103+
$result = $this->appFixtures->getDataSets();
104+
105+
self::assertIsArray($result);
106+
}
107+
108+
public function testGetFilePathsMethod()
109+
{
110+
$filePaths = $this->appFixtures->getFilePaths();
111+
112+
self::assertIsArray($filePaths);
113+
114+
foreach ($filePaths as $filePath) {
115+
self::assertFileExists($filePath);
116+
}
117+
}
118+
119+
public function testCreateQuiz()
120+
{
121+
$data = [
122+
'title' => 'Test Quiz Title',
123+
'slug' => 'test-quiz-title'
124+
];
125+
126+
$result = $this->appFixtures->createQuiz($data);
127+
128+
self::assertInstanceOf(Quiz::class, $result);
129+
self::assertEquals("Test Quiz Title", $result->getTitle());
130+
self::assertEquals("test-quiz-title", $result->getSlug());
131+
}
132+
133+
public function testCreateQuestion()
134+
{
135+
$quiz = new Quiz;
136+
$quiz = $this->invokeProperty($quiz, 'id', 10);
137+
138+
$data = [
139+
'content' => 'Test quiz content',
140+
];
141+
142+
$result = $this->appFixtures->createQuestion($data, $quiz);
143+
144+
self::assertInstanceOf(Question::class, $result);
145+
self::assertInstanceOf(Quiz::class, $result->getQuiz());
146+
self::assertEquals(10, $result->getQuiz()->getId());
147+
self::assertEquals("Test quiz content", $result->getContent());
148+
}
149+
150+
public function testCreateAnswer()
151+
{
152+
$question = new Question;
153+
$question = $this->invokeProperty($question, 'id', 10);
154+
155+
$data = [
156+
'content' => 'Test answer content',
157+
'display_order' => 3,
158+
'is_correct' => false
159+
];
160+
161+
$result = $this->appFixtures->CreateAnswer($data, $question);
162+
163+
self::assertInstanceOf(Answer::class, $result);
164+
self::assertEquals(10, $result->getQuestion()->getId());
165+
self::assertEquals("Test answer content", $result->getContent());
166+
self::assertEquals(3, $result->getDisplayOrder());
167+
self::assertEquals(false, $result->getIsCorrect());
168+
}
169+
}

0 commit comments

Comments
 (0)