File tree 9 files changed +130
-21
lines changed
9 files changed +130
-21
lines changed Original file line number Diff line number Diff line change 2
2
namespace App \Markdown ;
3
3
4
4
interface FetcherInterface {
5
- public function fetch (): array ;
5
+ public function fetch (string $ source ): array ;
6
6
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 3
3
4
4
class QuizFetcher implements FetcherInterface
5
5
{
6
- public function fetch (): array
6
+ public function fetch (string $ source ): array
7
7
{
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 );
12
10
13
11
$ data = [];
14
12
foreach ($ directories as $ directory ){
Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace App \Markdown ;
3
3
4
- class QuizGenerator
4
+ class QuizGenerator implements GeneratorInterface
5
5
{
6
- private array $ filePaths ;
7
-
8
6
public function __construct (private FetcherInterface $ fetcher )
9
7
{ }
10
8
11
- public function generate (): void
9
+ public function generate (string $ source ): array
12
10
{
13
- $ this ->filePaths = $ this ->fetcher ->fetch ();
11
+ $ filePaths = $ this ->fetcher ->fetch ($ source );
12
+ return $ this ->process ($ filePaths );
14
13
}
15
14
16
15
/**
@@ -32,10 +31,10 @@ public function generateNameFromFilePath(string $filePath): string
32
31
return ucfirst ($ name );
33
32
}
34
33
35
- public function getDataSets ( ): array
34
+ public function process ( array $ filePaths ): array
36
35
{
37
36
$ dataSets = [];
38
- foreach ($ this -> filePaths as $ filePath ){
37
+ foreach ($ filePaths as $ filePath ){
39
38
$ dataSets [] = [
40
39
'id ' => $ this ->generateIDFromFilePath ($ filePath ),
41
40
'name ' => $ this ->generateNameFromFilePath ($ filePath ),
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 8
8
use App \Markdown \QuizGenerator ;
9
9
use PHPUnit \Framework \TestCase ;
10
10
11
- class MarkdownFetcherTest extends TestCase
11
+ class QuizFetcherTest extends TestCase
12
12
{
13
13
public function testFetchQuizzes ()
14
14
{
@@ -23,9 +23,10 @@ public function testFetchQuizzes()
23
23
* $quizzes = ['1_CSS_Quiz','2_HTML_Quiz' '3_JavaScript_Quiz']
24
24
*/
25
25
26
- $ fetcher = new QuizFetcher ();
27
- $ data = $ fetcher ->fetch ();
26
+ $ source = '/config/fixtures/quizzes ' ;
28
27
28
+ $ fetcher = new QuizFetcher ();
29
+ $ data = $ fetcher ->fetch ($ source );
29
30
30
31
$ expected = '/config/fixtures/quizzes/1_CSS_Quiz ' ;
31
32
Original file line number Diff line number Diff line change 9
9
use App \Markdown \QuizGenerator ;
10
10
use PHPUnit \Framework \TestCase ;
11
11
12
- class MarkdownGeneratorTest extends TestCase
12
+ class QuizGeneratorTest extends TestCase
13
13
{
14
14
15
15
public function testGenerator ()
16
16
{
17
-
18
17
/**
19
18
* Return an array of directories
20
19
* $dataSets = [
@@ -31,14 +30,17 @@ public function testGenerator()
31
30
* ]
32
31
*/
33
32
33
+ $ source = '/config/fixtures/quizzes/ ' ;
34
34
$ 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 ([
36
39
'/config/fixtures/quizzes/1_CSS_Quiz ' ,
37
40
'/config/fixtures/quizzes/2_HTML_Quiz '
38
41
]);
39
42
$ quizGenerator = new QuizGenerator ($ fetcherMock );
40
- $ quizGenerator ->generate ();
41
- $ dataSets = $ quizGenerator ->getDataSets ();
43
+ $ dataSets = $ quizGenerator ->generate ($ source );
42
44
43
45
$ cssQuiz = $ dataSets [0 ];
44
46
You can’t perform that action at this time.
0 commit comments