-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRouteTest.php
More file actions
624 lines (507 loc) · 17.1 KB
/
Copy pathRouteTest.php
File metadata and controls
624 lines (507 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
namespace ChinLeung\MultilingualRoutes\Tests;
use ChinLeung\LaravelLocales\LaravelLocalesServiceProvider;
use ChinLeung\MultilingualRoutes\DetectRequestLocale;
use ChinLeung\MultilingualRoutes\MultilingualRoutePendingRegistration;
use ChinLeung\MultilingualRoutes\MultilingualRoutesServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use InvalidArgumentException;
use Orchestra\Testbench\TestCase;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class RouteTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['locales.supported' => [
'en', 'fr',
]]);
}
/** @test **/
public function a_multilingual_route_can_be_registered(): void
{
$this->registerTestRoute();
foreach (locales() as $locale) {
$this->assertEquals(
route($locale.'.test'),
localized_route('test', [], $locale)
);
}
}
/** @test **/
public function a_multilingual_redirect_route_can_be_registered(): void
{
$this->registerTestRedirectToRoute();
$this->registerTestRedirectRoute();
foreach (locales() as $locale) {
$response = $this->get(route($locale.'.from'));
$response->assertStatus(302);
$response->assertRedirect(localized_route('to'));
}
}
/** @test **/
public function a_route_can_have_different_names_based_on_locales(): void
{
$this
->registerTestRoute()
->names([
'en' => 'testing',
'fr' => 'teste',
]);
$this->assertEquals(
route('en.testing'),
localized_route('testing', [], 'en')
);
$this->assertEquals(
route('fr.teste'),
localized_route('teste', [], 'fr')
);
}
/** @test **/
public function the_group_name_can_be_renamed(): void
{
$this
->registerTestRoute()
->name('foo');
foreach (locales() as $locale) {
$this->assertEquals(
route($locale.'.foo'),
localized_route('foo', [], $locale)
);
}
}
/** @test **/
public function the_locale_name_has_priority_over_group_name(): void
{
$this
->registerTestRoute()
->name('foo')
->names([
'fr' => 'bar',
]);
$this->assertEquals(
route('en.foo'),
localized_route('foo', [], 'en')
);
$this->assertEquals(
route('fr.bar'),
localized_route('bar', [], 'fr')
);
$this->expectException(InvalidArgumentException::class);
localized_route('foo', [], 'fr');
}
/** @test **/
public function it_can_limit_route_to_specific_locales(): void
{
$this->registerTestRoute()
->only(['fr']);
$this->assertEquals(
route('fr.test'),
localized_route('test', [], 'fr')
);
$this->expectException(InvalidArgumentException::class);
localized_route('test', [], 'en');
}
/** @test **/
public function it_can_remove_specific_locales_from_route(): void
{
$this->registerTestRoute()
->except(['fr']);
$this->assertEquals(
route('en.test'),
localized_route('test', [], 'en')
);
$this->expectException(InvalidArgumentException::class);
localized_route('test', [], 'fr');
}
/** @test **/
public function the_default_locale_routes_can_be_prefixed(): void
{
config([
'laravel-multilingual-routes.prefix_default' => true,
]);
$this->registerTestRoute();
$route = localized_route('test', [], 'fr');
$this->assertEquals(route('fr.test'), $route);
$this->assertEquals(url('fr/teste'), $route);
}
/** @test **/
public function it_can_register_a_post_route(): void
{
$routes = $this
->registerTestRoute()
->method('post')
->register();
foreach (locales() as $locale) {
$this->assertContains(
'POST',
$routes->getByName("{$locale}.test")->methods
);
}
}
/** @test **/
public function the_app_locale_will_be_used_in_case_of_wrong_locale(): void
{
$this->registerTestRoute();
$this->assertEquals(
route(app()->getLocale().'.test'),
localized_route('test', [], 'cz')
);
}
/** @test **/
public function the_request_locale_can_be_changed_by_the_middleware(): void
{
$this->registerTestRoute();
(new DetectRequestLocale)->handle(
Request::create(localized_route('test', [], 'fr')),
function () {
$this->assertEquals(
'fr',
app()->getLocale()
);
}
);
}
/** @test **/
public function the_home_page_can_be_registered(): void
{
Route::multilingual('/', static function () {
//
})->name('home');
$this->assertEquals(url(''), localized_route('home'));
$this->assertEquals(url('fr'), localized_route('home', [], 'fr'));
}
/** @test **/
public function a_route_without_handle_can_be_registered(): void
{
$this->registerTestTranslations();
Route::multilingual('test');
$this->assertEquals(url('test'), localized_route('test'));
$this->assertEquals(url('fr/teste'), localized_route('test', [], 'fr'));
}
/** @test **/
public function a_route_with_identical_keys_can_be_registered(): void
{
Route::multilingual('test');
$this->assertEquals(url('test'), localized_route('test'));
$this->assertEquals(url('fr/test'), localized_route('test', [], 'fr'));
}
/** @test **/
public function a_route_with_prefix_stack_can_be_registered(): void
{
$this->registerTestTranslations();
Route::prefix('prefix')->group(static function () {
Route::multilingual('test');
});
$this->assertEquals(url('prefix/test'), localized_route('test'));
$this->assertEquals(
url('fr/prefixe/teste'),
localized_route('test', [], 'fr')
);
}
/** @test **/
public function a_root_route_with_prefix_stack_can_be_registered(): void
{
$this->registerTestTranslations();
Route::prefix('prefix')->group(static function () {
Route::multilingual('/')->name('test');
});
$this->assertEquals(url('prefix'), localized_route('test'));
$this->assertEquals(
url('fr/prefixe'),
localized_route('test', [], 'fr')
);
}
/** @test **/
public function a_view_route_can_be_registered(): void
{
Route::multilingual('/')->view('app')->name('home');
$this->assertEquals(config('app.url'), localized_route('home'));
$this->assertEquals(url('fr'), localized_route('home', [], 'fr'));
}
/** @test **/
public function a_view_route_can_be_registered_with_custom_data(): void
{
Route::multilingual('/')->name('home')->view('app', [
'name' => 'Taylor',
]);
foreach (locales() as $locale) {
$route = Route::getRoutes()->getByName("{$locale}.home");
$this->assertEquals(
'Taylor',
Arr::get($route->defaults, 'data.name')
);
}
}
/** @test **/
public function a_view_route_can_be_registered_with_custom_data_via_method(): void
{
Route::multilingual('/')->name('home')->view('app')->data([
'name' => 'Taylor',
]);
foreach (locales() as $locale) {
$route = Route::getRoutes()->getByName("{$locale}.home");
$this->assertEquals(
'Taylor',
Arr::get($route->defaults, 'data.name')
);
}
}
/** @test **/
public function a_route_param_can_have_constraints(): void
{
$this->registerTranslations([
'en' => [
'routes.search' => 'search/{filter?}',
],
'fr' => [
'routes.search' => 'recherche/{filter?}',
],
]);
Route::multilingual('search')->where('filter', '.*')->name('search.results');
foreach (locales() as $locale) {
$route = Route::getRoutes()->getByName("{$locale}.search.results");
$this->assertEquals('.*', Arr::get($route->wheres, 'filter'));
}
}
/** @test **/
public function a_route_param_can_have_constraints_by_locale(): void
{
$this->registerTranslations([
'en' => [
'routes.search' => 'search/{filter}/{filter2}',
],
'fr' => [
'routes.search' => 'recherche/{filter}/{filter2}',
],
]);
Route::multilingual('search')->where('filter', '.*')
->where('filter2', 'fr', 'fr')
->where('filter2', 'en', 'en')
->name('search.results');
foreach (locales() as $locale) {
$route = Route::getRoutes()->getByName("{$locale}.search.results");
$this->assertEquals('.*', Arr::get($route->wheres, 'filter'));
$this->assertEquals($locale, Arr::get($route->wheres, 'filter2'));
}
}
/** @test **/
public function a_route_without_translation_will_be_registered_with_its_key(): void
{
Route::multilingual('test');
$this->assertEquals(url('test'), localized_route('test'));
$this->assertEquals(url('fr/test'), localized_route('test', [], 'fr'));
}
/** @test **/
public function a_starting_slash_will_be_trimmed_from_translation(): void
{
$this->registerTestTranslations();
Route::multilingual('/test', static function () {
//
});
$this->assertEquals(url('test'), localized_route('test'));
$this->assertEquals(url('fr/teste'), localized_route('test', [], 'fr'));
}
/** @test **/
public function the_current_route_can_be_retrieved_in_a_different_locale(): void
{
$this->registerTestRoute();
Route::dispatch(Request::create(localized_route('test')));
$this->assertEquals(localized_route('test', [], 'fr'), current_route('fr'));
}
/** @test **/
public function the_current_route_can_be_retrieved_in_a_different_locale_with_query_strings(): void
{
$this->registerTestRoute();
app()->bind('request', static function () {
return Request::create(localized_route('test'), 'GET', [
'foo' => 'bar',
]);
});
Route::dispatch(request());
$this->assertEquals(
localized_route('test', ['foo' => 'bar'], 'fr'),
current_route('fr')
);
}
/** @test **/
public function the_current_route_will_fallback_to_current_route_by_default(): void
{
Route::view('test', 'app');
app()->bind('request', static function () {
return Request::create(url('test'), 'GET');
});
Route::dispatch(request());
$this->assertEquals(url('test'), current_route('fr'));
}
/** @test **/
public function the_current_route_can_have_a_custom_fallback(): void
{
Route::view('test', 'app');
Route::view('fallback', 'app');
app()->bind('request', static function () {
return Request::create(url('test'), 'GET');
});
Route::dispatch(request());
$this->assertEquals(
url('fallback'),
current_route('fr', url('fallback'))
);
}
/** @test **/
public function the_current_route_for_a_missing_page_will_return_the_custom_fallback(): void
{
Route::view('fallback', 'app');
app()->bind('request', static function () {
return Request::create(url('missing-route'), 'GET');
});
try {
Route::dispatch(request());
} catch (NotFoundHttpException $e) {
$this->assertEquals(
url('fallback'),
current_route('fr', url('fallback'))
);
}
}
/** @test **/
public function a_route_prefix_can_be_registered_after_the_locale(): void
{
Route::name('prefix.')->group(static function () {
Route::multilingual('test');
});
$this->assertNotNull(localized_route('prefix.test'));
$this->assertNotNull(localized_route('prefix.test', [], 'fr'));
}
/** @test **/
public function a_route_prefix_can_be_registered_before_the_locale(): void
{
config([
'laravel-multilingual-routes.name_prefix_before_locale' => true,
]);
Route::name('prefix.')->group(static function () {
Route::multilingual('test');
});
$this->assertNotNull(route('prefix.en.test'));
$this->assertNotNull(route('prefix.fr.test'));
}
/** @test **/
public function a_route_with_defaults_parameters_can_be_registered(): void
{
$params = ['param_1' => 'value_1', 'param_2' => 'value_2'];
Route::multilingual('test')->defaults($params)->name('test');
foreach (config('locales.supported') as $locale) {
$route = Route::getRoutes()->getByName($locale.'.test');
foreach ($params as $key => $value) {
$this->assertArrayHasKey($key, $route->defaults);
$this->assertEquals($value, $route->defaults[$key]);
}
}
}
/** @test **/
public function the_default_home_page_can_be_registered_with_prefix(): void
{
config([
'laravel-multilingual-routes.prefix_default' => true,
'laravel-multilingual-routes.prefix_default_home' => true,
]);
Route::multilingual('/', static function () {
//
})->name('home');
$this->assertEquals(url('en'), localized_route('home'));
$this->assertEquals(url('fr'), localized_route('home', [], 'fr'));
}
/** @test **/
public function the_default_home_page_can_be_registered_without_prefix(): void
{
config([
'laravel-multilingual-routes.prefix_default' => true,
'laravel-multilingual-routes.prefix_default_home' => false,
]);
Route::multilingual('/', static function () {
//
})->name('home');
$this->assertEquals(url(''), localized_route('home'));
$this->assertEquals(url('fr'), localized_route('home', [], 'fr'));
}
/** @test **/
public function a_route_can_be_registered_with_a_middleware(): void
{
$routes = Route::multilingual('/')
->middleware('web')
->name('home')
->register();
foreach (locales() as $locale) {
$this->assertContains(
'web',
data_get($routes->getByName("{$locale}.home"), 'action.middleware')
);
}
}
/** @test **/
public function a_named_route_can_be_checked_if_it_exists(): void
{
$this
->registerTestRoute()
->name('foo');
$this->assertTrue(Route::hasLocalized('foo'));
$this->assertFalse(Route::hasLocalized('bar'));
}
protected function registerTestRoute(): MultilingualRoutePendingRegistration
{
$this->registerTestTranslations();
return Route::multilingual(
'test',
static function () {
//
}
);
}
protected function registerTestRedirectToRoute(): MultilingualRoutePendingRegistration
{
$this->registerTestTranslations();
return Route::multilingual(
'to',
static function () {
//
}
);
}
protected function registerTestRedirectRoute(): MultilingualRoutePendingRegistration
{
$this->registerTestTranslations();
return Route::multilingual('from')->redirect('to');
}
protected function registerTestTranslations(): void
{
$this->registerTranslations([
'en' => [
'routes.prefix/test' => 'prefix/test',
'routes.prefix' => 'prefix',
'routes.test' => 'test',
],
'fr' => [
'routes.prefix/test' => 'prefixe/teste',
'routes.prefix' => 'prefixe',
'routes.test' => 'teste',
],
]);
}
protected function registerTranslations(array $translations): self
{
$translator = app('translator');
foreach ($translations as $locale => $translation) {
$translator->addLines($translation, $locale);
}
return $this;
}
protected function getPackageProviders($app)
{
return [
LaravelLocalesServiceProvider::class,
MultilingualRoutesServiceProvider::class,
];
}
}