Skip to content

Commit 503230c

Browse files
committed
#157 Adding tests and refactoring code
1 parent 7df4c98 commit 503230c

21 files changed

+40
-61
lines changed

api/src/Markdown/FetcherInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
interface FetcherInterface
@@ -8,4 +9,4 @@ interface FetcherInterface
89
* @return string[]
910
*/
1011
public function fetch(string $source): array;
11-
}
12+
}

api/src/Markdown/GeneratorInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
interface GeneratorInterface
@@ -14,4 +15,4 @@ public function generate(string $source): array;
1415
* @return array
1516
*/
1617
public function process(array $filePaths): array;
17-
}
18+
}

api/src/Markdown/Model/ModelInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ interface ModelInterface
66
{
77
public function getId(): int;
88
public function getFilePath(): string;
9-
}
9+
}

api/src/Markdown/Model/Question.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
class Question implements ModelInterface
66
{
7-
87
public function __construct(
9-
private readonly int $id,
8+
private readonly int $id,
109
private readonly int $quizID,
1110
private readonly string $filePath,
1211
private readonly string $title,
1312
private readonly array $content,
1413
private readonly array $possibleAnswers,
15-
private readonly array $correctAnswer)
16-
{
14+
private readonly array $correctAnswer
15+
) {
1716
}
1817

1918
/**
@@ -72,6 +71,4 @@ public function getQuizID(): int
7271
{
7372
return $this->quizID;
7473
}
75-
76-
77-
}
74+
}

api/src/Markdown/Model/Quiz.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
class Quiz
66
{
7-
87
public function __construct(
9-
private readonly int $id,
8+
private readonly int $id,
109
private readonly string $name,
1110
private readonly string $filePath
12-
)
13-
{
11+
) {
1412
}
1513

1614
/**
@@ -36,6 +34,4 @@ public function getFilePath()
3634
{
3735
return $this->filePath;
3836
}
39-
40-
41-
}
37+
}

api/src/Markdown/Parser/DocumentExtractor.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class DocumentExtractor
1616

1717
public function __construct(private readonly string $document)
1818
{
19-
2019
}
2120

2221
/**
@@ -25,12 +24,11 @@ public function __construct(private readonly string $document)
2524
public function extract(): void
2625
{
2726
$domDocument = new DOMDocument();
28-
libxml_use_internal_errors(TRUE);
27+
libxml_use_internal_errors(true);
2928
$domDocument->loadHTML($this->document);
3029
libxml_get_errors();
3130

3231
$this->process($domDocument);
33-
3432
}
3533

3634
public function process(DOMNode $domNode): void
@@ -50,7 +48,6 @@ public function process(DOMNode $domNode): void
5048

5149
// Get the question
5250
if (!$this->foundPossibleAnswers && !$this->foundCorrectAnswer) {
53-
5451
if ($node->nodeName !== 'body' && $node->nodeName !== 'html') {
5552
$this->question[] = $node;
5653
}
@@ -87,4 +84,4 @@ public function getCorrectAnswerNodes(): array
8784
{
8885
return $this->correctAnswer;
8986
}
90-
}
87+
}

api/src/Markdown/QuestionFetcher.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
use Symfony\Component\Finder\Finder;
@@ -18,12 +19,10 @@ public function fetch(string $source): array
1819
$filenames = [];
1920
$finder = new Finder();
2021
$files = $finder->files()->in($folderPath)->notName('index.md');
21-
foreach($files as $file){
22+
foreach ($files as $file) {
2223
$filenames[] = $file->getFilename();
2324
}
2425

2526
return $filenames;
2627
}
27-
28-
29-
}
28+
}

api/src/Markdown/QuestionGenerator.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Markdown\Model\Question;
66

7-
87
class QuestionGenerator implements GeneratorInterface
98
{
109
public function __construct(private readonly FetcherInterface $fetcher)
@@ -75,4 +74,4 @@ public function process(array $filePaths): array
7574
}
7675
return $dataSets;
7776
}
78-
}
77+
}

api/src/Markdown/QuizFetcher.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
class QuizFetcher implements FetcherInterface
@@ -10,18 +11,16 @@ class QuizFetcher implements FetcherInterface
1011
public function fetch(string $source): array
1112
{
1213
$fullPath = dirname(__DIR__) . '/..' . $source;
13-
$directories = glob($fullPath . '/*' , GLOB_ONLYDIR);
14-
if(!$directories){
14+
$directories = glob($fullPath . '/*', GLOB_ONLYDIR);
15+
if (!$directories) {
1516
return [];
1617
}
1718

1819
$data = [];
19-
foreach($directories as $directory){
20-
$data[] = str_replace('/var/www/html/src/..', '', $directory);
20+
foreach ($directories as $directory) {
21+
$data[] = str_replace('/var/www/html/src/..', '', $directory);
2122
}
2223

2324
return $data;
2425
}
25-
26-
27-
}
26+
}

api/src/Markdown/QuizGenerator.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
use App\Markdown\Model\Quiz;
56

67
class QuizGenerator implements GeneratorInterface
78
{
89
public function __construct(private readonly FetcherInterface $fetcher)
9-
{ }
10+
{
11+
}
1012

1113
/**
1214
* @param string $source
@@ -34,7 +36,7 @@ public function generateNameFromFilePath(string $filePath): string
3436
$directoryLeaf = basename($filePath);
3537
$name = str_replace('_', ' ', $directoryLeaf);
3638
$firstSpace = strstr($name, ' ');
37-
if($firstSpace) {
39+
if ($firstSpace) {
3840
$name = ltrim($firstSpace, ' ');
3941
}
4042
return ucfirst($name);
@@ -56,4 +58,4 @@ public function process(array $filePaths): array
5658
}
5759
return $dataSets;
5860
}
59-
}
61+
}

api/tests/unit/config/fixtures/data_fixtures/AppFixturesTest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ protected function setUp(): void
2727
->onlyMethods(['flush', 'persist'])
2828
->getMockForAbstractClass();
2929
$this->objectManager = $objectManager;
30-
31-
$appFixtures = new AppFixtures;
30+
31+
$appFixtures = new AppFixtures();
3232
$appFixtures = $this->invokeProperty($appFixtures, 'objectManager', $objectManager);
3333
$this->appFixtures = $appFixtures;
3434
}
@@ -69,7 +69,7 @@ public function testCreateQuestions()
6969
{
7070
$html = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes_old/html-quiz/quiz.php';
7171

72-
$quiz = new Quiz;
72+
$quiz = new Quiz();
7373
$quiz->setTitle('Test title');
7474
$quiz->setSlug('test-slug');
7575

@@ -85,7 +85,7 @@ public function testCreateQuestions()
8585
public function testCreateAnswers()
8686
{
8787
$question1 = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes_old/html-quiz/questions/question_1.php';
88-
$question = new Question;
88+
$question = new Question();
8989
$question->setContent('Test content');
9090

9191
$createdAnswers = $this->appFixtures->createAnswers($question1[0]['answers'], $question);
@@ -131,7 +131,7 @@ public function testCreateQuiz()
131131

132132
public function testCreateQuestion()
133133
{
134-
$quiz = new Quiz;
134+
$quiz = new Quiz();
135135
$quiz = $this->invokeProperty($quiz, 'id', 10);
136136

137137
$data = [
@@ -148,7 +148,7 @@ public function testCreateQuestion()
148148

149149
public function testCreateAnswer()
150150
{
151-
$question = new Question;
151+
$question = new Question();
152152
$question = $this->invokeProperty($question, 'id', 10);
153153

154154
$data = [
@@ -165,4 +165,4 @@ public function testCreateAnswer()
165165
self::assertEquals(3, $result->getDisplayOrder());
166166
self::assertEquals(false, $result->getIsCorrect());
167167
}
168-
}
168+
}

api/tests/unit/src/Markdown/Parser/CorrectAnswerExtractorTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,4 @@ public function testAnswerValue()
3333

3434
self::assertSame('Answer: 5', trim($questionNodes[4]->nodeValue));
3535
}
36-
37-
3836
}

api/tests/unit/src/Markdown/Parser/PossibleAnswerExtractorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testLastPossibleAnswerValue()
5858
$parser = new DocumentExtractor($this->document);
5959
$parser->extract();
6060
$nodes = $parser->getPossibleAnswerNodes();
61-
$lastIndex = count($nodes) -1;
61+
$lastIndex = count($nodes) - 1;
6262

6363
self::assertSame('[ ] 5', $nodes[$lastIndex]->nodeValue);
6464
}
@@ -68,7 +68,7 @@ public function testLastPossibleAnswerElement()
6868
$parser = new DocumentExtractor($this->document);
6969
$parser->extract();
7070
$nodes = $parser->getPossibleAnswerNodes();
71-
$lastIndex = count($nodes) -1;
71+
$lastIndex = count($nodes) - 1;
7272

7373
self::assertSame('li', $nodes[$lastIndex]->nodeName);
7474
}

api/tests/unit/src/Markdown/Parser/QuestionExtractorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testLastQuestionElement()
4040
$parser = new DocumentExtractor($this->document);
4141
$parser->extract();
4242
$questionNodes = $parser->getQuestionNodes();
43-
$count = count($questionNodes) -1;
43+
$count = count($questionNodes) - 1;
4444

4545
self::assertSame('code', $questionNodes[$count]->nodeName);
4646
}
@@ -50,7 +50,7 @@ public function testLastQuestionValue()
5050
$parser = new DocumentExtractor($this->document);
5151
$parser->extract();
5252
$questionNodes = $parser->getQuestionNodes();
53-
$count = count($questionNodes) -1;
53+
$count = count($questionNodes) - 1;
5454

5555
self::assertSame('<?php echo "hello world";', $questionNodes[$count]->nodeValue);
5656
}

api/tests/unit/src/Markdown/QuestionFetcherTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@ public function testFetchQuestion()
1515
$expected = '1_1_padding_properties.md';
1616

1717
self::assertContains($expected, $data);
18-
1918
}
20-
2119
}

api/tests/unit/src/Markdown/QuestionGeneratorTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,4 @@ public function testGeneratedTitle()
6060

6161
self::assertSame('Style override', $question2->getTitle());
6262
}
63-
6463
}

api/tests/unit/src/Markdown/QuestionIDGeneratorTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ public function testGetQuestionIDWithIncorrectValueType()
3939
$questionID = $generator->getIDFromFilePath('1_two.md', false);
4040
self::assertFalse($questionID);
4141
}
42-
4342
}

api/tests/unit/src/Markdown/QuestionQuizIDGeneratorTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ public function testGetQuestionIDWithIncorrectValueType()
3939
$ID = $generator->getIDFromFilePath('one_2.md');
4040
self::assertFalse($ID);
4141
}
42-
4342
}

api/tests/unit/src/Markdown/QuestionTitleGeneratorTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ public function testGetQuestionTitleWithNoTitleValue()
2323
$title = $generator->getTitleFromFilePath('1.md');
2424
self::assertFalse($title);
2525
}
26-
2726
}

api/tests/unit/src/Markdown/QuizFetcherTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,5 @@ public function testFetchQuizzes()
3131
$expected = '/config/fixtures/quizzes/1_CSS_Quiz';
3232

3333
self::assertContains($expected, $data);
34-
3534
}
36-
3735
}

api/tests/unit/src/Markdown/QuizGeneratorTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
class QuizGeneratorTest extends TestCase
1414
{
15-
1615
public function testGenerateIDFromFilePath()
1716
{
1817
// $document = '<p>This is a test</p><p>This is another test</p>';
@@ -22,5 +21,4 @@ public function testGenerateIDFromFilePath()
2221
// $id = $generator->generateIDFromFilePath($filePath);
2322
self::assertSame(1, 1);
2423
}
25-
2624
}

0 commit comments

Comments
 (0)