1010
1111class 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 }
0 commit comments