4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
16 use Symfony\Component\Translation\Loader\ArrayLoader;
17 use Symfony\Component\Translation\Loader\LoaderInterface;
18 use Symfony\Component\Translation\Translator;
19 use Symfony\Component\Translation\MessageCatalogue;
21 class TranslatorCacheTest extends TestCase
25 protected function setUp()
27 $this->tmpDir = sys_get_temp_dir().'/sf2_translation';
28 $this->deleteTmpDir();
31 protected function tearDown()
33 $this->deleteTmpDir();
36 protected function deleteTmpDir()
38 if (!file_exists($dir = $this->tmpDir)) {
42 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir), \RecursiveIteratorIterator::CHILD_FIRST);
43 foreach ($iterator as $path) {
44 if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) {
48 rmdir($path->__toString());
50 unlink($path->__toString());
57 * @dataProvider runForDebugAndProduction
59 public function testThatACacheIsUsed($debug)
61 $locale = 'any_locale';
62 $format = 'some_format';
66 $translator = new Translator($locale, null, $this->tmpDir, $debug);
67 $translator->addLoader($format, new ArrayLoader());
68 $translator->addResource($format, array($msgid => 'OK'), $locale);
69 $translator->trans($msgid);
71 // Try again and see we get a valid result whilst no loader can be used
72 $translator = new Translator($locale, null, $this->tmpDir, $debug);
73 $translator->addLoader($format, $this->createFailingLoader());
74 $translator->addResource($format, array($msgid => 'OK'), $locale);
75 $this->assertEquals('OK', $translator->trans($msgid), '-> caching does not work in '.($debug ? 'debug' : 'production'));
78 public function testCatalogueIsReloadedWhenResourcesAreNoLongerFresh()
81 * The testThatACacheIsUsed() test showed that we don't need the loader as long as the cache
84 * Now we add a Resource that is never fresh and make sure that the
85 * cache is discarded (the loader is called twice).
87 * We need to run this for debug=true only because in production the cache
88 * will never be revalidated.
91 $locale = 'any_locale';
92 $format = 'some_format';
95 $catalogue = new MessageCatalogue($locale, array());
96 $catalogue->addResource(new StaleResource()); // better use a helper class than a mock, because it gets serialized in the cache and re-loaded
98 /** @var LoaderInterface|\PHPUnit_Framework_MockObject_MockObject $loader */
99 $loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
101 ->expects($this->exactly(2))
103 ->will($this->returnValue($catalogue))
107 $translator = new Translator($locale, null, $this->tmpDir, true);
108 $translator->addLoader($format, $loader);
109 $translator->addResource($format, null, $locale);
110 $translator->trans($msgid);
113 $translator = new Translator($locale, null, $this->tmpDir, true);
114 $translator->addLoader($format, $loader);
115 $translator->addResource($format, null, $locale);
116 $translator->trans($msgid);
120 * @dataProvider runForDebugAndProduction
122 public function testDifferentTranslatorsForSameLocaleDoNotOverwriteEachOthersCache($debug)
125 * Similar to the previous test. After we used the second translator, make
126 * sure there's still a useable cache for the first one.
129 $locale = 'any_locale';
130 $format = 'some_format';
133 // Create a Translator and prime its cache
134 $translator = new Translator($locale, null, $this->tmpDir, $debug);
135 $translator->addLoader($format, new ArrayLoader());
136 $translator->addResource($format, array($msgid => 'OK'), $locale);
137 $translator->trans($msgid);
139 // Create another Translator with a different catalogue for the same locale
140 $translator = new Translator($locale, null, $this->tmpDir, $debug);
141 $translator->addLoader($format, new ArrayLoader());
142 $translator->addResource($format, array($msgid => 'FAIL'), $locale);
143 $translator->trans($msgid);
145 // Now the first translator must still have a useable cache.
146 $translator = new Translator($locale, null, $this->tmpDir, $debug);
147 $translator->addLoader($format, $this->createFailingLoader());
148 $translator->addResource($format, array($msgid => 'OK'), $locale);
149 $this->assertEquals('OK', $translator->trans($msgid), '-> the cache was overwritten by another translator instance in '.($debug ? 'debug' : 'production'));
152 public function testGeneratedCacheFilesAreOnlyBelongRequestedLocales()
154 $translator = new Translator('a', null, $this->tmpDir);
155 $translator->setFallbackLocales(array('b'));
156 $translator->trans('bar');
158 $cachedFiles = glob($this->tmpDir.'/*.php');
160 $this->assertCount(1, $cachedFiles);
163 public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales()
166 * Because the cache file contains a catalogue including all of its fallback
167 * catalogues, we must take the set of fallback locales into consideration when
168 * loading a catalogue from the cache.
170 $translator = new Translator('a', null, $this->tmpDir);
171 $translator->setFallbackLocales(array('b'));
173 $translator->addLoader('array', new ArrayLoader());
174 $translator->addResource('array', array('foo' => 'foo (a)'), 'a');
175 $translator->addResource('array', array('bar' => 'bar (b)'), 'b');
177 $this->assertEquals('bar (b)', $translator->trans('bar'));
179 // Remove fallback locale
180 $translator->setFallbackLocales(array());
181 $this->assertEquals('bar', $translator->trans('bar'));
183 // Use a fresh translator with no fallback locales, result should be the same
184 $translator = new Translator('a', null, $this->tmpDir);
186 $translator->addLoader('array', new ArrayLoader());
187 $translator->addResource('array', array('foo' => 'foo (a)'), 'a');
188 $translator->addResource('array', array('bar' => 'bar (b)'), 'b');
190 $this->assertEquals('bar', $translator->trans('bar'));
193 public function testPrimaryAndFallbackCataloguesContainTheSameMessagesRegardlessOfCaching()
196 * As a safeguard against potential BC breaks, make sure that primary and fallback
197 * catalogues (reachable via getFallbackCatalogue()) always contain the full set of
198 * messages provided by the loader. This must also be the case when these catalogues
199 * are (internally) read from a cache.
201 * Optimizations inside the translator must not change this behaviour.
205 * Create a translator that loads two catalogues for two different locales.
206 * The catalogues contain distinct sets of messages.
208 $translator = new Translator('a', null, $this->tmpDir);
209 $translator->setFallbackLocales(array('b'));
211 $translator->addLoader('array', new ArrayLoader());
212 $translator->addResource('array', array('foo' => 'foo (a)'), 'a');
213 $translator->addResource('array', array('foo' => 'foo (b)'), 'b');
214 $translator->addResource('array', array('bar' => 'bar (b)'), 'b');
216 $catalogue = $translator->getCatalogue('a');
217 $this->assertFalse($catalogue->defines('bar')); // Sure, the "a" catalogue does not contain that message.
219 $fallback = $catalogue->getFallbackCatalogue();
220 $this->assertTrue($fallback->defines('foo')); // "foo" is present in "a" and "b"
223 * Now, repeat the same test.
224 * Behind the scenes, the cache is used. But that should not matter, right?
226 $translator = new Translator('a', null, $this->tmpDir);
227 $translator->setFallbackLocales(array('b'));
229 $translator->addLoader('array', new ArrayLoader());
230 $translator->addResource('array', array('foo' => 'foo (a)'), 'a');
231 $translator->addResource('array', array('foo' => 'foo (b)'), 'b');
232 $translator->addResource('array', array('bar' => 'bar (b)'), 'b');
234 $catalogue = $translator->getCatalogue('a');
235 $this->assertFalse($catalogue->defines('bar'));
237 $fallback = $catalogue->getFallbackCatalogue();
238 $this->assertTrue($fallback->defines('foo'));
241 public function testRefreshCacheWhenResourcesAreNoLongerFresh()
243 $resource = $this->getMockBuilder('Symfony\Component\Config\Resource\SelfCheckingResourceInterface')->getMock();
244 $loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
245 $resource->method('isFresh')->will($this->returnValue(false));
247 ->expects($this->exactly(2))
249 ->will($this->returnValue($this->getCatalogue('fr', array(), array($resource))));
252 $translator = new Translator('fr', null, $this->tmpDir, true);
253 $translator->addLoader('loader', $loader);
254 $translator->addResource('loader', 'foo', 'fr');
255 $translator->trans('foo');
257 // prime the cache second time
258 $translator = new Translator('fr', null, $this->tmpDir, true);
259 $translator->addLoader('loader', $loader);
260 $translator->addResource('loader', 'foo', 'fr');
261 $translator->trans('foo');
264 protected function getCatalogue($locale, $messages, $resources = array())
266 $catalogue = new MessageCatalogue($locale);
267 foreach ($messages as $key => $translation) {
268 $catalogue->set($key, $translation);
270 foreach ($resources as $resource) {
271 $catalogue->addResource($resource);
277 public function runForDebugAndProduction()
279 return array(array(true), array(false));
283 * @return LoaderInterface
285 private function createFailingLoader()
287 $loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
289 ->expects($this->never())
296 class StaleResource implements SelfCheckingResourceInterface
298 public function isFresh($timestamp)
303 public function getResource()
307 public function __toString()