1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Feature; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Exceptions\UnsupportedPageTypeException; |
6
|
|
|
use Hyde\Framework\Hyde; |
7
|
|
|
use Hyde\Framework\Models\Pages\BladePage; |
8
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
9
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPage; |
10
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
11
|
|
|
use Hyde\Framework\Models\Parsers\DocumentationPageParser; |
12
|
|
|
use Hyde\Framework\Models\Parsers\MarkdownPageParser; |
13
|
|
|
use Hyde\Framework\Models\Parsers\MarkdownPostParser; |
14
|
|
|
use Hyde\Framework\Services\DiscoveryService; |
15
|
|
|
use Hyde\Testing\TestCase; |
16
|
|
|
use Illuminate\Support\Facades\File; |
17
|
|
|
|
18
|
|
|
class DiscoveryServiceTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function createContentSourceTestFiles() |
21
|
|
|
{ |
22
|
|
|
Hyde::touch((DiscoveryService::getModelSourceDirectory(MarkdownPost::class).'/test.md')); |
|
|
|
|
23
|
|
|
Hyde::touch((DiscoveryService::getModelSourceDirectory(MarkdownPage::class).'/test.md')); |
24
|
|
|
Hyde::touch((DiscoveryService::getModelSourceDirectory(DocumentationPage::class).'/test.md')); |
25
|
|
|
Hyde::touch((DiscoveryService::getModelSourceDirectory(BladePage::class).'/test.blade.php')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function deleteContentSourceTestFiles() |
29
|
|
|
{ |
30
|
|
|
unlink(Hyde::path(DiscoveryService::getModelSourceDirectory(MarkdownPost::class).'/test.md')); |
31
|
|
|
unlink(Hyde::path(DiscoveryService::getModelSourceDirectory(MarkdownPage::class).'/test.md')); |
32
|
|
|
unlink(Hyde::path(DiscoveryService::getModelSourceDirectory(DocumentationPage::class).'/test.md')); |
33
|
|
|
unlink(Hyde::path(DiscoveryService::getModelSourceDirectory(BladePage::class).'/test.blade.php')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function test_get_parser_class_for_model() |
37
|
|
|
{ |
38
|
|
|
$this->assertEquals(MarkdownPageParser::class, DiscoveryService::getParserClassForModel(MarkdownPage::class)); |
39
|
|
|
$this->assertEquals(MarkdownPostParser::class, DiscoveryService::getParserClassForModel(MarkdownPost::class)); |
40
|
|
|
$this->assertEquals(DocumentationPageParser::class, DiscoveryService::getParserClassForModel(DocumentationPage::class)); |
41
|
|
|
$this->assertEquals(BladePage::class, DiscoveryService::getParserClassForModel(BladePage::class)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function test_get_parser_instance_for_model() |
45
|
|
|
{ |
46
|
|
|
$this->createContentSourceTestFiles(); |
47
|
|
|
|
48
|
|
|
$this->assertInstanceOf(MarkdownPageParser::class, DiscoveryService::getParserInstanceForModel(MarkdownPage::class, 'test')); |
49
|
|
|
$this->assertInstanceOf(MarkdownPostParser::class, DiscoveryService::getParserInstanceForModel(MarkdownPost::class, 'test')); |
50
|
|
|
$this->assertInstanceOf(DocumentationPageParser::class, DiscoveryService::getParserInstanceForModel(DocumentationPage::class, 'test')); |
51
|
|
|
$this->assertInstanceOf(BladePage::class, DiscoveryService::getParserInstanceForModel(BladePage::class, 'test')); |
52
|
|
|
|
53
|
|
|
$this->deleteContentSourceTestFiles(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function test_get_file_extension_for_model_files() |
57
|
|
|
{ |
58
|
|
|
$this->assertEquals('.md', DiscoveryService::getModelFileExtension(MarkdownPage::class)); |
59
|
|
|
$this->assertEquals('.md', DiscoveryService::getModelFileExtension(MarkdownPost::class)); |
60
|
|
|
$this->assertEquals('.md', DiscoveryService::getModelFileExtension(DocumentationPage::class)); |
61
|
|
|
$this->assertEquals('.blade.php', DiscoveryService::getModelFileExtension(BladePage::class)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function test_get_file_path_for_model_class_files() |
65
|
|
|
{ |
66
|
|
|
$this->assertEquals('_posts', DiscoveryService::getModelSourceDirectory(MarkdownPost::class)); |
67
|
|
|
$this->assertEquals('_pages', DiscoveryService::getModelSourceDirectory(MarkdownPage::class)); |
68
|
|
|
$this->assertEquals('_docs', DiscoveryService::getModelSourceDirectory(DocumentationPage::class)); |
69
|
|
|
$this->assertEquals('_pages', DiscoveryService::getModelSourceDirectory(BladePage::class)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function test_create_clickable_filepath_creates_link_for_existing_file() |
73
|
|
|
{ |
74
|
|
|
$filename = 'be2329d7-3596-48f4-b5b8-deff352246a9'; |
75
|
|
|
touch($filename); |
76
|
|
|
$output = DiscoveryService::createClickableFilepath($filename); |
77
|
|
|
$this->assertStringContainsString('file://', $output); |
78
|
|
|
$this->assertStringContainsString($filename, $output); |
79
|
|
|
unlink($filename); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function test_create_clickable_filepath_falls_back_to_returning_input_if_file_does_not_exist() |
83
|
|
|
{ |
84
|
|
|
$filename = 'be2329d7-3596-48f4-b5b8-deff352246a9'; |
85
|
|
|
$output = DiscoveryService::createClickableFilepath($filename); |
86
|
|
|
$this->assertSame($filename, $output); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function test_get_source_file_list_for_blade_page() |
90
|
|
|
{ |
91
|
|
|
$this->assertEquals(['404', 'index'], DiscoveryService::getBladePageFiles()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function test_get_source_file_list_for_markdown_page() |
95
|
|
|
{ |
96
|
|
|
Hyde::touch(('_pages/foo.md')); |
97
|
|
|
$this->assertEquals(['foo'], DiscoveryService::getMarkdownPageFiles()); |
98
|
|
|
unlink(Hyde::path('_pages/foo.md')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function test_get_source_file_list_for_markdown_post() |
102
|
|
|
{ |
103
|
|
|
Hyde::touch(('_posts/foo.md')); |
104
|
|
|
$this->assertEquals(['foo'], DiscoveryService::getMarkdownPostFiles()); |
105
|
|
|
unlink(Hyde::path('_posts/foo.md')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function test_get_source_file_list_for_documentation_page() |
109
|
|
|
{ |
110
|
|
|
Hyde::touch(('_docs/foo.md')); |
111
|
|
|
$this->assertEquals(['foo'], DiscoveryService::getDocumentationPageFiles()); |
112
|
|
|
unlink(Hyde::path('_docs/foo.md')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function test_get_source_file_list_for_model_method() |
116
|
|
|
{ |
117
|
|
|
$this->unitTestMarkdownBasedPageList(MarkdownPage::class, '_pages/foo.md'); |
118
|
|
|
$this->unitTestMarkdownBasedPageList(MarkdownPost::class, '_posts/foo.md'); |
119
|
|
|
$this->unitTestMarkdownBasedPageList(DocumentationPage::class, '_docs/foo.md'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function test_get_source_file_list_for_model_method_finds_customized_model_properties() |
123
|
|
|
{ |
124
|
|
|
$matrix = [ |
125
|
|
|
MarkdownPage::class, |
126
|
|
|
MarkdownPost::class, |
127
|
|
|
DocumentationPage::class, |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
/** @var MarkdownPage $model */ |
131
|
|
|
foreach ($matrix as $model) { |
132
|
|
|
// Setup |
133
|
|
|
@mkdir(Hyde::path('foo')); |
|
|
|
|
134
|
|
|
$sourceDirectoryBackup = $model::$sourceDirectory; |
|
|
|
|
135
|
|
|
$fileExtensionBackup = $model::$fileExtension; |
|
|
|
|
136
|
|
|
|
137
|
|
|
// Test baseline |
138
|
|
|
$this->unitTestMarkdownBasedPageList($model, $model::$sourceDirectory.'/foo.md'); |
139
|
|
|
|
140
|
|
|
// Set the source directory to a custom value |
141
|
|
|
$model::$sourceDirectory = 'foo'; |
142
|
|
|
|
143
|
|
|
// Test customized source directory |
144
|
|
|
$this->unitTestMarkdownBasedPageList($model, 'foo/foo.md'); |
145
|
|
|
|
146
|
|
|
// Set file extension to a custom value |
147
|
|
|
$model::$fileExtension = '.foo'; |
148
|
|
|
|
149
|
|
|
// Test customized file extension |
150
|
|
|
$this->unitTestMarkdownBasedPageList($model, 'foo/foo.foo', 'foo'); |
151
|
|
|
|
152
|
|
|
// Cleanup |
153
|
|
|
File::deleteDirectory(Hyde::path('foo')); |
154
|
|
|
$model::$sourceDirectory = $sourceDirectoryBackup; |
155
|
|
|
$model::$fileExtension = $fileExtensionBackup; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function test_get_source_file_list_throws_exception_for_invalid_model_class() |
160
|
|
|
{ |
161
|
|
|
$this->expectException(UnsupportedPageTypeException::class); |
162
|
|
|
|
163
|
|
|
DiscoveryService::getSourceFileListForModel('NonExistentModel'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function test_get_media_asset_files() |
167
|
|
|
{ |
168
|
|
|
$this->assertTrue(is_array(DiscoveryService::getMediaAssetFiles())); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function test_get_media_asset_files_discovers_files() |
172
|
|
|
{ |
173
|
|
|
$testFiles = [ |
174
|
|
|
'png', |
175
|
|
|
'svg', |
176
|
|
|
'jpg', |
177
|
|
|
'jpeg', |
178
|
|
|
'gif', |
179
|
|
|
'ico', |
180
|
|
|
'css', |
181
|
|
|
'js', |
182
|
|
|
]; |
183
|
|
|
foreach ($testFiles as $fileType) { |
184
|
|
|
$path = Hyde::path('_media/test.'.$fileType); |
185
|
|
|
touch($path); |
186
|
|
|
$this->assertContains($path, DiscoveryService::getMediaAssetFiles()); |
187
|
|
|
unlink($path); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function test_get_media_asset_files_discovers_custom_file_types() |
192
|
|
|
{ |
193
|
|
|
$path = Hyde::path('_media/test.custom'); |
194
|
|
|
touch($path); |
195
|
|
|
$this->assertNotContains($path, DiscoveryService::getMediaAssetFiles()); |
196
|
|
|
config(['hyde.media_extensions' => 'custom']); |
197
|
|
|
$this->assertContains($path, DiscoveryService::getMediaAssetFiles()); |
198
|
|
|
unlink($path); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function test_blade_page_files_starting_with_underscore_are_ignored() |
202
|
|
|
{ |
203
|
|
|
Hyde::touch(('_pages/_foo.blade.php')); |
204
|
|
|
$this->assertEquals([ |
205
|
|
|
'404', |
206
|
|
|
'index', |
207
|
|
|
], DiscoveryService::getBladePageFiles()); |
208
|
|
|
unlink(Hyde::path('_pages/_foo.blade.php')); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function test_markdown_page_files_starting_with_underscore_are_ignored() |
212
|
|
|
{ |
213
|
|
|
Hyde::touch(('_pages/_foo.md')); |
214
|
|
|
$this->assertEquals([], DiscoveryService::getMarkdownPageFiles()); |
215
|
|
|
unlink(Hyde::path('_pages/_foo.md')); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function test_post_files_starting_with_underscore_are_ignored() |
219
|
|
|
{ |
220
|
|
|
Hyde::touch(('_posts/_foo.md')); |
221
|
|
|
$this->assertEquals([], DiscoveryService::getMarkdownPostFiles()); |
222
|
|
|
unlink(Hyde::path('_posts/_foo.md')); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function test_documentation_page_files_starting_with_underscore_are_ignored() |
226
|
|
|
{ |
227
|
|
|
Hyde::touch(('_docs/_foo.md')); |
228
|
|
|
$this->assertEquals([], DiscoveryService::getDocumentationPageFiles()); |
229
|
|
|
unlink(Hyde::path('_docs/_foo.md')); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected function unitTestMarkdownBasedPageList(string $model, string $path, ?string $expected = null) |
233
|
|
|
{ |
234
|
|
|
Hyde::touch(($path)); |
235
|
|
|
|
236
|
|
|
$expected = $expected ?? basename($path, '.md'); |
237
|
|
|
|
238
|
|
|
$this->assertEquals([$expected], DiscoveryService::getSourceFileListForModel($model)); |
239
|
|
|
|
240
|
|
|
unlink(Hyde::path($path)); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|