|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeZero\LocalizedRoutes\Tests\Unit\Macros; |
| 4 | + |
| 5 | +use CodeZero\LocalizedRoutes\Tests\Stubs\Model; |
| 6 | +use CodeZero\LocalizedRoutes\Tests\TestCase; |
| 7 | +use Illuminate\Support\Facades\App; |
| 8 | +use Illuminate\Support\Facades\Route; |
| 9 | + |
| 10 | +class CurrentUrlMacroTest extends TestCase |
| 11 | +{ |
| 12 | + /** @test */ |
| 13 | + public function it_automatically_generates_urls_with_localized_route_keys_for_the_current_route_using_route_model_binding() |
| 14 | + { |
| 15 | + $this->setSupportedLocales(['en', 'nl']); |
| 16 | + |
| 17 | + $model = (new Model([ |
| 18 | + 'slug' => [ |
| 19 | + 'en' => 'en-slug', |
| 20 | + 'nl' => 'nl-slug', |
| 21 | + ], |
| 22 | + ]))->setKeyName('slug'); |
| 23 | + |
| 24 | + App::instance(Model::class, $model); |
| 25 | + |
| 26 | + Route::localized(function () { |
| 27 | + Route::get('route/{model}', function (Model $model) { |
| 28 | + return [ |
| 29 | + 'current' => Route::localizedUrl(), |
| 30 | + 'en' => Route::localizedUrl('en'), |
| 31 | + 'nl' => Route::localizedUrl('nl'), |
| 32 | + ]; |
| 33 | + })->middleware(['web']); |
| 34 | + }); |
| 35 | + |
| 36 | + $response = $this->call('GET', '/en/route/en-slug'); |
| 37 | + $response->assertOk(); |
| 38 | + $this->assertEquals([ |
| 39 | + 'current' => url('/en/route/en-slug'), |
| 40 | + 'en' => url('/en/route/en-slug'), |
| 41 | + 'nl' => url('/nl/route/nl-slug'), |
| 42 | + ], $response->original); |
| 43 | + } |
| 44 | + |
| 45 | + /** @test */ |
| 46 | + public function it_cannot_guess_a_localized_route_key_without_route_model_binding() |
| 47 | + { |
| 48 | + $this->setSupportedLocales(['en', 'nl']); |
| 49 | + |
| 50 | + $model = (new Model([ |
| 51 | + 'slug' => [ |
| 52 | + 'en' => 'en-slug', |
| 53 | + 'nl' => 'nl-slug', |
| 54 | + ], |
| 55 | + ]))->setKeyName('slug'); |
| 56 | + |
| 57 | + App::instance(Model::class, $model); |
| 58 | + |
| 59 | + Route::localized(function () { |
| 60 | + Route::get('route/{slug}', function ($slug) { |
| 61 | + return [ |
| 62 | + 'current' => Route::localizedUrl(), |
| 63 | + 'en' => Route::localizedUrl('en'), |
| 64 | + 'nl' => Route::localizedUrl('nl'), |
| 65 | + ]; |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + $response = $this->call('GET', '/en/route/en-slug'); |
| 70 | + $response->assertOk(); |
| 71 | + $this->assertEquals([ |
| 72 | + 'current' => url('/en/route/en-slug'), |
| 73 | + 'en' => url('/en/route/en-slug'), |
| 74 | + 'nl' => url('/nl/route/en-slug'), // Wrong slug! |
| 75 | + ], $response->original); |
| 76 | + } |
| 77 | + |
| 78 | + /** @test */ |
| 79 | + public function you_can_pass_it_a_model_with_a_localized_route_key() |
| 80 | + { |
| 81 | + $this->setSupportedLocales(['en', 'nl']); |
| 82 | + |
| 83 | + $model = (new Model([ |
| 84 | + 'slug' => [ |
| 85 | + 'en' => 'en-slug', |
| 86 | + 'nl' => 'nl-slug', |
| 87 | + ], |
| 88 | + ]))->setKeyName('slug'); |
| 89 | + |
| 90 | + App::instance(Model::class, $model); |
| 91 | + |
| 92 | + Route::localized(function () use ($model) { |
| 93 | + Route::get('route/{slug}', function ($slug) use ($model) { |
| 94 | + return [ |
| 95 | + 'current' => Route::localizedUrl(), |
| 96 | + 'en' => Route::localizedUrl('en', [$model]), |
| 97 | + 'nl' => Route::localizedUrl('nl', [$model]), |
| 98 | + ]; |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + $response = $this->call('GET', '/en/route/en-slug'); |
| 103 | + $response->assertOk(); |
| 104 | + $this->assertEquals([ |
| 105 | + 'current' => url('/en/route/en-slug'), |
| 106 | + 'en' => url('/en/route/en-slug'), |
| 107 | + 'nl' => url('/nl/route/nl-slug'), |
| 108 | + ], $response->original); |
| 109 | + } |
| 110 | +} |
0 commit comments