1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use Orchestra\Testbench\TestCase; |
6
|
|
|
|
7
|
|
|
class ResolvesViewsTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Setup the test environment. |
11
|
|
|
*/ |
12
|
9 |
|
public function setUp() |
13
|
|
|
{ |
14
|
9 |
|
parent::setUp(); |
15
|
|
|
|
16
|
|
|
// Make sure we load the view directory. |
17
|
9 |
|
$this->app['view']->addLocation(__DIR__ . '/../src/views'); |
18
|
|
|
|
19
|
|
|
// Set up our test routes. |
20
|
9 |
|
$this->app['router']->get('auto-route', 'Tests\Resources\RouteController@autoRoute'); |
21
|
9 |
|
$this->app['router']->get('manual-route', 'Tests\Resources\RouteController@manualRoute'); |
22
|
9 |
|
$this->app['router']->get('manual-layout-route', 'Tests\Resources\RouteController@manualLayoutRoute'); |
23
|
9 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get package providers. At a minimum this is the package being tested, but also |
27
|
|
|
* would include packages upon which our package depends, e.g. Cartalyst/Sentry |
28
|
|
|
* In a normal app environment these would be added to the 'providers' array in |
29
|
|
|
* the config/app.php file. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Foundation\Application $app |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
9 |
|
protected function getPackageProviders($app) |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
9 |
|
\JumpGate\ViewResolution\Providers\ViewServiceProvider::class, |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
* |
45
|
|
|
* @expectedException InvalidArgumentException |
46
|
|
|
* @expectedExceptionMessage The layoutOptions must be an array. |
47
|
|
|
*/ |
48
|
1 |
|
public function it_errors_when_no_config_is_set() |
49
|
|
|
{ |
50
|
1 |
|
(new \Tests\Resources\RouteController)->autoRoute(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
* |
56
|
|
|
* @expectedException InvalidArgumentException |
57
|
|
|
* @expectedExceptionMessage The layoutOptions must have a default layout view. |
58
|
|
|
*/ |
59
|
1 |
|
public function it_fails_when_no_default_layout_is_present() |
60
|
|
|
{ |
61
|
1 |
|
$controller = new \Tests\Resources\RouteController; |
62
|
1 |
|
$controller->layoutOptions = []; |
63
|
|
|
|
64
|
1 |
|
$controller->autoRoute(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
* |
70
|
|
|
* @expectedException InvalidArgumentException |
71
|
|
|
* @expectedExceptionMessage The layoutOptions must have a ajax layout view. |
72
|
|
|
*/ |
73
|
1 |
|
public function it_fails_when_no_ajax_layout_is_present() |
74
|
|
|
{ |
75
|
1 |
|
$controller = new \Tests\Resources\RouteController; |
76
|
1 |
|
$controller->layoutOptions = [ |
77
|
|
|
'default' => 'test', |
78
|
|
|
]; |
79
|
|
|
|
80
|
1 |
|
$controller->autoRoute(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
*/ |
86
|
1 |
View Code Duplication |
public function it_can_use_the_config_for_defaults() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
// Use the default layout. |
89
|
1 |
|
$this->loadLayout(); |
90
|
|
|
|
91
|
|
|
// Call the route and get the auto view debug information. |
92
|
1 |
|
$this->call('GET', '/auto-route'); |
93
|
|
|
|
94
|
1 |
|
$debug = viewResolver()->debug(); |
95
|
|
|
|
96
|
|
|
// Assert. |
97
|
1 |
|
$this->assertEquals('layout', $debug->layout); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
*/ |
103
|
1 |
View Code Duplication |
public function it_can_use_the_config_for_default_layout_options() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
// Add the view. |
106
|
1 |
|
$this->createView('configDefault'); |
107
|
|
|
|
108
|
|
|
// Use the default layout. |
109
|
1 |
|
$this->loadLayout(); |
110
|
|
|
|
111
|
|
|
// Set up the config entry to specify a view to use. |
112
|
1 |
|
$this->app['config']->set('jumpgate.view-resolution.layout_options.default', 'configDefault'); |
113
|
|
|
|
114
|
|
|
// Call the route and get the auto view debug information. |
115
|
1 |
|
$this->call('GET', '/auto-route'); |
116
|
|
|
|
117
|
1 |
|
$debug = viewResolver()->debug(); |
118
|
|
|
|
119
|
|
|
// Assert. |
120
|
1 |
|
$this->assertEquals('configDefault', $debug->layout); |
121
|
|
|
|
122
|
|
|
// Clean up. |
123
|
1 |
|
$this->deleteView('configDefault'); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @test |
128
|
|
|
*/ |
129
|
1 |
View Code Duplication |
public function it_can_automatically_resolve_a_view() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
// Add the view. |
132
|
1 |
|
$this->makeViewDir('route'); |
133
|
1 |
|
$this->createView('route/autoroute'); |
134
|
|
|
|
135
|
|
|
// Use the default layout. |
136
|
1 |
|
$this->loadLayout(); |
137
|
|
|
|
138
|
|
|
// Call the route and get the auto view debug information. |
139
|
1 |
|
$this->call('GET', '/auto-route'); |
140
|
|
|
|
141
|
1 |
|
$debug = viewResolver()->debug(); |
142
|
|
|
|
143
|
|
|
// Assert. |
144
|
1 |
|
$this->assertEquals('route.autoroute', $debug->view); |
145
|
1 |
|
$this->assertEquals('auto', $debug->type); |
146
|
|
|
|
147
|
|
|
// Clean up. |
148
|
1 |
|
$this->deleteViewDir('route'); |
149
|
1 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @test |
153
|
|
|
*/ |
154
|
1 |
View Code Duplication |
public function it_can_manually_specify_a_view() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
// Use the default layout. |
157
|
1 |
|
$this->loadLayout(); |
158
|
|
|
|
159
|
|
|
// Call the route and get the auto view debug information. |
160
|
1 |
|
$this->call('GET', '/manual-route'); |
161
|
|
|
|
162
|
1 |
|
$debug = viewResolver()->debug(); |
163
|
|
|
|
164
|
|
|
// Assert. |
165
|
1 |
|
$this->assertEquals('test', $debug->view); |
166
|
1 |
|
$this->assertEquals('manual', $debug->type); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @test |
171
|
|
|
*/ |
172
|
1 |
View Code Duplication |
public function it_can_manually_specify_a_layout() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
// Add the view. |
175
|
1 |
|
$this->createView('testLayout'); |
176
|
|
|
|
177
|
|
|
// Call the route and get the auto view debug information. |
178
|
1 |
|
$this->call('GET', '/manual-layout-route'); |
179
|
|
|
|
180
|
1 |
|
$debug = viewResolver()->debug(); |
181
|
|
|
|
182
|
|
|
// Assert. |
183
|
1 |
|
$this->assertEquals('test', $debug->view); |
184
|
1 |
|
$this->assertEquals('manual', $debug->type); |
185
|
1 |
|
$this->assertEquals('testLayout', $debug->layout); |
186
|
|
|
|
187
|
|
|
// Clean up. |
188
|
1 |
|
$this->deleteView('testLayout'); |
189
|
1 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @test |
193
|
|
|
*/ |
194
|
1 |
View Code Duplication |
public function it_can_get_a_view_from_the_config() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
// Add the view. |
197
|
1 |
|
$this->createView('configView'); |
198
|
|
|
|
199
|
|
|
// Use the default layout. |
200
|
1 |
|
$this->loadLayout(); |
201
|
|
|
|
202
|
|
|
// Set up the config entry to specify a view to use. |
203
|
1 |
|
$this->app['config']->set('jumpgate.view-resolution.view_locations.Tests\Resources\RouteController.autoroute', 'configView'); |
204
|
|
|
|
205
|
|
|
// Call the route and get the auto view debug information. |
206
|
1 |
|
$this->call('GET', '/auto-route'); |
207
|
|
|
|
208
|
1 |
|
$debug = viewResolver()->debug(); |
209
|
|
|
|
210
|
|
|
// Asset. |
211
|
1 |
|
$this->assertEquals('configView', $debug->view); |
212
|
1 |
|
$this->assertEquals('config', $debug->type); |
213
|
|
|
|
214
|
|
|
// Clean up. |
215
|
1 |
|
$this->deleteView('configView'); |
216
|
1 |
|
} |
217
|
|
|
|
218
|
5 |
|
private function loadLayout($boolean = true) |
219
|
|
|
{ |
220
|
5 |
|
$this->app['config']->set('jumpgate.view-resolution.load_layout', $boolean); |
221
|
5 |
|
} |
222
|
|
|
|
223
|
4 |
|
private function createView($view) |
224
|
|
|
{ |
225
|
4 |
|
$this->app['files']->put(__DIR__ . '/../src/views/' . $view . '.blade.php', 'something'); |
226
|
4 |
|
} |
227
|
|
|
|
228
|
3 |
|
private function deleteView($view) |
229
|
|
|
{ |
230
|
3 |
|
$this->app['files']->delete(__DIR__ . '/../src/views/' . $view . '.blade.php'); |
231
|
3 |
|
} |
232
|
|
|
|
233
|
1 |
|
private function makeViewDir($directory) |
234
|
|
|
{ |
235
|
1 |
|
$this->app['files']->makeDirectory(__DIR__ . '/../src/views/' . $directory); |
236
|
1 |
|
} |
237
|
|
|
|
238
|
1 |
|
private function deleteViewDir($directory) |
239
|
|
|
{ |
240
|
1 |
|
$this->app['files']->deleteDirectory(__DIR__ . '/../src/views/' . $directory); |
241
|
1 |
|
} |
242
|
|
|
} |
243
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.