|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Translation\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\Translation\Translator; |
| 15 | +use Symfony\Component\Translation\MessageCatalogue; |
| 16 | +use Symfony\Component\Translation\MessageSelector; |
| 17 | + |
| 18 | +class TranslatorCacheTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + protected $tmpDir; |
| 21 | + |
| 22 | + protected function setUp() |
| 23 | + { |
| 24 | + $this->tmpDir = sys_get_temp_dir().'/sf2_translation'; |
| 25 | + $this->deleteTmpDir(); |
| 26 | + } |
| 27 | + |
| 28 | + public function tearDown() |
| 29 | + { |
| 30 | + $this->deleteTmpDir(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function deleteTmpDir() |
| 34 | + { |
| 35 | + if (!file_exists($dir = $this->tmpDir)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir), \RecursiveIteratorIterator::CHILD_FIRST); |
| 40 | + foreach ($iterator as $path) { |
| 41 | + if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + if ($path->isDir()) { |
| 45 | + rmdir($path->__toString()); |
| 46 | + } else { |
| 47 | + unlink($path->__toString()); |
| 48 | + } |
| 49 | + } |
| 50 | + rmdir($this->tmpDir); |
| 51 | + } |
| 52 | + |
| 53 | + public function testTransWithoutCaching() |
| 54 | + { |
| 55 | + $translator = $this->getTranslator($this->getLoader()); |
| 56 | + $translator->setLocale('fr'); |
| 57 | + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); |
| 58 | + |
| 59 | + $this->assertEquals('foo (FR)', $translator->trans('foo')); |
| 60 | + $this->assertEquals('bar (EN)', $translator->trans('bar')); |
| 61 | + $this->assertEquals('foobar (ES)', $translator->trans('foobar')); |
| 62 | + $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); |
| 63 | + $this->assertEquals('no translation', $translator->trans('no translation')); |
| 64 | + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); |
| 65 | + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); |
| 66 | + $this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); |
| 67 | + $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); |
| 68 | + } |
| 69 | + |
| 70 | + public function testTransWithCaching() |
| 71 | + { |
| 72 | + // prime the cache |
| 73 | + $translator = $this->getTranslator($this->getLoader(), $this->tmpDir); |
| 74 | + $translator->setLocale('fr'); |
| 75 | + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); |
| 76 | + |
| 77 | + $this->assertEquals('foo (FR)', $translator->trans('foo')); |
| 78 | + $this->assertEquals('bar (EN)', $translator->trans('bar')); |
| 79 | + $this->assertEquals('foobar (ES)', $translator->trans('foobar')); |
| 80 | + $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); |
| 81 | + $this->assertEquals('no translation', $translator->trans('no translation')); |
| 82 | + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); |
| 83 | + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); |
| 84 | + $this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); |
| 85 | + $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); |
| 86 | + |
| 87 | + // do it another time as the cache is primed now |
| 88 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 89 | + $translator = $this->getTranslator($loader, $this->tmpDir); |
| 90 | + $translator->setLocale('fr'); |
| 91 | + $translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin')); |
| 92 | + |
| 93 | + $this->assertEquals('foo (FR)', $translator->trans('foo')); |
| 94 | + $this->assertEquals('bar (EN)', $translator->trans('bar')); |
| 95 | + $this->assertEquals('foobar (ES)', $translator->trans('foobar')); |
| 96 | + $this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0)); |
| 97 | + $this->assertEquals('no translation', $translator->trans('no translation')); |
| 98 | + $this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo')); |
| 99 | + $this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1)); |
| 100 | + $this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz')); |
| 101 | + $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); |
| 102 | + } |
| 103 | + |
| 104 | + public function testTransWithCachingWithInvalidLocale() |
| 105 | + { |
| 106 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 107 | + $translator = $this->getTranslator($loader, $this->tmpDir, 'Symfony\Component\Translation\Tests\TranslatorWithInvalidLocale'); |
| 108 | + |
| 109 | + $translator->setLocale('invalid locale'); |
| 110 | + |
| 111 | + try { |
| 112 | + $translator->trans('foo'); |
| 113 | + $this->fail(); |
| 114 | + } catch (\InvalidArgumentException $e) { |
| 115 | + $this->assertFalse(file_exists($this->tmpDir.'/catalogue.invalid locale.php')); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public function testLoadCatalogueWithCachingWithInvalidLocale() |
| 120 | + { |
| 121 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 122 | + $translator = $this->getTranslator($loader, $this->tmpDir, 'Symfony\Component\Translation\Tests\TranslatorWithInvalidLocale'); |
| 123 | + |
| 124 | + try { |
| 125 | + $translator->proxyLoadCatalogue('invalid locale'); |
| 126 | + $this->fail(); |
| 127 | + } catch (\InvalidArgumentException $e) { |
| 128 | + $this->assertFalse(file_exists($this->tmpDir.'/catalogue.invalid locale.php')); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + protected function getCatalogue($locale, $messages) |
| 133 | + { |
| 134 | + $catalogue = new MessageCatalogue($locale); |
| 135 | + foreach ($messages as $key => $translation) { |
| 136 | + $catalogue->set($key, $translation); |
| 137 | + } |
| 138 | + |
| 139 | + return $catalogue; |
| 140 | + } |
| 141 | + |
| 142 | + protected function getLoader() |
| 143 | + { |
| 144 | + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); |
| 145 | + $loader |
| 146 | + ->expects($this->at(0)) |
| 147 | + ->method('load') |
| 148 | + ->will($this->returnValue($this->getCatalogue('fr', array( |
| 149 | + 'foo' => 'foo (FR)', |
| 150 | + )))) |
| 151 | + ; |
| 152 | + $loader |
| 153 | + ->expects($this->at(1)) |
| 154 | + ->method('load') |
| 155 | + ->will($this->returnValue($this->getCatalogue('en', array( |
| 156 | + 'foo' => 'foo (EN)', |
| 157 | + 'bar' => 'bar (EN)', |
| 158 | + 'choice' => '{0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)', |
| 159 | + )))) |
| 160 | + ; |
| 161 | + $loader |
| 162 | + ->expects($this->at(2)) |
| 163 | + ->method('load') |
| 164 | + ->will($this->returnValue($this->getCatalogue('es', array( |
| 165 | + 'foobar' => 'foobar (ES)', |
| 166 | + )))) |
| 167 | + ; |
| 168 | + $loader |
| 169 | + ->expects($this->at(3)) |
| 170 | + ->method('load') |
| 171 | + ->will($this->returnValue($this->getCatalogue('pt-PT', array( |
| 172 | + 'foobarfoo' => 'foobarfoo (PT-PT)', |
| 173 | + )))) |
| 174 | + ; |
| 175 | + $loader |
| 176 | + ->expects($this->at(4)) |
| 177 | + ->method('load') |
| 178 | + ->will($this->returnValue($this->getCatalogue('pt_BR', array( |
| 179 | + 'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)', |
| 180 | + )))) |
| 181 | + ; |
| 182 | + $loader |
| 183 | + ->expects($this->at(5)) |
| 184 | + ->method('load') |
| 185 | + ->will($this->returnValue($this->getCatalogue('fr.UTF-8', array( |
| 186 | + 'foobarbaz' => 'foobarbaz (fr.UTF-8)', |
| 187 | + )))) |
| 188 | + ; |
| 189 | + $loader |
| 190 | + ->expects($this->at(6)) |
| 191 | + ->method('load') |
| 192 | + ->will($this->returnValue($this->getCatalogue('sr@latin', array( |
| 193 | + 'foobarbax' => 'foobarbax (sr@latin)', |
| 194 | + )))) |
| 195 | + ; |
| 196 | + |
| 197 | + return $loader; |
| 198 | + } |
| 199 | + |
| 200 | + public function getTranslator($loader, $cacheDir = null, $translatorClass = '\Symfony\Component\Translation\Translator') |
| 201 | + { |
| 202 | + $translator = new $translatorClass('fr', new MessageSelector(), $cacheDir); |
| 203 | + |
| 204 | + $translator->addLoader('loader', $loader); |
| 205 | + $translator->addResource('loader', 'foo', 'fr'); |
| 206 | + $translator->addResource('loader', 'foo', 'en'); |
| 207 | + $translator->addResource('loader', 'foo', 'es'); |
| 208 | + $translator->addResource('loader', 'foo', 'pt-PT'); // European Portuguese |
| 209 | + $translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese |
| 210 | + $translator->addResource('loader', 'foo', 'fr.UTF-8'); |
| 211 | + $translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian |
| 212 | + |
| 213 | + return $translator; |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +class TranslatorWithInvalidLocale extends Translator |
| 218 | +{ |
| 219 | + /** |
| 220 | + * {@inheritdoc} |
| 221 | + */ |
| 222 | + public function setLocale($locale) |
| 223 | + { |
| 224 | + $this->locale = $locale; |
| 225 | + } |
| 226 | + |
| 227 | + public function proxyLoadCatalogue($locale) |
| 228 | + { |
| 229 | + $this->loadCatalogue($locale); |
| 230 | + } |
| 231 | +} |
0 commit comments