Skip to content

Commit

Permalink
#157 Adding tests and refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
pfwd committed Nov 6, 2022
1 parent 7df4c98 commit 503230c
Show file tree
Hide file tree
Showing 21 changed files with 40 additions and 61 deletions.
3 changes: 2 additions & 1 deletion api/src/Markdown/FetcherInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Markdown;

interface FetcherInterface
Expand All @@ -8,4 +9,4 @@ interface FetcherInterface
* @return string[]
*/
public function fetch(string $source): array;
}
}
3 changes: 2 additions & 1 deletion api/src/Markdown/GeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Markdown;

interface GeneratorInterface
Expand All @@ -14,4 +15,4 @@ public function generate(string $source): array;
* @return array
*/
public function process(array $filePaths): array;
}
}
2 changes: 1 addition & 1 deletion api/src/Markdown/Model/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ interface ModelInterface
{
public function getId(): int;
public function getFilePath(): string;
}
}
11 changes: 4 additions & 7 deletions api/src/Markdown/Model/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

class Question implements ModelInterface
{

public function __construct(
private readonly int $id,
private readonly int $id,
private readonly int $quizID,
private readonly string $filePath,
private readonly string $title,
private readonly array $content,
private readonly array $possibleAnswers,
private readonly array $correctAnswer)
{
private readonly array $correctAnswer
) {
}

/**
Expand Down Expand Up @@ -72,6 +71,4 @@ public function getQuizID(): int
{
return $this->quizID;
}


}
}
10 changes: 3 additions & 7 deletions api/src/Markdown/Model/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

class Quiz
{

public function __construct(
private readonly int $id,
private readonly int $id,
private readonly string $name,
private readonly string $filePath
)
{
) {
}

/**
Expand All @@ -36,6 +34,4 @@ public function getFilePath()
{
return $this->filePath;
}


}
}
7 changes: 2 additions & 5 deletions api/src/Markdown/Parser/DocumentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class DocumentExtractor

public function __construct(private readonly string $document)
{

}

/**
Expand All @@ -25,12 +24,11 @@ public function __construct(private readonly string $document)
public function extract(): void
{
$domDocument = new DOMDocument();
libxml_use_internal_errors(TRUE);
libxml_use_internal_errors(true);
$domDocument->loadHTML($this->document);
libxml_get_errors();

$this->process($domDocument);

}

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

// Get the question
if (!$this->foundPossibleAnswers && !$this->foundCorrectAnswer) {

if ($node->nodeName !== 'body' && $node->nodeName !== 'html') {
$this->question[] = $node;
}
Expand Down Expand Up @@ -87,4 +84,4 @@ public function getCorrectAnswerNodes(): array
{
return $this->correctAnswer;
}
}
}
7 changes: 3 additions & 4 deletions api/src/Markdown/QuestionFetcher.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Markdown;

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

return $filenames;
}


}
}
3 changes: 1 addition & 2 deletions api/src/Markdown/QuestionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Markdown\Model\Question;


class QuestionGenerator implements GeneratorInterface
{
public function __construct(private readonly FetcherInterface $fetcher)
Expand Down Expand Up @@ -75,4 +74,4 @@ public function process(array $filePaths): array
}
return $dataSets;
}
}
}
13 changes: 6 additions & 7 deletions api/src/Markdown/QuizFetcher.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace App\Markdown;

class QuizFetcher implements FetcherInterface
Expand All @@ -10,18 +11,16 @@ class QuizFetcher implements FetcherInterface
public function fetch(string $source): array
{
$fullPath = dirname(__DIR__) . '/..' . $source;
$directories = glob($fullPath . '/*' , GLOB_ONLYDIR);
if(!$directories){
$directories = glob($fullPath . '/*', GLOB_ONLYDIR);
if (!$directories) {
return [];
}

$data = [];
foreach($directories as $directory){
$data[] = str_replace('/var/www/html/src/..', '', $directory);
foreach ($directories as $directory) {
$data[] = str_replace('/var/www/html/src/..', '', $directory);
}

return $data;
}


}
}
8 changes: 5 additions & 3 deletions api/src/Markdown/QuizGenerator.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

namespace App\Markdown;

use App\Markdown\Model\Quiz;

class QuizGenerator implements GeneratorInterface
{
public function __construct(private readonly FetcherInterface $fetcher)
{ }
{
}

/**
* @param string $source
Expand Down Expand Up @@ -34,7 +36,7 @@ public function generateNameFromFilePath(string $filePath): string
$directoryLeaf = basename($filePath);
$name = str_replace('_', ' ', $directoryLeaf);
$firstSpace = strstr($name, ' ');
if($firstSpace) {
if ($firstSpace) {
$name = ltrim($firstSpace, ' ');
}
return ucfirst($name);
Expand All @@ -56,4 +58,4 @@ public function process(array $filePaths): array
}
return $dataSets;
}
}
}
14 changes: 7 additions & 7 deletions api/tests/unit/config/fixtures/data_fixtures/AppFixturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ protected function setUp(): void
->onlyMethods(['flush', 'persist'])
->getMockForAbstractClass();
$this->objectManager = $objectManager;
$appFixtures = new AppFixtures;

$appFixtures = new AppFixtures();
$appFixtures = $this->invokeProperty($appFixtures, 'objectManager', $objectManager);
$this->appFixtures = $appFixtures;
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public function testCreateQuestions()
{
$html = require dirname(__DIR__) . '/../../../../config/fixtures/quizzes_old/html-quiz/quiz.php';

$quiz = new Quiz;
$quiz = new Quiz();
$quiz->setTitle('Test title');
$quiz->setSlug('test-slug');

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

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

public function testCreateQuestion()
{
$quiz = new Quiz;
$quiz = new Quiz();
$quiz = $this->invokeProperty($quiz, 'id', 10);

$data = [
Expand All @@ -148,7 +148,7 @@ public function testCreateQuestion()

public function testCreateAnswer()
{
$question = new Question;
$question = new Question();
$question = $this->invokeProperty($question, 'id', 10);

$data = [
Expand All @@ -165,4 +165,4 @@ public function testCreateAnswer()
self::assertEquals(3, $result->getDisplayOrder());
self::assertEquals(false, $result->getIsCorrect());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ public function testAnswerValue()

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


}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testLastPossibleAnswerValue()
$parser = new DocumentExtractor($this->document);
$parser->extract();
$nodes = $parser->getPossibleAnswerNodes();
$lastIndex = count($nodes) -1;
$lastIndex = count($nodes) - 1;

self::assertSame('[ ] 5', $nodes[$lastIndex]->nodeValue);
}
Expand All @@ -68,7 +68,7 @@ public function testLastPossibleAnswerElement()
$parser = new DocumentExtractor($this->document);
$parser->extract();
$nodes = $parser->getPossibleAnswerNodes();
$lastIndex = count($nodes) -1;
$lastIndex = count($nodes) - 1;

self::assertSame('li', $nodes[$lastIndex]->nodeName);
}
Expand Down
4 changes: 2 additions & 2 deletions api/tests/unit/src/Markdown/Parser/QuestionExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testLastQuestionElement()
$parser = new DocumentExtractor($this->document);
$parser->extract();
$questionNodes = $parser->getQuestionNodes();
$count = count($questionNodes) -1;
$count = count($questionNodes) - 1;

self::assertSame('code', $questionNodes[$count]->nodeName);
}
Expand All @@ -50,7 +50,7 @@ public function testLastQuestionValue()
$parser = new DocumentExtractor($this->document);
$parser->extract();
$questionNodes = $parser->getQuestionNodes();
$count = count($questionNodes) -1;
$count = count($questionNodes) - 1;

self::assertSame('<?php echo "hello world";', $questionNodes[$count]->nodeValue);
}
Expand Down
2 changes: 0 additions & 2 deletions api/tests/unit/src/Markdown/QuestionFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ public function testFetchQuestion()
$expected = '1_1_padding_properties.md';

self::assertContains($expected, $data);

}

}
1 change: 0 additions & 1 deletion api/tests/unit/src/Markdown/QuestionGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ public function testGeneratedTitle()

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

}
1 change: 0 additions & 1 deletion api/tests/unit/src/Markdown/QuestionIDGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ public function testGetQuestionIDWithIncorrectValueType()
$questionID = $generator->getIDFromFilePath('1_two.md', false);
self::assertFalse($questionID);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ public function testGetQuestionIDWithIncorrectValueType()
$ID = $generator->getIDFromFilePath('one_2.md');
self::assertFalse($ID);
}

}
1 change: 0 additions & 1 deletion api/tests/unit/src/Markdown/QuestionTitleGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public function testGetQuestionTitleWithNoTitleValue()
$title = $generator->getTitleFromFilePath('1.md');
self::assertFalse($title);
}

}
2 changes: 0 additions & 2 deletions api/tests/unit/src/Markdown/QuizFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ public function testFetchQuizzes()
$expected = '/config/fixtures/quizzes/1_CSS_Quiz';

self::assertContains($expected, $data);

}

}
2 changes: 0 additions & 2 deletions api/tests/unit/src/Markdown/QuizGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

class QuizGeneratorTest extends TestCase
{

public function testGenerateIDFromFilePath()
{
// $document = '<p>This is a test</p><p>This is another test</p>';
Expand All @@ -22,5 +21,4 @@ public function testGenerateIDFromFilePath()
// $id = $generator->generateIDFromFilePath($filePath);
self::assertSame(1, 1);
}

}

0 comments on commit 503230c

Please sign in to comment.