|
| 1 | +<?php |
| 2 | + |
| 3 | +use Dimsav\Translatable\Locales; |
| 4 | + |
| 5 | +class LocalesTest extends TestsBase |
| 6 | +{ |
| 7 | + public function test_singleton() |
| 8 | + { |
| 9 | + $this->assertSame(spl_object_id($this->app->make('translatable.locales')), spl_object_id($this->app->make('translatable.locales'))); |
| 10 | + $this->assertSame(spl_object_id($this->app->make(Locales::class)), spl_object_id($this->app->make(Locales::class))); |
| 11 | + $this->assertSame(spl_object_id($this->app->make('translatable.locales')), spl_object_id($this->app->make(Locales::class))); |
| 12 | + } |
| 13 | + |
| 14 | + public function test_load() |
| 15 | + { |
| 16 | + $this->app['config']->set('translatable.locales', [ |
| 17 | + 'de', |
| 18 | + ]); |
| 19 | + $this->assertEquals(['de'], $this->app->make('translatable.locales')->all()); |
| 20 | + |
| 21 | + $this->app['config']->set('translatable.locales', [ |
| 22 | + 'de', |
| 23 | + 'en', |
| 24 | + ]); |
| 25 | + $this->assertEquals(['de'], $this->app->make('translatable.locales')->all()); |
| 26 | + $this->app->make('translatable.locales')->load(); |
| 27 | + $this->assertEquals(['de', 'en'], $this->app->make('translatable.locales')->all()); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_all_language_locales() |
| 31 | + { |
| 32 | + $this->app['config']->set('translatable.locales', [ |
| 33 | + 'el', |
| 34 | + 'en', |
| 35 | + 'fr', |
| 36 | + 'de', |
| 37 | + 'id', |
| 38 | + ]); |
| 39 | + |
| 40 | + $this->assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->all()); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_all_country_locales() |
| 44 | + { |
| 45 | + $this->app['config']->set('translatable.locales', [ |
| 46 | + 'en' => [ |
| 47 | + 'GB', |
| 48 | + 'US', |
| 49 | + ], |
| 50 | + 'de' => [ |
| 51 | + 'DE', |
| 52 | + 'CH', |
| 53 | + ], |
| 54 | + ]); |
| 55 | + |
| 56 | + $this->assertEquals(['en', 'en-GB', 'en-US', 'de', 'de-DE', 'de-CH'], $this->app->make('translatable.locales')->all()); |
| 57 | + } |
| 58 | + |
| 59 | + public function test_to_array() |
| 60 | + { |
| 61 | + $this->app['config']->set('translatable.locales', [ |
| 62 | + 'el', |
| 63 | + 'en', |
| 64 | + 'fr', |
| 65 | + 'de', |
| 66 | + 'id', |
| 67 | + ]); |
| 68 | + |
| 69 | + $this->assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->toArray()); |
| 70 | + } |
| 71 | + |
| 72 | + public function test_current_config() |
| 73 | + { |
| 74 | + $this->app['config']->set('translatable.locale', 'de'); |
| 75 | + |
| 76 | + $this->assertEquals('de', $this->app->make('translatable.locales')->current()); |
| 77 | + } |
| 78 | + |
| 79 | + public function test_current_translator() |
| 80 | + { |
| 81 | + $this->app['config']->set('translatable.locale', null); |
| 82 | + $this->app['translator']->setLocale('en'); |
| 83 | + |
| 84 | + $this->assertEquals('en', $this->app->make('translatable.locales')->current()); |
| 85 | + } |
| 86 | + |
| 87 | + public function test_has() |
| 88 | + { |
| 89 | + $this->app['config']->set('translatable.locales', [ |
| 90 | + 'el', |
| 91 | + 'en', |
| 92 | + 'fr', |
| 93 | + 'de', |
| 94 | + 'id', |
| 95 | + ]); |
| 96 | + |
| 97 | + $this->assertTrue($this->app->make('translatable.locales')->has('de')); |
| 98 | + $this->assertFalse($this->app->make('translatable.locales')->has('jp')); |
| 99 | + } |
| 100 | + |
| 101 | + public function test_offset_exists() |
| 102 | + { |
| 103 | + $this->app['config']->set('translatable.locales', [ |
| 104 | + 'el', |
| 105 | + 'en', |
| 106 | + 'fr', |
| 107 | + 'de', |
| 108 | + 'id', |
| 109 | + ]); |
| 110 | + |
| 111 | + $this->assertTrue(isset($this->app->make('translatable.locales')['de'])); |
| 112 | + $this->assertFalse(isset($this->app->make('translatable.locales')['jp'])); |
| 113 | + } |
| 114 | + |
| 115 | + public function test_get() |
| 116 | + { |
| 117 | + $this->app['config']->set('translatable.locales', [ |
| 118 | + 'el', |
| 119 | + 'en', |
| 120 | + 'fr', |
| 121 | + 'de', |
| 122 | + 'id', |
| 123 | + ]); |
| 124 | + |
| 125 | + $this->assertEquals('de', $this->app->make('translatable.locales')->get('de')); |
| 126 | + $this->assertNull($this->app->make('translatable.locales')->get('jp')); |
| 127 | + } |
| 128 | + |
| 129 | + public function test_offset_get() |
| 130 | + { |
| 131 | + $this->app['config']->set('translatable.locales', [ |
| 132 | + 'el', |
| 133 | + 'en', |
| 134 | + 'fr', |
| 135 | + 'de', |
| 136 | + 'id', |
| 137 | + ]); |
| 138 | + |
| 139 | + $this->assertEquals('de', $this->app->make('translatable.locales')['de']); |
| 140 | + $this->assertNull($this->app->make('translatable.locales')['jp']); |
| 141 | + } |
| 142 | + |
| 143 | + public function test_add_language_locale() |
| 144 | + { |
| 145 | + $this->app['config']->set('translatable.locales', [ |
| 146 | + 'de', |
| 147 | + ]); |
| 148 | + $this->assertTrue($this->app->make('translatable.locales')->has('de')); |
| 149 | + $this->assertFalse($this->app->make('translatable.locales')->has('en')); |
| 150 | + $this->app->make('translatable.locales')->add('en'); |
| 151 | + $this->assertTrue($this->app->make('translatable.locales')->has('en')); |
| 152 | + } |
| 153 | + |
| 154 | + public function test_offset_set_language_locale() |
| 155 | + { |
| 156 | + $this->app['config']->set('translatable.locales', [ |
| 157 | + 'de', |
| 158 | + ]); |
| 159 | + $this->assertTrue($this->app->make('translatable.locales')->has('de')); |
| 160 | + $this->assertFalse($this->app->make('translatable.locales')->has('en')); |
| 161 | + $this->app->make('translatable.locales')[] = 'en'; |
| 162 | + $this->assertTrue($this->app->make('translatable.locales')->has('en')); |
| 163 | + } |
| 164 | + |
| 165 | + public function test_offset_set_country_locale() |
| 166 | + { |
| 167 | + $this->app['config']->set('translatable.locales', [ |
| 168 | + 'de', |
| 169 | + ]); |
| 170 | + $this->assertTrue($this->app->make('translatable.locales')->has('de')); |
| 171 | + $this->assertFalse($this->app->make('translatable.locales')->has('de-AT')); |
| 172 | + $this->app->make('translatable.locales')['de'] = 'AT'; |
| 173 | + $this->assertTrue($this->app->make('translatable.locales')->has('de-AT')); |
| 174 | + } |
| 175 | + |
| 176 | + public function test_forget() |
| 177 | + { |
| 178 | + $this->app['config']->set('translatable.locales', [ |
| 179 | + 'de', |
| 180 | + 'en', |
| 181 | + ]); |
| 182 | + $this->assertTrue($this->app->make('translatable.locales')->has('de')); |
| 183 | + $this->assertTrue($this->app->make('translatable.locales')->has('en')); |
| 184 | + $this->app->make('translatable.locales')->forget('en'); |
| 185 | + $this->assertFalse($this->app->make('translatable.locales')->has('en')); |
| 186 | + } |
| 187 | + |
| 188 | + public function test_offset_unset() |
| 189 | + { |
| 190 | + $this->app['config']->set('translatable.locales', [ |
| 191 | + 'de', |
| 192 | + 'en', |
| 193 | + ]); |
| 194 | + $this->assertTrue($this->app->make('translatable.locales')->has('de')); |
| 195 | + $this->assertTrue($this->app->make('translatable.locales')->has('en')); |
| 196 | + unset($this->app->make('translatable.locales')['en']); |
| 197 | + $this->assertFalse($this->app->make('translatable.locales')->has('en')); |
| 198 | + } |
| 199 | + |
| 200 | + public function test_get_locale_separator_config() |
| 201 | + { |
| 202 | + $this->app['config']->set('translatable.locale_separator', '_'); |
| 203 | + |
| 204 | + $this->assertEquals('_', $this->app->make('translatable.locales')->getLocaleSeparator()); |
| 205 | + } |
| 206 | + |
| 207 | + public function test_get_locale_separator_default() |
| 208 | + { |
| 209 | + $this->app['config']->set('translatable.locale_separator', null); |
| 210 | + |
| 211 | + $this->assertEquals('-', $this->app->make('translatable.locales')->getLocaleSeparator()); |
| 212 | + } |
| 213 | + |
| 214 | + public function test_get_country_locale() |
| 215 | + { |
| 216 | + $this->assertEquals('de-AT', $this->app->make('translatable.locales')->getCountryLocale('de', 'AT')); |
| 217 | + } |
| 218 | + |
| 219 | + public function test_is_locale_country_based() |
| 220 | + { |
| 221 | + $this->assertTrue($this->app->make('translatable.locales')->isLocaleCountryBased('de-AT')); |
| 222 | + $this->assertFalse($this->app->make('translatable.locales')->isLocaleCountryBased('de')); |
| 223 | + } |
| 224 | + |
| 225 | + public function test_get_language_from_country_based_locale() |
| 226 | + { |
| 227 | + $this->assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de-AT')); |
| 228 | + $this->assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de')); |
| 229 | + } |
| 230 | +} |
0 commit comments