|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Feature; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Hyde; |
|
6
|
|
|
use Hyde\Framework\Models\MarkdownDocument; |
|
7
|
|
|
use Hyde\Framework\Modules\Markdown\MarkdownFileParser; |
|
8
|
|
|
use Hyde\Testing\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class MarkdownFileParserTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
protected function makeTestPost(): void |
|
13
|
|
|
{ |
|
14
|
|
|
file_put_contents(Hyde::path('_posts/test-post.md'), '--- |
|
15
|
|
|
title: My New Post |
|
16
|
|
|
category: blog |
|
17
|
|
|
author: Mr. Hyde |
|
18
|
|
|
--- |
|
19
|
|
|
|
|
20
|
|
|
# My New Post |
|
21
|
|
|
|
|
22
|
|
|
This is a post stub used in the automated tests |
|
23
|
|
|
'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function tearDown(): void |
|
27
|
|
|
{ |
|
28
|
|
|
unlink(Hyde::path('_posts/test-post.md')); |
|
29
|
|
|
|
|
30
|
|
|
parent::tearDown(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_can_parse_markdown_file() |
|
34
|
|
|
{ |
|
35
|
|
|
file_put_contents(Hyde::path('_posts/test-post.md'), 'Foo bar'); |
|
36
|
|
|
|
|
37
|
|
|
$document = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get(); |
|
38
|
|
|
$this->assertInstanceOf(MarkdownDocument::class, $document); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals('Foo bar', $document->body); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_can_parse_markdown_file_with_front_matter() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->makeTestPost(); |
|
46
|
|
|
|
|
47
|
|
|
$document = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get(); |
|
48
|
|
|
$this->assertInstanceOf(MarkdownDocument::class, $document); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals([ |
|
51
|
|
|
'title' => 'My New Post', |
|
52
|
|
|
'category' => 'blog', |
|
53
|
|
|
'author' => 'Mr. Hyde', |
|
54
|
|
|
], $document->matter); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertEquals( |
|
57
|
|
|
'# My New PostThis is a post stub used in the automated tests', |
|
58
|
|
|
str_replace(["\n", "\r"], '', $document->body) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function test_parsed_markdown_post_contains_valid_front_matter() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->makeTestPost(); |
|
65
|
|
|
|
|
66
|
|
|
$post = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get(); |
|
67
|
|
|
$this->assertEquals('My New Post', $post->matter['title']); |
|
68
|
|
|
$this->assertEquals('Mr. Hyde', $post->matter['author']); |
|
69
|
|
|
$this->assertEquals('blog', $post->matter['category']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function test_parsed_front_matter_does_not_contain_slug_key() |
|
73
|
|
|
{ |
|
74
|
|
|
file_put_contents(Hyde::path('_posts/test-post.md'), "---\nslug: foo\n---\n"); |
|
75
|
|
|
|
|
76
|
|
|
$post = (new MarkdownFileParser(Hyde::path('_posts/test-post.md')))->get(); |
|
77
|
|
|
$this->assertArrayNotHasKey('slug', $post->matter); |
|
78
|
|
|
$this->assertEquals([], $post->matter); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function test_static_parse_shorthand() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->makeTestPost(); |
|
84
|
|
|
|
|
85
|
|
|
$post = MarkdownFileParser::parse(Hyde::path('_posts/test-post.md')); |
|
86
|
|
|
$this->assertEquals('My New Post', $post->matter['title']); |
|
87
|
|
|
$this->assertEquals('Mr. Hyde', $post->matter['author']); |
|
88
|
|
|
$this->assertEquals('blog', $post->matter['category']); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals( |
|
91
|
|
|
'# My New PostThis is a post stub used in the automated tests', |
|
92
|
|
|
str_replace(["\n", "\r"], '', $post->body) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|