1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Feature; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Contracts\PageContract; |
6
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
7
|
|
|
use Hyde\Framework\Hyde; |
8
|
|
|
use Hyde\Framework\Models\Pages\BladePage; |
9
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
10
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPage; |
11
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
12
|
|
|
use Hyde\Framework\Models\Route; |
13
|
|
|
use Hyde\Framework\Services\RoutingService; |
14
|
|
|
use Hyde\Testing\TestCase; |
15
|
|
|
use Illuminate\Support\Collection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @covers \Hyde\Framework\Services\RoutingService |
19
|
|
|
*/ |
20
|
|
|
class RoutingServiceTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::getInstance |
24
|
|
|
*/ |
25
|
|
|
public function test_get_instance_returns_the_router_instance() |
26
|
|
|
{ |
27
|
|
|
$this->assertInstanceOf(RoutingService::class, RoutingService::getInstance()); |
28
|
|
|
$this->assertEquals(RoutingService::getInstance(), RoutingService::getInstance()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Test route autodiscovery. |
33
|
|
|
* |
34
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::__construct |
35
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::getRoutes |
36
|
|
|
*/ |
37
|
|
|
public function test_get_routes_returns_discovered_routes() |
38
|
|
|
{ |
39
|
|
|
$routes = (new RoutingService())->getRoutes(); |
40
|
|
|
|
41
|
|
|
$this->assertContainsOnlyInstancesOf(RouteContract::class, $routes); |
42
|
|
|
|
43
|
|
|
$this->assertEquals(collect([ |
44
|
|
|
'404' => new Route(BladePage::parse('404')), |
45
|
|
|
'index' => new Route(BladePage::parse('index')), |
46
|
|
|
]), $routes); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::getRoutesForModel |
51
|
|
|
*/ |
52
|
|
|
public function test_get_routes_for_model_returns_only_routes_for_the_given_model() |
53
|
|
|
{ |
54
|
|
|
Hyde::touch(('_pages/foo.md')); |
55
|
|
|
|
56
|
|
|
$routes = (new RoutingService())->getRoutesForModel(MarkdownPage::class); |
57
|
|
|
|
58
|
|
|
$this->assertEquals(collect([ |
59
|
|
|
'foo' => new Route(MarkdownPage::parse('foo')), |
60
|
|
|
]), $routes); |
61
|
|
|
|
62
|
|
|
unlink(Hyde::path('_pages/foo.md')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test route autodiscovery. |
67
|
|
|
* |
68
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::discover |
69
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::discoverRoutes |
70
|
|
|
* @covers \Hyde\Framework\Services\RoutingService::discoverPageRoutes |
71
|
|
|
*/ |
72
|
|
|
public function test_discover_routes_finds_and_adds_all_pages_to_route_collection() |
73
|
|
|
{ |
74
|
|
|
backup(Hyde::path('_pages/404.blade.php')); |
75
|
|
|
backup(Hyde::path('_pages/index.blade.php')); |
76
|
|
|
unlink(Hyde::path('_pages/404.blade.php')); |
77
|
|
|
unlink(Hyde::path('_pages/index.blade.php')); |
78
|
|
|
|
79
|
|
|
$this->testRouteModelDiscoveryForPageModel(BladePage::class); |
80
|
|
|
$this->testRouteModelDiscoveryForPageModel(MarkdownPage::class); |
81
|
|
|
$this->testRouteModelDiscoveryForPageModel(MarkdownPost::class); |
82
|
|
|
$this->testRouteModelDiscoveryForPageModel(DocumentationPage::class); |
83
|
|
|
|
84
|
|
|
restore(Hyde::path('_pages/404.blade.php')); |
85
|
|
|
restore(Hyde::path('_pages/index.blade.php')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function test_routes_are_not_discovered_for_disabled_features() |
89
|
|
|
{ |
90
|
|
|
config(['hyde.features' => []]); |
91
|
|
|
|
92
|
|
|
touch('_pages/blade.blade.php'); |
93
|
|
|
touch('_pages/markdown.md'); |
94
|
|
|
touch('_posts/post.md'); |
95
|
|
|
touch('_docs/doc.md'); |
96
|
|
|
|
97
|
|
|
$this->assertEmpty((new RoutingService())->getRoutes()); |
98
|
|
|
|
99
|
|
|
unlink('_pages/blade.blade.php'); |
100
|
|
|
unlink('_pages/markdown.md'); |
101
|
|
|
unlink('_posts/post.md'); |
102
|
|
|
unlink('_docs/doc.md'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function test_add_route_adds_a_route_to_the_routes_collection() |
106
|
|
|
{ |
107
|
|
|
$routes = (new RoutingService())->getRoutes(); |
108
|
|
|
|
109
|
|
|
$this->assertEquals(collect([ |
110
|
|
|
'404' => new Route(BladePage::parse('404')), |
111
|
|
|
'index' => new Route(BladePage::parse('index')), |
112
|
|
|
]), $routes); |
113
|
|
|
|
114
|
|
|
Hyde::touch('_pages/foo.md'); |
115
|
|
|
(new RoutingService())->addRoute(new Route(MarkdownPage::parse('foo'))); |
116
|
|
|
$routes = (new RoutingService())->getRoutes(); |
117
|
|
|
|
118
|
|
|
$this->assertEquals(collect([ |
119
|
|
|
'404' => new Route(BladePage::parse('404')), |
120
|
|
|
'index' => new Route(BladePage::parse('index')), |
121
|
|
|
'foo' => new Route(MarkdownPage::parse('foo')), |
122
|
|
|
]), $routes); |
123
|
|
|
|
124
|
|
|
Hyde::unlink('_pages/foo.md'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function test_routes_with_custom_source_directories_are_discovered_properly() |
128
|
|
|
{ |
129
|
|
|
$this->markTestSkipped('TODO'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function test_routes_with_custom_output_paths_are_registered_properly() |
133
|
|
|
{ |
134
|
|
|
$this->markTestSkipped('TODO'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function testRouteModelDiscoveryForPageModel(string $class) |
138
|
|
|
{ |
139
|
|
|
/** @var PageContract $class */ |
140
|
|
|
Hyde::touch(($class::qualifyBasename('foo'))); |
141
|
|
|
|
142
|
|
|
$expectedKey = 'foo'; |
143
|
|
|
if ($class === MarkdownPost::class) { |
144
|
|
|
$expectedKey = 'posts/foo'; |
145
|
|
|
} |
146
|
|
|
if ($class === DocumentationPage::class) { |
147
|
|
|
$expectedKey = 'docs/foo'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$expected = collect([ |
151
|
|
|
$expectedKey => new Route($class::parse('foo')), |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$this->assertEquals($expected, (new RoutingService())->getRoutes()); |
155
|
|
|
unlink(Hyde::path($class::qualifyBasename('foo'))); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function assertHasRoute(RouteContract $route, Collection $routes) |
159
|
|
|
{ |
160
|
|
|
$this->assertTrue($routes->has($route->getRouteKey()), "Failed asserting route collection has key {$route->getRouteKey()}"); |
161
|
|
|
$this->assertEquals($route, $routes->get($route->getRouteKey()), "Failed asserting route collection has route {$route->getRouteKey()}"); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|