Skip to content

Commit ae7dc05

Browse files
committed
#157 WIP
1 parent 7a3a1d4 commit ae7dc05

9 files changed

+130
-21
lines changed

api/src/Markdown/FetcherInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
namespace App\Markdown;
33

44
interface FetcherInterface {
5-
public function fetch(): array;
5+
public function fetch(string $source): array;
66
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace App\Markdown;
3+
4+
interface GeneratorInterface {
5+
public function generate(string $source): array;
6+
public function process(array $filePaths): array;
7+
}

api/src/Markdown/QuestionFetcher.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace App\Markdown;
3+
4+
use Symfony\Component\Finder\Finder;
5+
6+
class QuestionFetcher implements FetcherInterface
7+
{
8+
/**
9+
* Fetch all the question filenames from within a given quiz directory (source)
10+
*
11+
* @param string $source
12+
* @return array
13+
*/
14+
public function fetch(string $source): array
15+
{
16+
$folderPath = dirname(__DIR__) . '/..' . $source;
17+
18+
$filenames = [];
19+
$finder = new Finder();
20+
$files = $finder->files()->in($folderPath)->notName('index.md');
21+
foreach($files as $file){
22+
$filenames[] = $file->getFilename();
23+
}
24+
25+
return $filenames;
26+
}
27+
28+
29+
}

api/src/Markdown/QuizFetcher.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33

44
class QuizFetcher implements FetcherInterface
55
{
6-
public function fetch(): array
6+
public function fetch(string $source): array
77
{
8-
$filePath = '/config/fixtures/quizzes';
9-
$baseDir = dirname(__DIR__) . '/..' . $filePath;
10-
11-
$directories = glob($baseDir . '/*' , GLOB_ONLYDIR);
8+
$fullPath = dirname(__DIR__) . '/..' . $source;
9+
$directories = glob($fullPath . '/*' , GLOB_ONLYDIR);
1210

1311
$data = [];
1412
foreach($directories as $directory){

api/src/Markdown/QuizGenerator.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22
namespace App\Markdown;
33

4-
class QuizGenerator
4+
class QuizGenerator implements GeneratorInterface
55
{
6-
private array $filePaths;
7-
86
public function __construct(private FetcherInterface $fetcher)
97
{ }
108

11-
public function generate(): void
9+
public function generate(string $source): array
1210
{
13-
$this->filePaths = $this->fetcher->fetch();
11+
$filePaths = $this->fetcher->fetch($source);
12+
return $this->process($filePaths);
1413
}
1514

1615
/**
@@ -32,10 +31,10 @@ public function generateNameFromFilePath(string $filePath): string
3231
return ucfirst($name);
3332
}
3433

35-
public function getDataSets(): array
34+
public function process(array $filePaths): array
3635
{
3736
$dataSets = [];
38-
foreach($this->filePaths as $filePath){
37+
foreach($filePaths as $filePath){
3938
$dataSets[] = [
4039
'id' => $this->generateIDFromFilePath($filePath),
4140
'name' => $this->generateNameFromFilePath($filePath),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Tests\unit\src\Markdown;
4+
5+
use App\Markdown\QuestionFetcher;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class QuestionFetcherTest extends TestCase
9+
{
10+
public function testFetchQuestion()
11+
{
12+
$fetcher = new QuestionFetcher();
13+
$data = $fetcher->fetch('/config/fixtures/quizzes/1_CSS_Quiz');
14+
15+
$expected = '1_1_padding_properties.md';
16+
17+
self::assertContains($expected, $data);
18+
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Tests\unit\src\Markdown;
4+
5+
use App\Markdown\QuizFetcher;
6+
use App\Markdown\QuestionGenerator;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class QuestionGeneratorTest extends TestCase
10+
{
11+
12+
public function testGenerator()
13+
{
14+
/**
15+
* Return an array of file data
16+
* $dataSets = [
17+
* [
18+
* 'code' => '1_1',
19+
* 'title' => 'Padding properties',
20+
* 'quiz_id' => 1,
21+
* 'contents_raw' => '<p>Raw HTML for question 1</p>
22+
* ],
23+
* [
24+
* 'code' => 1_3,
25+
* 'title' => 'Style override',
26+
* 'quiz_id' => 1,
27+
* 'contents_raw' => '<p>Raw HTML for question 2</p>
28+
* ],
29+
* ]
30+
*/
31+
32+
$source = '/config/fixtures/quizzes/';
33+
$fetcherMock = $this->createMock(QuizFetcher::class);
34+
$fetcherMock->expects(self::once())
35+
->method('fetch')
36+
->with($source)
37+
->willReturn([
38+
'/config/fixtures/quizzes/1_CSS_Quiz',
39+
'/config/fixtures/quizzes/2_HTML_Quiz'
40+
]);
41+
$quizGenerator = new QuestionGenerator($fetcherMock);
42+
$dataSets = $quizGenerator->generate($source);
43+
44+
$cssQuiz = $dataSets[0];
45+
46+
self::assertArrayHasKey('id', $cssQuiz);
47+
self::assertArrayHasKey('name', $cssQuiz);
48+
self::assertArrayHasKey('file_path', $cssQuiz);
49+
50+
}
51+
52+
}

api/tests/unit/src/Markdown/MarkdownFetcherTest.php renamed to api/tests/unit/src/Markdown/QuizFetcherTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use App\Markdown\QuizGenerator;
99
use PHPUnit\Framework\TestCase;
1010

11-
class MarkdownFetcherTest extends TestCase
11+
class QuizFetcherTest extends TestCase
1212
{
1313
public function testFetchQuizzes()
1414
{
@@ -23,9 +23,10 @@ public function testFetchQuizzes()
2323
* $quizzes = ['1_CSS_Quiz','2_HTML_Quiz' '3_JavaScript_Quiz']
2424
*/
2525

26-
$fetcher = new QuizFetcher();
27-
$data = $fetcher->fetch();
26+
$source = '/config/fixtures/quizzes';
2827

28+
$fetcher = new QuizFetcher();
29+
$data = $fetcher->fetch($source);
2930

3031
$expected = '/config/fixtures/quizzes/1_CSS_Quiz';
3132

api/tests/unit/src/Markdown/MarkdownGeneratorTest.php renamed to api/tests/unit/src/Markdown/QuizGeneratorTest.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
use App\Markdown\QuizGenerator;
1010
use PHPUnit\Framework\TestCase;
1111

12-
class MarkdownGeneratorTest extends TestCase
12+
class QuizGeneratorTest extends TestCase
1313
{
1414

1515
public function testGenerator()
1616
{
17-
1817
/**
1918
* Return an array of directories
2019
* $dataSets = [
@@ -31,14 +30,17 @@ public function testGenerator()
3130
* ]
3231
*/
3332

33+
$source = '/config/fixtures/quizzes/';
3434
$fetcherMock = $this->createMock(QuizFetcher::class);
35-
$fetcherMock->expects(self::once())->method('fetch')->willReturn([
35+
$fetcherMock->expects(self::once())
36+
->method('fetch')
37+
->with($source)
38+
->willReturn([
3639
'/config/fixtures/quizzes/1_CSS_Quiz',
3740
'/config/fixtures/quizzes/2_HTML_Quiz'
3841
]);
3942
$quizGenerator = new QuizGenerator($fetcherMock);
40-
$quizGenerator->generate();
41-
$dataSets = $quizGenerator->getDataSets();
43+
$dataSets = $quizGenerator->generate($source);
4244

4345
$cssQuiz = $dataSets[0];
4446

0 commit comments

Comments
 (0)