Skip to content

Commit 7df4c98

Browse files
committed
#157 Adding tests and refactoring code
1 parent 6de7b03 commit 7df4c98

File tree

6 files changed

+214
-40
lines changed

6 files changed

+214
-40
lines changed

api/src/Markdown/Model/Question.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class Question implements ModelInterface
77

88
public function __construct(
99
private readonly int $id,
10+
private readonly int $quizID,
1011
private readonly string $filePath,
12+
private readonly string $title,
1113
private readonly array $content,
1214
private readonly array $possibleAnswers,
1315
private readonly array $correctAnswer)
@@ -22,6 +24,14 @@ public function getId(): int
2224
return $this->id;
2325
}
2426

27+
/**
28+
* @return string
29+
*/
30+
public function getFilePath(): string
31+
{
32+
return $this->filePath;
33+
}
34+
2535
/**
2636
* @return array
2737
*/
@@ -49,9 +59,18 @@ public function getCorrectAnswer(): array
4959
/**
5060
* @return string
5161
*/
52-
public function getFilePath(): string
62+
public function getTitle(): string
5363
{
54-
return $this->filePath;
64+
return $this->title;
65+
}
66+
67+
68+
/**
69+
* @return int
70+
*/
71+
public function getQuizID(): int
72+
{
73+
return $this->quizID;
5574
}
5675

5776

api/src/Markdown/QuestionGenerator.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@ public function __construct(private readonly FetcherInterface $fetcher)
1111
{
1212
}
1313

14+
public function getTitleFromFilePath(string $filePath): false|string
15+
{
16+
$index = 2;
17+
$filenameParts = explode('_', $filePath);
18+
if (!isset($filenameParts[$index])) {
19+
return false;
20+
}
21+
22+
if (!is_string($filenameParts[$index])) {
23+
return false;
24+
}
25+
26+
unset($filenameParts[0], $filenameParts[1]);
27+
$title = implode(' ', $filenameParts);
28+
$title = preg_replace('/\.md/i', '', $title);
29+
return ucfirst($title);
30+
}
31+
32+
public function getIDFromFilePath(string $filePath, bool $isQuiz = true): false|int
33+
{
34+
$index = ($isQuiz) ? 0 : 1;
35+
$filenameParts = explode('_', $filePath);
36+
37+
if (!isset($filenameParts[$index])) {
38+
return false;
39+
}
40+
41+
if (!is_numeric($filenameParts[$index])) {
42+
return false;
43+
}
44+
45+
return $filenameParts[$index];
46+
}
47+
1448
/**
1549
* @param string $source
1650
* @return array{int, array{id: int, name: string, file_path: string}}
@@ -29,11 +63,13 @@ public function process(array $filePaths): array
2963
{
3064
$dataSets = [];
3165
foreach ($filePaths as $filePath) {
32-
$id = $this->generateIDFromFilePath($filePath);
66+
$quizID = $this->getIDFromFilePath($filePath);
67+
$questionID = $this->getIDFromFilePath($filePath, false);
68+
$title = $this->getTitleFromFilePath($filePath);
3369
$content = [];
3470
$possibleAnswers = [];
3571
$correctAnswers = [];
36-
$question = new Question($id, $filePath, $content, $possibleAnswers, $correctAnswers);
72+
$question = new Question($questionID, $quizID, $filePath, $title, $content, $possibleAnswers, $correctAnswers);
3773

3874
$dataSets[] = $question;
3975
}

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

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,63 @@
22

33
namespace App\Tests\unit\src\Markdown;
44

5+
use App\Markdown\FetcherInterface;
6+
use App\Markdown\QuestionFetcher;
57
use App\Markdown\QuizFetcher;
68
use App\Markdown\QuestionGenerator;
79
use PHPUnit\Framework\TestCase;
810

911
class QuestionGeneratorTest extends TestCase
1012
{
13+
private QuestionFetcher $fetcherMock;
14+
private const SOURCE = '/config/fixtures/quizzes/1_CSS_Quiz';
1115

12-
public function testGenerator()
16+
public function setUp(): void
1317
{
14-
$this->markTestSkipped('Not in current scop but needs fixing before merge');
15-
/**
16-
* Return an array of file data
17-
* $dataSets = [
18-
* [
19-
* 'file_path' =>
20-
* 'code' => '1_1',
21-
* 'title' => 'Padding properties',
22-
* 'quiz_id' => 1,
23-
* 'contents_raw' => '<p>Raw HTML for question 1</p>
24-
* ],
25-
* [
26-
* 'code' => 1_3,
27-
* 'title' => 'Style override',
28-
* 'quiz_id' => 1,
29-
* 'contents_raw' => '<p>Raw HTML for question 2</p>
30-
* ],
31-
* ]
32-
*/
33-
34-
$source = '/config/fixtures/quizzes/';
35-
$fetcherMock = $this->createMock(QuizFetcher::class);
18+
$fetcherMock = $this->createMock(QuestionFetcher::class);
3619
$fetcherMock->expects(self::once())
3720
->method('fetch')
38-
->with($source)
21+
->with(self::SOURCE)
3922
->willReturn([
40-
'/config/fixtures/quizzes/1_CSS_Quiz',
41-
'/config/fixtures/quizzes/2_HTML_Quiz'
42-
]);
43-
$quizGenerator = new QuestionGenerator($fetcherMock);
44-
$dataSets = $quizGenerator->generate($source);
23+
'1_1_padding_properties.md',
24+
'1_2_style_override.md',
25+
'1_3_text_font.md'
26+
]);
27+
$this->fetcherMock = $fetcherMock;
4528

46-
$cssQuiz = $dataSets[0];
29+
parent::setUp();
30+
}
31+
32+
public function testGeneratedQuestionID()
33+
{
34+
$quizGenerator = new QuestionGenerator($this->fetcherMock);
35+
$dataSets = $quizGenerator->generate(self::SOURCE);
36+
37+
$question2 = $dataSets[1];
4738

48-
self::assertArrayHasKey('id', $cssQuiz);
49-
self::assertArrayHasKey('name', $cssQuiz);
50-
self::assertArrayHasKey('file_path', $cssQuiz);
39+
self::assertSame(2, $question2->getId());
40+
}
41+
42+
public function testGeneratedQuizID()
43+
{
44+
$quizGenerator = new QuestionGenerator($this->fetcherMock);
45+
$dataSets = $quizGenerator->generate(self::SOURCE);
46+
47+
$question2 = $dataSets[1];
48+
49+
self::assertSame(1, $question2->getQuizId());
50+
self::assertSame('Style override', $question2->getTitle());
51+
}
52+
53+
54+
public function testGeneratedTitle()
55+
{
56+
$quizGenerator = new QuestionGenerator($this->fetcherMock);
57+
$dataSets = $quizGenerator->generate(self::SOURCE);
5158

52-
self::assertSame(1, $cssQuiz->getId());
53-
self::assertSame('CSS Quiz', $cssQuiz->getName());
54-
self::assertSame('/config/fixtures/quizzes/1_CSS_Quiz', $cssQuiz->getFilePath());
59+
$question2 = $dataSets[1];
5560

61+
self::assertSame('Style override', $question2->getTitle());
5662
}
5763

5864
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Tests\unit\src\Markdown;
4+
5+
use App\Markdown\FetcherInterface;
6+
use App\Markdown\QuestionGenerator;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class QuestionIDGeneratorTest extends TestCase
10+
{
11+
public function testGetQuestionID()
12+
{
13+
$fetcherMock = $this->createMock(FetcherInterface::class);
14+
$generator = new QuestionGenerator($fetcherMock);
15+
$questionID = $generator->getIDFromFilePath('1_2_style_override.md', false);
16+
self::assertSame(2, $questionID);
17+
}
18+
19+
public function testGetQuestionIDWithDoubleDigits()
20+
{
21+
$fetcherMock = $this->createMock(FetcherInterface::class);
22+
$generator = new QuestionGenerator($fetcherMock);
23+
$questionID = $generator->getIDFromFilePath('1_20_style_override.md', false);
24+
self::assertSame(20, $questionID);
25+
}
26+
27+
public function testGetQuestionIDWithNoIDValue()
28+
{
29+
$fetcherMock = $this->createMock(FetcherInterface::class);
30+
$generator = new QuestionGenerator($fetcherMock);
31+
$questionID = $generator->getIDFromFilePath('1.md', false);
32+
self::assertFalse($questionID);
33+
}
34+
35+
public function testGetQuestionIDWithIncorrectValueType()
36+
{
37+
$fetcherMock = $this->createMock(FetcherInterface::class);
38+
$generator = new QuestionGenerator($fetcherMock);
39+
$questionID = $generator->getIDFromFilePath('1_two.md', false);
40+
self::assertFalse($questionID);
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Tests\unit\src\Markdown;
4+
5+
use App\Markdown\FetcherInterface;
6+
use App\Markdown\QuestionGenerator;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class QuestionQuizIDGeneratorTest extends TestCase
10+
{
11+
public function testGetQuestionID()
12+
{
13+
$fetcherMock = $this->createMock(FetcherInterface::class);
14+
$generator = new QuestionGenerator($fetcherMock);
15+
$ID = $generator->getIDFromFilePath('1_2_style_override.md');
16+
self::assertSame(1, $ID);
17+
}
18+
19+
public function testGetQuestionIDWithDoubleDigits()
20+
{
21+
$fetcherMock = $this->createMock(FetcherInterface::class);
22+
$generator = new QuestionGenerator($fetcherMock);
23+
$ID = $generator->getIDFromFilePath('10_20_style_override.md');
24+
self::assertSame(10, $ID);
25+
}
26+
27+
public function testGetQuestionIDWithNoIDValue()
28+
{
29+
$fetcherMock = $this->createMock(FetcherInterface::class);
30+
$generator = new QuestionGenerator($fetcherMock);
31+
$ID = $generator->getIDFromFilePath('foo.md');
32+
self::assertFalse($ID);
33+
}
34+
35+
public function testGetQuestionIDWithIncorrectValueType()
36+
{
37+
$fetcherMock = $this->createMock(FetcherInterface::class);
38+
$generator = new QuestionGenerator($fetcherMock);
39+
$ID = $generator->getIDFromFilePath('one_2.md');
40+
self::assertFalse($ID);
41+
}
42+
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Tests\unit\src\Markdown;
4+
5+
use App\Markdown\FetcherInterface;
6+
use App\Markdown\QuestionGenerator;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class QuestionTitleGeneratorTest extends TestCase
10+
{
11+
public function testGetQuestionTitle()
12+
{
13+
$fetcherMock = $this->createMock(FetcherInterface::class);
14+
$generator = new QuestionGenerator($fetcherMock);
15+
$title = $generator->getTitleFromFilePath('1_2_style_override.md');
16+
self::assertSame('Style override', $title);
17+
}
18+
19+
public function testGetQuestionTitleWithNoTitleValue()
20+
{
21+
$fetcherMock = $this->createMock(FetcherInterface::class);
22+
$generator = new QuestionGenerator($fetcherMock);
23+
$title = $generator->getTitleFromFilePath('1.md');
24+
self::assertFalse($title);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)