Passed
Push — master ( 6956bd...dff5c5 )
by Caen
03:50 queued 12s
created

test_markdown_page_implements_page_contract()

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Testing\Feature;
4
5
use Hyde\Framework\Concerns\HasDynamicTitle;
6
use Hyde\Framework\Contracts\AbstractMarkdownPage;
7
use Hyde\Framework\Contracts\AbstractPage;
8
use Hyde\Framework\Contracts\PageContract;
9
use Hyde\Framework\Hyde;
10
use Hyde\Framework\Models\MarkdownDocument;
11
use Hyde\Framework\Models\Pages\BladePage;
12
use Hyde\Framework\Models\Pages\DocumentationPage;
13
use Hyde\Framework\Models\Pages\MarkdownPage;
14
use Hyde\Framework\Models\Pages\MarkdownPost;
15
use Hyde\Framework\Models\Parsers\MarkdownPageParser;
16
use Hyde\Framework\Models\Parsers\MarkdownPostParser;
17
use Hyde\Framework\Models\Route;
18
use Hyde\Testing\TestCase;
19
20
/**
21
 * Test the AbstractPage class.
22
 *
23
 * Since the class is abstract, we can't test it directly,
24
 * so we will use the MarkdownPage class as a proxy,
25
 * since it's the simplest implementation.
26
 *
27
 * @covers \Hyde\Framework\Contracts\AbstractPage
28
 * @covers \Hyde\Framework\Contracts\AbstractMarkdownPage
29
 *
30
 * @backupStaticAttributes enabled
31
 */
32
class AbstractPageTest extends TestCase
33
{
34
    public function test_get_source_directory_returns_static_property()
35
    {
36
        MarkdownPage::$sourceDirectory = 'foo';
37
        $this->assertEquals('foo', MarkdownPage::getSourceDirectory());
38
    }
39
40
    public function test_get_source_directory_trims_trailing_slashes()
41
    {
42
        MarkdownPage::$sourceDirectory = '/foo/\\';
43
        $this->assertEquals('foo', MarkdownPage::getSourceDirectory());
44
    }
45
46
    public function test_get_output_directory_returns_static_property()
47
    {
48
        MarkdownPage::$outputDirectory = 'foo';
49
        $this->assertEquals('foo', MarkdownPage::getOutputDirectory());
50
    }
51
52
    public function test_get_output_directory_trims_trailing_slashes()
53
    {
54
        MarkdownPage::$outputDirectory = '/foo/\\';
55
        $this->assertEquals('foo', MarkdownPage::getOutputDirectory());
56
    }
57
58
    public function test_get_file_extension_returns_static_property()
59
    {
60
        MarkdownPage::$fileExtension = '.foo';
61
        $this->assertEquals('.foo', MarkdownPage::getFileExtension());
62
    }
63
64
    public function test_get_file_extension_forces_leading_period()
65
    {
66
        MarkdownPage::$fileExtension = 'foo';
67
        $this->assertEquals('.foo', MarkdownPage::getFileExtension());
68
    }
69
70
    public function test_get_parser_class_returns_static_property()
71
    {
72
        MarkdownPage::$parserClass = 'foo';
73
        $this->assertEquals('foo', MarkdownPage::getParserClass());
74
    }
75
76
    public function test_get_parser_returns_the_configured_parser_class()
77
    {
78
        Hyde::touch(('_posts/foo.md'));
0 ignored issues
show
Bug introduced by
The method touch() does not exist on Hyde\Framework\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        Hyde::/** @scrutinizer ignore-call */ 
79
              touch(('_posts/foo.md'));
Loading history...
79
80
        MarkdownPage::$parserClass = MarkdownPostParser::class;
81
        $this->assertInstanceOf(MarkdownPostParser::class, MarkdownPage::getParser('foo'));
82
83
        unlink(Hyde::path('_posts/foo.md'));
84
    }
85
86
    public function test_get_parser_returns_instantiated_parser_for_the_supplied_slug()
87
    {
88
        Hyde::touch(('_pages/foo.md'));
89
90
        $this->assertInstanceOf(MarkdownPageParser::class, $parser = MarkdownPage::getParser('foo'));
91
        $this->assertEquals('foo', $parser->get()->slug);
92
93
        unlink(Hyde::path('_pages/foo.md'));
94
    }
95
96
    public function test_parse_parses_supplied_slug_into_a_page_model()
97
    {
98
        Hyde::touch(('_pages/foo.md'));
99
100
        $this->assertInstanceOf(MarkdownPage::class, $page = MarkdownPage::parse('foo'));
101
        $this->assertEquals('foo', $page->slug);
102
103
        unlink(Hyde::path('_pages/foo.md'));
104
    }
105
106
    public function test_files_returns_array_of_source_files()
107
    {
108
        Hyde::touch(('_pages/foo.md'));
109
        $this->assertEquals(['foo'], MarkdownPage::files());
110
        unlink(Hyde::path('_pages/foo.md'));
111
    }
112
113
    public function test_all_returns_collection_of_all_source_files_parsed_into_the_model()
114
    {
115
        Hyde::touch(('_pages/foo.md'));
116
        $this->assertEquals(
117
            collect([new MarkdownPage([], '', '', 'foo')]),
0 ignored issues
show
Bug introduced by
array(new Hyde\Framework...rray(), '', '', 'foo')) of type array<integer,Hyde\Frame...els\Pages\MarkdownPage> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
            collect(/** @scrutinizer ignore-type */ [new MarkdownPage([], '', '', 'foo')]),
Loading history...
118
            MarkdownPage::all()
119
        );
120
        unlink(Hyde::path('_pages/foo.md'));
121
    }
122
123
    public function test_qualify_basename_properly_expands_basename_for_the_model()
124
    {
125
        $this->assertEquals('_pages/foo.md', MarkdownPage::qualifyBasename('foo'));
126
    }
127
128
    public function test_qualify_basename_trims_slashes_from_input()
129
    {
130
        $this->assertEquals('_pages/foo.md', MarkdownPage::qualifyBasename('/foo/\\'));
131
    }
132
133
    public function test_qualify_basename_uses_the_static_properties()
134
    {
135
        MarkdownPage::$sourceDirectory = 'foo';
136
        MarkdownPage::$fileExtension = 'txt';
137
        $this->assertEquals('foo/bar.txt', MarkdownPage::qualifyBasename('bar'));
138
    }
139
140
    public function test_get_output_location_returns_the_file_output_path_for_the_supplied_basename()
141
    {
142
        $this->assertEquals('foo.html', MarkdownPage::getOutputLocation('foo'));
143
    }
144
145
    public function test_get_output_location_returns_the_configured_location()
146
    {
147
        MarkdownPage::$outputDirectory = 'foo';
148
        $this->assertEquals('foo/bar.html', MarkdownPage::getOutputLocation('bar'));
149
    }
150
151
    public function test_get_output_location_trims_trailing_slashes_from_directory_setting()
152
    {
153
        MarkdownPage::$outputDirectory = '/foo/\\';
154
        $this->assertEquals('foo/bar.html', MarkdownPage::getOutputLocation('bar'));
155
    }
156
157
    public function test_get_output_location_trims_trailing_slashes_from_basename()
158
    {
159
        $this->assertEquals('foo.html', MarkdownPage::getOutputLocation('/foo/\\'));
160
    }
161
162
    public function test_get_current_page_path_returns_output_directory_and_basename()
163
    {
164
        $page = new MarkdownPage([], '', '', 'foo');
165
        $this->assertEquals('foo', $page->getCurrentPagePath());
166
    }
167
168
    public function test_get_current_page_path_returns_output_directory_and_basename_for_configured_directory()
169
    {
170
        MarkdownPage::$outputDirectory = 'foo';
171
        $page = new MarkdownPage([], '', '', 'bar');
172
        $this->assertEquals('foo/bar', $page->getCurrentPagePath());
173
    }
174
175
    public function test_get_current_page_path_trims_trailing_slashes_from_directory_setting()
176
    {
177
        MarkdownPage::$outputDirectory = '/foo/\\';
178
        $page = new MarkdownPage([], '', '', 'bar');
179
        $this->assertEquals('foo/bar', $page->getCurrentPagePath());
180
    }
181
182
    public function test_get_output_path_returns_current_page_path_with_html_extension_appended()
183
    {
184
        $page = new MarkdownPage([], '', '', 'foo');
185
        $this->assertEquals('foo.html', $page->getOutputPath());
186
    }
187
188
    public function test_get_source_path_returns_qualified_basename()
189
    {
190
        $this->assertEquals(
191
            MarkdownPage::qualifyBasename('foo'),
192
            (new MarkdownPage(slug: 'foo'))->getSourcePath()
193
        );
194
    }
195
196
    public function test_markdown_page_implements_page_contract()
197
    {
198
        $this->assertInstanceOf(PageContract::class, new class extends AbstractPage {});
199
    }
200
201
    public function test_all_page_models_extend_abstract_page()
202
    {
203
        $pages = [
204
            MarkdownPage::class,
205
            MarkdownPost::class,
206
            DocumentationPage::class,
207
        ];
208
209
        foreach ($pages as $page) {
210
            $this->assertInstanceOf(AbstractPage::class, new $page());
211
        }
212
213
        $this->assertInstanceOf(AbstractPage::class, new BladePage('foo'));
214
    }
215
216
    public function test_all_page_models_have_configured_source_directory()
217
    {
218
        $pages = [
219
            BladePage::class => '_pages',
220
            MarkdownPage::class => '_pages',
221
            MarkdownPost::class => '_posts',
222
            DocumentationPage::class => '_docs',
223
        ];
224
225
        foreach ($pages as $page => $expected) {
226
            $this->assertEquals($expected, $page::$sourceDirectory);
0 ignored issues
show
Bug introduced by
The property sourceDirectory does not exist on string.
Loading history...
227
        }
228
    }
229
230
    public function test_all_page_models_have_configured_output_directory()
231
    {
232
        $pages = [
233
            BladePage::class => '',
234
            MarkdownPage::class => '',
235
            MarkdownPost::class => 'posts',
236
            DocumentationPage::class => 'docs',
237
        ];
238
239
        foreach ($pages as $page => $expected) {
240
            $this->assertEquals($expected, $page::$outputDirectory);
0 ignored issues
show
Bug introduced by
The property outputDirectory does not exist on string.
Loading history...
241
        }
242
    }
243
244
    public function test_all_page_models_have_configured_file_extension()
245
    {
246
        $pages = [
247
            BladePage::class => '.blade.php',
248
            MarkdownPage::class => '.md',
249
            MarkdownPost::class => '.md',
250
            DocumentationPage::class => '.md',
251
        ];
252
253
        foreach ($pages as $page => $expected) {
254
            $this->assertEquals($expected, $page::$fileExtension);
0 ignored issues
show
Bug introduced by
The property fileExtension does not exist on string.
Loading history...
255
        }
256
    }
257
258
    public function test_all_page_models_have_configured_parser_class()
259
    {
260
        $pages = [
261
            BladePage::class => 'Hyde\Framework\Models\Pages\BladePage',
262
            MarkdownPage::class => 'Hyde\Framework\Models\Parsers\MarkdownPageParser',
263
            MarkdownPost::class => 'Hyde\Framework\Models\Parsers\MarkdownPostParser',
264
            DocumentationPage::class => 'Hyde\Framework\Models\Parsers\DocumentationPageParser',
265
        ];
266
267
        foreach ($pages as $page => $expected) {
268
            $this->assertEquals($expected, $page::$parserClass);
0 ignored issues
show
Bug introduced by
The property parserClass does not exist on string.
Loading history...
269
        }
270
    }
271
272
    public function test_abstract_markdown_page_extends_abstract_page()
273
    {
274
        $this->assertInstanceOf(AbstractPage::class, new class extends AbstractMarkdownPage {});
275
    }
276
277
    public function test_abstract_markdown_page_implements_page_contract()
278
    {
279
        $this->assertInstanceOf(PageContract::class, new class extends AbstractMarkdownPage {});
280
    }
281
282
    public function test_abstract_markdown_page_uses_has_dynamic_title_trait()
283
    {
284
        $this->assertContains(HasDynamicTitle::class, class_uses_recursive(AbstractMarkdownPage::class));
285
    }
286
287
    public function test_abstract_markdown_page_has_markdown_document_property()
288
    {
289
        $this->assertClassHasAttribute('markdown', AbstractMarkdownPage::class);
290
    }
291
292
    public function test_abstract_markdown_page_has_file_extension_property()
293
    {
294
        $this->assertClassHasAttribute('fileExtension', AbstractMarkdownPage::class);
295
    }
296
297
    public function test_abstract_markdown_page_file_extension_property_is_set_to_md()
298
    {
299
        $this->assertEquals('.md', AbstractMarkdownPage::$fileExtension);
300
    }
301
302
    public function test_abstract_markdown_page_constructor_arguments_are_optional()
303
    {
304
        $page = new class extends AbstractMarkdownPage {};
305
        $this->assertInstanceOf(AbstractMarkdownPage::class, $page); // If we get this far, we're good as no exception was thrown
306
    }
307
308
    public function test_abstract_markdown_page_constructor_assigns_markdown_document_property_if_set()
309
    {
310
        $document = new MarkdownDocument();
311
        $page = new MarkdownPage(markdownDocument: $document);
312
        $this->assertSame($document, $page->markdown);
313
    }
314
315
    public function test_abstract_markdown_page_constructor_creates_new_markdown_document_if_no_markdown_document_is_set()
316
    {
317
        $page = new MarkdownPage();
318
        $this->assertInstanceOf(MarkdownDocument::class, $page->markdown);
319
    }
320
321
    public function test_abstract_markdown_page_markdown_helper_returns_the_markdown_document_instance()
322
    {
323
        $page = new MarkdownPage();
324
        $this->assertSame($page->markdown, $page->markdown());
325
    }
326
327
    public function test_abstract_markdown_page_markdown_helper_returns_the_configured_markdown_document_instance()
328
    {
329
        $document = new MarkdownDocument();
330
        $page = new MarkdownPage(markdownDocument: $document);
331
        $this->assertSame($document, $page->markdown());
332
    }
333
334
    public function test_abstract_markdown_page_constructor_constructs_dynamic_title_automatically()
335
    {
336
        $page = new MarkdownPage(['title' => 'Foo']);
337
        $this->assertEquals('Foo', $page->title);
338
    }
339
340
    public function test_markdown_based_pages_extend_abstract_markdown_page()
341
    {
342
        $pages = [
343
            MarkdownPage::class,
344
            MarkdownPost::class,
345
            DocumentationPage::class,
346
        ];
347
348
        foreach ($pages as $page) {
349
            $this->assertInstanceOf(AbstractMarkdownPage::class, new $page());
350
        }
351
    }
352
353
    public function test_blade_pages_do_not_extend_abstract_markdown_page()
354
    {
355
        $this->assertNotInstanceOf(AbstractMarkdownPage::class, new BladePage('foo'));
356
    }
357
358
    public function test_get_route_returns_page_route()
359
    {
360
        $page = new MarkdownPage();
361
        $this->assertEquals(new Route($page), $page->getRoute());
362
    }
363
364
    public function test_html_title_returns_site_name_plus_page_title()
365
    {
366
        $this->assertEquals('HydePHP - Foo', (new MarkdownPage(['title' => 'Foo']))->htmlTitle());
367
    }
368
369
    public function test_html_title_can_be_overridden()
370
    {
371
        $this->assertEquals('HydePHP - Bar', (new MarkdownPage(['title' => 'Foo']))->htmlTitle('Bar'));
372
    }
373
374
    public function test_html_title_returns_site_name_if_no_page_title()
375
    {
376
        $this->assertEquals('HydePHP', (new MarkdownPage())->htmlTitle());
377
    }
378
379
    public function test_html_title_uses_configured_site_name()
380
    {
381
        config(['site.name' => 'Foo Bar']);
382
        $this->assertEquals('Foo Bar', (new MarkdownPage())->htmlTitle());
383
    }
384
}
385