|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeZero\UriTranslator\Tests\Feature; |
| 4 | + |
| 5 | +use CodeZero\UriTranslator\Tests\TestCase; |
| 6 | +use Illuminate\Support\Facades\Lang; |
| 7 | + |
| 8 | +class UriTranslatorTest extends TestCase |
| 9 | +{ |
| 10 | + /** @test */ |
| 11 | + public function it_translates_every_segment_in_a_uri_to_the_current_locale() |
| 12 | + { |
| 13 | + $this->setTranslations([ |
| 14 | + 'nl' => [ |
| 15 | + 'my' => 'mijn', |
| 16 | + 'new' => 'nieuwe', |
| 17 | + 'page' => 'pagina', |
| 18 | + ] |
| 19 | + ]); |
| 20 | + |
| 21 | + $this->setAppLocale('en'); |
| 22 | + $this->assertEquals('my/new/page', Lang::uri('my/new/page')); |
| 23 | + |
| 24 | + $this->setAppLocale('nl'); |
| 25 | + $this->assertEquals('mijn/nieuwe/pagina', Lang::uri('my/new/page')); |
| 26 | + $this->assertEquals('mijn/nieuwe/pagina', trans()->uri('my/new/page')); |
| 27 | + } |
| 28 | + |
| 29 | + /** @test */ |
| 30 | + public function it_translates_every_segment_in_a_uri_to_the_given_locale() |
| 31 | + { |
| 32 | + $this->setTranslations([ |
| 33 | + 'nl' => [ |
| 34 | + 'my' => 'mijn', |
| 35 | + 'new' => 'nieuwe', |
| 36 | + 'page' => 'pagina', |
| 37 | + ] |
| 38 | + ]); |
| 39 | + |
| 40 | + $this->assertEquals('mijn/nieuwe/pagina', Lang::uri('my/new/page', 'nl')); |
| 41 | + } |
| 42 | + |
| 43 | + /** @test */ |
| 44 | + public function it_uses_the_original_values_if_a_translation_does_not_exist() |
| 45 | + { |
| 46 | + $this->setTranslations([ |
| 47 | + 'nl' => [ |
| 48 | + 'my' => 'mijn', |
| 49 | + 'new' => 'nieuwe', |
| 50 | + ] |
| 51 | + ]); |
| 52 | + |
| 53 | + $this->assertEquals('mijn/nieuwe/page', Lang::uri('my/new/page', 'nl')); |
| 54 | + $this->assertEquals('my/new/page', Lang::uri('my/new/page', 'fr')); |
| 55 | + } |
| 56 | + |
| 57 | + /** @test */ |
| 58 | + public function it_ignores_trailing_slashes() |
| 59 | + { |
| 60 | + $this->setTranslations([ |
| 61 | + 'nl' => [ |
| 62 | + 'my' => 'mijn', |
| 63 | + 'new' => 'nieuwe', |
| 64 | + 'page' => 'pagina', |
| 65 | + ] |
| 66 | + ]); |
| 67 | + |
| 68 | + $this->assertEquals('mijn/nieuwe/pagina', Lang::uri('/my/new/page/', 'nl')); |
| 69 | + } |
| 70 | + |
| 71 | + /** @test */ |
| 72 | + public function it_skips_placeholders_in_a_uri() |
| 73 | + { |
| 74 | + $this->setTranslations([ |
| 75 | + 'nl' => [ |
| 76 | + 'articles' => 'artikels', |
| 77 | + ] |
| 78 | + ]); |
| 79 | + |
| 80 | + $this->assertEquals('artikels/{articles}', Lang::uri('articles/{articles}', 'nl')); |
| 81 | + } |
| 82 | + |
| 83 | + /** @test */ |
| 84 | + public function you_can_translate_a_full_uri() |
| 85 | + { |
| 86 | + $this->setTranslations([ |
| 87 | + 'nl' => [ |
| 88 | + 'glass' => 'glas', |
| 89 | + 'products' => 'producten', |
| 90 | + 'products/glass' => 'producten/glazen' |
| 91 | + ] |
| 92 | + ]); |
| 93 | + |
| 94 | + $this->assertEquals('producten/glazen', Lang::uri('products/glass', 'nl')); |
| 95 | + } |
| 96 | + |
| 97 | + /** @test */ |
| 98 | + public function you_can_translate_a_full_uri_with_placeholder() |
| 99 | + { |
| 100 | + $this->setTranslations([ |
| 101 | + 'nl' => [ |
| 102 | + 'glass' => 'glas', |
| 103 | + 'products' => 'producten', |
| 104 | + 'products/glass/{type}' => 'producten/glazen/{type}' |
| 105 | + ] |
| 106 | + ]); |
| 107 | + |
| 108 | + $this->assertEquals('producten/glazen/{type}', Lang::uri('products/glass/{type}', 'nl')); |
| 109 | + } |
| 110 | + |
| 111 | + /** @test */ |
| 112 | + public function you_can_specify_a_namespace() |
| 113 | + { |
| 114 | + $this->setTranslations([ |
| 115 | + 'nl' => [ |
| 116 | + 'articles' => 'artikels', |
| 117 | + ] |
| 118 | + ], 'blog'); |
| 119 | + |
| 120 | + $this->assertEquals('artikels/{article}', Lang::uri('articles/{article}', 'nl', 'blog')); |
| 121 | + } |
| 122 | + |
| 123 | + /** @test */ |
| 124 | + public function the_uri_macro_is_available_via_the_trans_helper() |
| 125 | + { |
| 126 | + $this->setTranslations([ |
| 127 | + 'nl' => [ |
| 128 | + 'my' => 'mijn', |
| 129 | + 'new' => 'nieuwe', |
| 130 | + 'page' => 'pagina', |
| 131 | + ] |
| 132 | + ]); |
| 133 | + |
| 134 | + $this->setAppLocale('en'); |
| 135 | + $this->assertEquals('my/new/page', trans()->uri('my/new/page')); |
| 136 | + |
| 137 | + $this->setAppLocale('nl'); |
| 138 | + $this->assertEquals('mijn/nieuwe/pagina', trans()->uri('my/new/page')); |
| 139 | + } |
| 140 | +} |
0 commit comments