Passed
Push — master ( a80842...bbbee0 )
by Caen
03:37 queued 13s
created

PublicationsExtensionTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 141
dl 0
loc 267
rs 10
c 1
b 0
f 0
wmc 19
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Foundation\Kernel\FileCollection;
8
use Hyde\Foundation\Kernel\PageCollection;
9
use Hyde\Foundation\Kernel\RouteCollection;
10
use Hyde\Hyde;
11
use Hyde\Pages\InMemoryPage;
12
use Hyde\Publications\Models\PublicationType;
13
use Hyde\Publications\Pages\PublicationListPage;
14
use Hyde\Publications\Pages\PublicationPage;
15
use Hyde\Publications\PublicationsExtension;
16
use Hyde\Support\Filesystem\MediaFile;
17
use Hyde\Support\Filesystem\SourceFile;
18
use Hyde\Support\Models\Route;
19
use Hyde\Testing\TestCase;
20
21
/**
22
 * @covers \Hyde\Publications\PublicationsExtension
23
 */
24
class PublicationsExtensionTest extends TestCase
25
{
26
    protected function setUp(): void
27
    {
28
        parent::setUp();
29
30
        $this->withoutDefaultPages();
31
    }
32
33
    protected function tearDown(): void
34
    {
35
        $this->restoreDefaultPages();
36
37
        parent::tearDown();
38
    }
39
40
    public function test_get_page_classes_method()
41
    {
42
        $this->assertSame([], PublicationsExtension::getPageClasses());
43
    }
44
45
    public function test_get_types_method()
46
    {
47
        $extension = new PublicationsExtension;
48
        $extension->discoverFiles(Hyde::files());
49
        $this->assertSame([], $extension->getTypes()->toArray());
50
    }
51
52
    public function test_get_types_method_with_types()
53
    {
54
        $this->createPublication();
55
56
        $extension = new PublicationsExtension;
57
        $extension->discoverFiles(Hyde::files());
58
        $this->assertEquals(['publication' => PublicationType::get('publication')], $extension->getTypes()->all());
59
    }
60
61
    public function test_get_types_method_with_multiple_types()
62
    {
63
        $this->createPublication();
64
65
        $this->directory('publication2');
66
        (new PublicationType('publication2'))->save();
67
68
        $extension = new PublicationsExtension;
69
        $extension->discoverFiles(Hyde::files());
70
        $this->assertEquals([
71
            'publication' => PublicationType::get('publication'),
72
            'publication2' => PublicationType::get('publication2'),
73
        ], $extension->getTypes()->all());
74
    }
75
76
    public function test_publication_files_are_discovered()
77
    {
78
        $this->createPublication();
79
80
        $booted = FileCollection::init(Hyde::getInstance())->boot();
81
82
        $files = $booted->getFiles()->keys()->toArray();
83
        $this->assertSame(['publication/foo.md'], $files);
84
85
        $this->assertInstanceOf(SourceFile::class, $booted->getFiles()->get('publication/foo.md'));
86
        $this->assertEquals(new SourceFile('publication/foo.md', PublicationPage::class),
87
            $booted->getFiles()->get('publication/foo.md')
88
        );
89
    }
90
91
    public function test_publication_files_are_discovered_for_multiple_types()
92
    {
93
        $this->createPublication();
94
95
        $this->directory('publication2');
96
        (new PublicationType('publication2'))->save();
97
        $this->file('publication2/bar.md', 'bar');
98
99
        $booted = FileCollection::init(Hyde::getInstance())->boot();
100
101
        $files = $booted->getFiles()->keys()->toArray();
102
        $this->assertSame(['publication/foo.md', 'publication2/bar.md'], $files);
103
    }
104
105
    public function test_publication_media_files_are_discovered()
106
    {
107
        $this->directory('_media/publication');
108
        $this->file('_media/publication/foo.jpg', 'foo');
109
110
        $files = collect(MediaFile::all());
111
        $this->assertSame(['app.css', 'publication/foo.jpg'], MediaFile::files());
112
        $this->assertInstanceOf(MediaFile::class, $files->get('publication/foo.jpg'));
113
    }
114
115
    public function test_base_publication_pages_are_discovered()
116
    {
117
        $this->createPublication();
118
119
        $this->assertSame([
120
            'publication/foo.md',
121
            'publication/index',
122
        ], PageCollection::init(Hyde::getInstance())->boot()->getPages()->keys()->toArray());
123
    }
124
125
    public function test_publication_pages_are_discovered()
126
    {
127
        $this->createPublication();
128
129
        $booted = PageCollection::init(Hyde::getInstance())->boot();
130
131
        $this->assertSame([
132
            'publication/foo.md',
133
        ], $booted->getPages(PublicationPage::class)->keys()->toArray());
134
135
        $this->assertInstanceOf(PublicationPage::class, $booted->getPages()->get('publication/foo.md'));
136
        $this->assertEquals(new PublicationPage('foo', [], '', PublicationType::get('publication')),
137
            $booted->getPages()->get('publication/foo.md')
138
        );
139
    }
140
141
    public function test_listing_pages_for_publications_are_generated()
142
    {
143
        $this->createPublication();
144
        $booted = PageCollection::init(Hyde::getInstance())->boot();
145
146
        $this->assertSame([
147
            'publication/index',
148
        ], $booted->getPages(PublicationListPage::class)->keys()->toArray());
149
150
        $this->assertInstanceOf(PublicationListPage::class, $booted->getPages()->get('publication/index'));
151
        $this->assertEquals(new PublicationListPage(PublicationType::get('publication')),
152
            $booted->getPages()->get('publication/index')
153
        );
154
    }
155
156
    public function test_paginated_listing_pages_for_publications_are_generated()
157
    {
158
        $this->createPublication();
159
        (new PublicationType('publication', pageSize: 1))->save();
160
        (new PublicationPage('bar', [], '', PublicationType::get('publication')))->save();
161
162
        $booted = PageCollection::init(Hyde::getInstance())->boot();
163
164
        $this->assertInstanceOf(PublicationListPage::class, $booted->getPage('publication/index'));
165
        $this->assertInstanceOf(InMemoryPage::class, $booted->getPage('publication/page-1'));
166
        $this->assertInstanceOf(InMemoryPage::class, $booted->getPage('publication/page-2'));
167
    }
168
169
    public function test_publication_tag_list_pages_are_generated()
170
    {
171
        $this->directory('publication');
172
173
        (new PublicationType('publication', fields: [
174
            ['name' => 'tags', 'type' => 'tag'],
175
        ]))->save();
176
177
        $this->markdown('publication/foo.md', matter: ['tags' => ['foo', 'bar']]);
178
179
        $booted = PageCollection::init(Hyde::getInstance())->boot();
180
181
        $this->assertInstanceOf(InMemoryPage::class, $booted->getPage('tags/index'));
182
    }
183
184
    public function test_publication_tag_list_routes_with_tags_are_generated()
185
    {
186
        $this->createPublication();
187
        (new PublicationType('publication', fields: [
188
            ['name' => 'tags', 'type' => 'tag'],
189
        ]))->save();
190
191
        $this->markdown('publication/foo.md', matter: ['tags' => ['foo', 'bar']]);
192
193
        $booted = PageCollection::init(Hyde::getInstance())->boot();
194
        $routes = $booted->getPages()->keys()->toArray();
195
196
        $this->assertSame([
197
            'publication/foo.md',
198
            'publication/index',
199
            'tags/index',
200
            'tags/foo',
201
            'tags/bar',
202
        ], $routes);
203
204
        // test tags
205
        $tagPage = $booted->getPages()->get('tags/index');
206
        $this->assertInstanceOf(InMemoryPage::class, $tagPage);
207
        $this->assertSame(['foo' => 1, 'bar' => 1], $tagPage->matter('tags'));
208
    }
209
210
    public function test_publication_routes_are_discovered()
211
    {
212
        $this->createPublication();
213
214
        $booted = RouteCollection::init(Hyde::getInstance())->boot();
215
        $routes = $booted->getRoutes()->keys()->toArray();
216
217
        $this->assertSame([
218
            'publication/foo',
219
            'publication/index',
220
        ], $routes);
221
222
        $this->assertContainsOnlyInstancesOf(Route::class, $booted->getRoutes());
223
224
        $this->assertEquals(new Route(new PublicationPage('foo', [], '', PublicationType::get('publication'))),
225
            $booted->getRoutes()->get('publication/foo')
226
        );
227
228
        $this->assertEquals(new Route(new PublicationListPage(PublicationType::get('publication'))),
229
            $booted->getRoutes()->get('publication/index')
230
        );
231
    }
232
233
    public function test_publication_tag_list_routes_are_discovered()
234
    {
235
        $this->directory('publication');
236
237
        (new PublicationType('publication', fields: [
238
            ['name' => 'tag', 'type' => 'tag'],
239
        ]))->save();
240
241
        $this->markdown('publication/foo.md', matter: ['tag' => ['foo']]);
242
243
        $booted = RouteCollection::init(Hyde::getInstance())->boot();
244
        $routes = $booted->getRoutes()->keys()->toArray();
245
246
        $this->assertSame([
247
            'publication/foo',
248
            'publication/index',
249
            'tags/index',
250
            'tags/foo',
251
        ], $routes);
252
253
        // test tags
254
        $tagPage = $booted->getRoutes()->get('tags/index')->getPage();
255
        $this->assertInstanceOf(InMemoryPage::class, $tagPage);
256
        $this->assertSame(['foo' => 1], $tagPage->matter('tags'));
257
    }
258
259
    public function test_publication_tag_list_routes_with_tags_are_discovered()
260
    {
261
        $this->createPublication();
262
        (new PublicationType('publication', fields: [
263
            ['name' => 'tags', 'type' => 'tag'],
264
        ]))->save();
265
266
        $this->markdown('publication/foo.md', matter: ['tags' => ['foo', 'bar']]);
267
268
        $booted = RouteCollection::init(Hyde::getInstance())->boot();
269
        $routes = $booted->getRoutes()->keys()->toArray();
270
271
        $this->assertSame([
272
            'publication/foo',
273
            'publication/index',
274
            'tags/index',
275
            'tags/foo',
276
            'tags/bar',
277
        ], $routes);
278
279
        // test tags
280
        $tagPage = $booted->getRoutes()->get('tags/index')->getPage();
281
        $this->assertInstanceOf(InMemoryPage::class, $tagPage);
282
        $this->assertSame(['foo' => 1, 'bar' => 1], $tagPage->matter('tags'));
283
    }
284
285
    protected function createPublication(): void
286
    {
287
        $this->directory('publication');
288
289
        (new PublicationType('publication'))->save();
290
        (new PublicationPage('foo', [], '', PublicationType::get('publication')))->save();
291
    }
292
}
293