Skip to content

Commit e297bea

Browse files
committed
Added / updated tests.
1 parent bd18fee commit e297bea

File tree

7 files changed

+66
-35
lines changed

7 files changed

+66
-35
lines changed

tests/TestClasses/ViewerTestCase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Mistralys\MarkdownViewer\DocFile;
88
use Mistralys\MarkdownViewer\DocParser;
9+
use Mistralys\MarkdownViewer\DocsConfig;
10+
use Mistralys\MarkdownViewer\DocsManager;
911
use PHPUnit\Framework\TestCase;
1012

1113
abstract class ViewerTestCase extends TestCase
@@ -36,6 +38,12 @@ protected function createTestParser(string $relativePath='', ?string $title='')
3638
$title = 'Title';
3739
}
3840

39-
return new DocParser(new DocFile($title, $this->getPath($relativePath)));
41+
$config = (new DocsConfig())
42+
->addIncludePath($this->filesFolder.'/includes')
43+
->addIncludeExtension('php');
44+
45+
$manager = new DocsManager($config);
46+
47+
return new DocParser(new DocFile($manager, $title, $this->getPath($relativePath)));
4048
}
4149
}

tests/TestSuites/Parser/IncludeTests.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ final class IncludeTests extends ViewerTestCase
1111
{
1212
public function test_findIncludes() : void
1313
{
14-
$includes = $this->createTestParser('document-with-includes.md')
14+
$includes = $this->createTestParser('document-with-errors.md')
1515
->getIncludes();
1616

1717
$this->assertNotEmpty($includes);
18-
$this->assertArrayHasKey('includes/test-php-highlight.php', $includes);
18+
$this->assertArrayHasKey('not-a-file.md', $includes);
1919
}
2020

2121
public function test_includeReplacements() : void
@@ -24,15 +24,28 @@ public function test_includeReplacements() : void
2424
->render();
2525

2626
$this->assertStringContainsString('sample PHP file', $html);
27-
$this->assertStringContainsString('#'.DocParser::ERROR_INCLUDE_FILE_NOT_FOUND, $html);
2827
}
2928

30-
public function test_includeSizeTooBig() : void
29+
public function test_includeErrorTooBig() : void
3130
{
32-
$html = $this->createTestParser('document-with-includes.md')
33-
->setMaxIncludeSize(20)
34-
->render();
31+
$parser = $this->createTestParser('document-with-includes.md');
32+
$parser->getConfig()->setMaxIncludeSize(20);
33+
34+
$html = $parser->render();
3535

3636
$this->assertStringContainsString('#'.DocParser::ERROR_INCLUDE_FILE_TOO_BIG, $html);
3737
}
38+
39+
public function test_includeErrors() : void
40+
{
41+
$parser = $this->createTestParser('document-with-errors.md');
42+
$parser->getConfig()->setMaxIncludeSize(20);
43+
44+
$html = $parser->render();
45+
46+
$this->assertStringContainsString('#'.DocParser::ERROR_INCLUDE_FILE_NOT_FOUND, $html, 'File not found');
47+
$this->assertStringContainsString('#'.DocParser::ERROR_ATTEMPTED_NAVIGATING_UP, $html, 'Navigating upwards');
48+
$this->assertStringContainsString('#'.DocParser::ERROR_INVALID_INCLUDE_EXTENSION, $html, 'Unknown extension');
49+
$this->assertStringContainsString('#'.DocParser::ERROR_INCLUDE_IS_NOT_A_FILE, $html, 'Not a file');
50+
}
3851
}

tests/TestSuites/Parser/OptionTests.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@
44

55
namespace Mistralys\MarkdownViewerTests\TestSuites\Parser;
66

7+
use Mistralys\MarkdownViewer\DocsConfig;
78
use Mistralys\MarkdownViewerTests\TestClasses\ViewerTestCase;
89

910
final class OptionTests extends ViewerTestCase
1011
{
1112
public function test_addIncludeExtension() : void
1213
{
13-
$parser = $this->createTestParser();
14+
$config = new DocsConfig();
1415

15-
$this->assertNotContains('json', $parser->getIncludeExtensions());
16-
$this->assertNotContains('rtf', $parser->getIncludeExtensions());
16+
$this->assertNotContains('json', $config->getIncludeExtensions());
17+
$this->assertNotContains('rtf', $config->getIncludeExtensions());
1718

18-
$parser->addIncludeExtension('json');
19-
$parser->addIncludeExtension('.rtf');
19+
$config->addIncludeExtension('json');
20+
$config->addIncludeExtension('.rtf');
2021

21-
$this->assertContains('json', $parser->getIncludeExtensions());
22-
$this->assertContains('rtf', $parser->getIncludeExtensions());
22+
$this->assertContains('json', $config->getIncludeExtensions());
23+
$this->assertContains('rtf', $config->getIncludeExtensions());
2324
}
2425

2526
public function test_addIncludeExtensions() : void
2627
{
27-
$parser = $this->createTestParser();
28+
$config = new DocsConfig();
2829

29-
$this->assertNotContains('json', $parser->getIncludeExtensions());
30+
$this->assertNotContains('json', $config->getIncludeExtensions());
3031

31-
$parser->addIncludeExtension('json');
32-
$parser->addIncludeExtension('.rtf');
32+
$config->addIncludeExtension('json');
33+
$config->addIncludeExtension('.rtf');
3334

34-
$this->assertContains('json', $parser->getIncludeExtensions());
35-
$this->assertContains('rtf', $parser->getIncludeExtensions());
35+
$this->assertContains('json', $config->getIncludeExtensions());
36+
$this->assertContains('rtf', $config->getIncludeExtensions());
3637
}
3738
}

tests/files/document-with-errors.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Errors while including files
2+
3+
If an error occurs when loading an include file, the command
4+
is replaced by an error message.
5+
6+
## Include an unknown file
7+
8+
{include-file: unknown-file.txt}
9+
10+
## Include a folder
11+
12+
{include-file: not-a-file.md}
13+
14+
## Include an unsupported extension
15+
16+
{include-file: disallowed-extension.ext}
17+
18+
## Navigate up in relative path
19+
20+
{include-file: ../includes/document-with-includes.md}

tests/files/document-with-includes.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,4 @@ Doc viewer files can be processed to include external files
44
anywhere. For example to load sample code from files instead
55
of maintaining them in the markdown.
66

7-
{include-file: includes/test-php-highlight.php}
8-
9-
If a file does not exist, the command is replaced by an
10-
error message.
11-
12-
{include-file: unknown-file.txt}
13-
14-
Same thing for trying to include a folder:
15-
16-
{include-file: includes/}
17-
18-
Or files that are too big:
19-
20-
7+
{include-file: test-php-highlight.php}

tests/files/includes/disallowed-extension.ext

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Folder used in the unit tests to ensure target
2+
is not a folder looking like a file.

0 commit comments

Comments
 (0)