|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Feature\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\ConvertsArrayToFrontMatter; |
|
6
|
|
|
use Hyde\Framework\Hyde; |
|
7
|
|
|
use Hyde\Framework\Models\DocumentationSidebar; |
|
8
|
|
|
use Hyde\Framework\Models\NavItem; |
|
9
|
|
|
use Hyde\Framework\Models\Route; |
|
10
|
|
|
use Hyde\Testing\TestCase; |
|
11
|
|
|
use Illuminate\Support\Facades\Config; |
|
12
|
|
|
use Illuminate\Support\Facades\File; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @covers \Hyde\Framework\Models\DocumentationSidebar |
|
16
|
|
|
*/ |
|
17
|
|
|
class DocumentationSidebarTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
protected function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
parent::setUp(); |
|
22
|
|
|
|
|
23
|
|
|
$this->resetDocs(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function tearDown(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->resetDocs(); |
|
29
|
|
|
|
|
30
|
|
|
parent::tearDown(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_sidebar_can_be_created() |
|
34
|
|
|
{ |
|
35
|
|
|
$sidebar = DocumentationSidebar::create(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertInstanceOf(DocumentationSidebar::class, $sidebar); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function test_sidebar_items_are_added_automatically() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->createTestFiles(); |
|
43
|
|
|
|
|
44
|
|
|
$sidebar = DocumentationSidebar::create(); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertCount(5, $sidebar->items); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function test_index_page_is_removed_from_sidebar() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->createTestFiles(); |
|
52
|
|
|
Hyde::touch(('_docs/index.md')); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$sidebar = DocumentationSidebar::create(); |
|
55
|
|
|
$this->assertCount(5, $sidebar->items); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function test_files_with_front_matter_hidden_set_to_true_are_removed_from_sidebar() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->createTestFiles(); |
|
61
|
|
|
File::put(Hyde::path('_docs/test.md'), "---\nhidden: true\n---\n\n# Foo"); |
|
62
|
|
|
|
|
63
|
|
|
$sidebar = DocumentationSidebar::create(); |
|
64
|
|
|
$this->assertCount(5, $sidebar->items); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_sidebar_is_ordered_alphabetically_when_no_order_is_set_in_config() |
|
68
|
|
|
{ |
|
69
|
|
|
Config::set('docs.sidebar_order', []); |
|
70
|
|
|
Hyde::touch(('_docs/a.md')); |
|
71
|
|
|
Hyde::touch(('_docs/b.md')); |
|
72
|
|
|
Hyde::touch(('_docs/c.md')); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals( |
|
75
|
|
|
collect([ |
|
|
|
|
|
|
76
|
|
|
NavItem::fromRoute(Route::get('docs/a'))->setPriority(500), |
|
77
|
|
|
NavItem::fromRoute(Route::get('docs/b'))->setPriority(500), |
|
78
|
|
|
NavItem::fromRoute(Route::get('docs/c'))->setPriority(500), |
|
79
|
|
|
]), |
|
80
|
|
|
DocumentationSidebar::create()->items |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function test_sidebar_is_ordered_by_priority_when_priority_is_set_in_config() |
|
85
|
|
|
{ |
|
86
|
|
|
Config::set('docs.sidebar_order', [ |
|
87
|
|
|
'c', |
|
88
|
|
|
'b', |
|
89
|
|
|
'a', |
|
90
|
|
|
]); |
|
91
|
|
|
Hyde::touch(('_docs/a.md')); |
|
92
|
|
|
Hyde::touch(('_docs/b.md')); |
|
93
|
|
|
Hyde::touch(('_docs/c.md')); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertEquals( |
|
96
|
|
|
collect([ |
|
|
|
|
|
|
97
|
|
|
NavItem::fromRoute(Route::get('docs/c'))->setPriority(250), |
|
98
|
|
|
NavItem::fromRoute(Route::get('docs/b'))->setPriority(251), |
|
99
|
|
|
NavItem::fromRoute(Route::get('docs/a'))->setPriority(252), |
|
100
|
|
|
]), |
|
101
|
|
|
DocumentationSidebar::create()->items |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function test_sidebar_item_priority_can_be_set_in_front_matter() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->makePage('foo', ['priority' => 25]); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals(25, DocumentationSidebar::create()->items->first()->priority); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function test_sidebar_item_priority_set_in_config_overrides_front_matter() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->makePage('foo', ['priority' => 25]); |
|
115
|
|
|
|
|
116
|
|
|
Config::set('docs.sidebar_order', ['foo']); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals(25, DocumentationSidebar::create()->items->first()->priority); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function test_sidebar_priorities_can_be_set_in_both_front_matter_and_config() |
|
122
|
|
|
{ |
|
123
|
|
|
Config::set('docs.sidebar_order', [ |
|
124
|
|
|
'first', |
|
125
|
|
|
'third', |
|
126
|
|
|
'second', |
|
127
|
|
|
]); |
|
128
|
|
|
Hyde::touch(('_docs/first.md')); |
|
129
|
|
|
Hyde::touch(('_docs/second.md')); |
|
130
|
|
|
file_put_contents(Hyde::path('_docs/third.md'), |
|
131
|
|
|
(new ConvertsArrayToFrontMatter)->execute(['priority' => 300]) |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertEquals( |
|
135
|
|
|
collect([ |
|
|
|
|
|
|
136
|
|
|
NavItem::fromRoute(Route::get('docs/first'))->setPriority(250), |
|
137
|
|
|
NavItem::fromRoute(Route::get('docs/second'))->setPriority(252), |
|
138
|
|
|
NavItem::fromRoute(Route::get('docs/third'))->setPriority(300), |
|
139
|
|
|
]), |
|
140
|
|
|
DocumentationSidebar::create()->items |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function test_category_can_be_set_in_front_matter() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->makePage('foo', ['category' => 'bar']); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals('bar', DocumentationSidebar::create()->items->first()->getGroup()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function test_has_groups_returns_false_when_there_are_no_groups() |
|
152
|
|
|
{ |
|
153
|
|
|
$this->assertFalse(DocumentationSidebar::create()->hasGroups()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function test_has_groups_returns_true_when_there_are_groups() |
|
157
|
|
|
{ |
|
158
|
|
|
$this->makePage('foo', ['category' => 'bar']); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertTrue(DocumentationSidebar::create()->hasGroups()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function test_get_groups_returns_empty_array_when_there_are_no_groups() |
|
164
|
|
|
{ |
|
165
|
|
|
$this->assertEquals([], DocumentationSidebar::create()->getGroups()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function test_get_groups_returns_array_of_groups_when_there_are_groups() |
|
169
|
|
|
{ |
|
170
|
|
|
$this->makePage('foo', ['category' => 'bar']); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertEquals(['bar'], DocumentationSidebar::create()->getGroups()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function test_get_groups_returns_array_with_no_duplicates() |
|
176
|
|
|
{ |
|
177
|
|
|
$this->makePage('foo', ['category' => 'bar']); |
|
178
|
|
|
$this->makePage('bar', ['category' => 'bar']); |
|
179
|
|
|
$this->makePage('baz', ['category' => 'baz']); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertEquals(['bar', 'baz'], DocumentationSidebar::create()->getGroups()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function test_groups_are_sorted_by_lowest_found_priority_in_each_group() |
|
185
|
|
|
{ |
|
186
|
|
|
$this->makePage('foo', ['category' => 'bar', 'priority' => 100]); |
|
187
|
|
|
$this->makePage('bar', ['category' => 'bar', 'priority' => 200]); |
|
188
|
|
|
$this->makePage('baz', ['category' => 'baz', 'priority' => 10]); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertEquals(['baz', 'bar'], DocumentationSidebar::create()->getGroups()); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function test_get_items_in_group_returns_empty_collection_when_there_are_no_items() |
|
194
|
|
|
{ |
|
195
|
|
|
$this->assertEquals(collect(), DocumentationSidebar::create()->getItemsInGroup('foo')); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function test_get_items_in_group_returns_collection_of_items_in_group() |
|
199
|
|
|
{ |
|
200
|
|
|
$this->makePage('foo', ['category' => 'bar']); |
|
201
|
|
|
$this->makePage('bar', ['category' => 'bar']); |
|
202
|
|
|
$this->makePage('baz', ['category' => 'baz']); |
|
203
|
|
|
|
|
204
|
|
|
$this->assertEquals( |
|
205
|
|
|
collect([ |
|
|
|
|
|
|
206
|
|
|
NavItem::fromRoute(Route::get('docs/bar'))->setPriority(500), |
|
207
|
|
|
NavItem::fromRoute(Route::get('docs/foo'))->setPriority(500), |
|
208
|
|
|
]), |
|
209
|
|
|
DocumentationSidebar::create()->getItemsInGroup('bar') |
|
210
|
|
|
); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertEquals( |
|
213
|
|
|
collect([ |
|
|
|
|
|
|
214
|
|
|
NavItem::fromRoute(Route::get('docs/baz'))->setPriority(500), |
|
215
|
|
|
]), |
|
216
|
|
|
DocumentationSidebar::create()->getItemsInGroup('baz') |
|
217
|
|
|
); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function test_get_items_in_group_normalizes_group_name_to_slug_format() |
|
221
|
|
|
{ |
|
222
|
|
|
$this->makePage('a', ['category' => 'foo bar']); |
|
223
|
|
|
$this->makePage('b', ['category' => 'Foo Bar']); |
|
224
|
|
|
$this->makePage('c', ['category' => 'foo-bar']); |
|
225
|
|
|
|
|
226
|
|
|
$this->assertEquals( |
|
227
|
|
|
collect([ |
|
|
|
|
|
|
228
|
|
|
NavItem::fromRoute(Route::get('docs/a'))->setPriority(500), |
|
229
|
|
|
NavItem::fromRoute(Route::get('docs/b'))->setPriority(500), |
|
230
|
|
|
NavItem::fromRoute(Route::get('docs/c'))->setPriority(500), |
|
231
|
|
|
]), |
|
232
|
|
|
DocumentationSidebar::create()->getItemsInGroup('Foo bar') |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function test_get_items_in_group_does_not_include_items_with_hidden_front_matter() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->makePage('a', ['hidden' => true, 'category' => 'foo']); |
|
239
|
|
|
$this->makePage('b', ['category' => 'foo']); |
|
240
|
|
|
|
|
241
|
|
|
$this->assertEquals( |
|
242
|
|
|
collect([NavItem::fromRoute(Route::get('docs/b'))->setPriority(500)]), |
|
|
|
|
|
|
243
|
|
|
DocumentationSidebar::create()->getItemsInGroup('foo') |
|
244
|
|
|
); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function test_get_items_in_group_does_not_include_docs_index() |
|
248
|
|
|
{ |
|
249
|
|
|
Hyde::touch('_docs/foo.md'); |
|
250
|
|
|
Hyde::touch('_docs/index.md'); |
|
251
|
|
|
|
|
252
|
|
|
$this->assertEquals( |
|
253
|
|
|
collect([NavItem::fromRoute(Route::get('docs/foo'))->setPriority(500)]), |
|
|
|
|
|
|
254
|
|
|
DocumentationSidebar::create()->items |
|
255
|
|
|
); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
protected function createTestFiles(int $count = 5): void |
|
259
|
|
|
{ |
|
260
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
261
|
|
|
Hyde::touch('_docs/test-'.$i.'.md'); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
protected function makePage(string $name, ?array $matter = null): void |
|
266
|
|
|
{ |
|
267
|
|
|
file_put_contents( |
|
268
|
|
|
Hyde::path('_docs/'.$name.'.md'), |
|
269
|
|
|
(new ConvertsArrayToFrontMatter)->execute($matter ?? []) |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|