SymfonyComponentTranslationTestsTranslatorCacheTest

The Simple Calendar Symfony Component Translation Tests TranslatorCacheTest class.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/Tests/TranslatorCacheTest.php  
  1. class TranslatorCacheTest extends \PHPUnit_Framework_TestCase 
  2. protected $tmpDir; 
  3.  
  4. protected function setUp() 
  5. $this->tmpDir = sys_get_temp_dir().'/sf2_translation'; 
  6. $this->deleteTmpDir(); 
  7.  
  8. protected function tearDown() 
  9. $this->deleteTmpDir(); 
  10.  
  11. protected function deleteTmpDir() 
  12. if (!file_exists($dir = $this->tmpDir)) { 
  13. return; 
  14.  
  15. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir), \RecursiveIteratorIterator::CHILD_FIRST); 
  16. foreach ($iterator as $path) { 
  17. if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) { 
  18. continue; 
  19. if ($path->isDir()) { 
  20. rmdir($path->__toString()); 
  21. } else { 
  22. unlink($path->__toString()); 
  23. rmdir($this->tmpDir); 
  24.  
  25. /** 
  26. * @dataProvider runForDebugAndProduction 
  27. */ 
  28. public function testThatACacheIsUsed($debug) 
  29. $locale = 'any_locale'; 
  30. $format = 'some_format'; 
  31. $msgid = 'test'; 
  32.  
  33. // Prime the cache 
  34. $translator = new Translator($locale, null, $this->tmpDir, $debug); 
  35. $translator->addLoader($format, new ArrayLoader()); 
  36. $translator->addResource($format, array($msgid => 'OK'), $locale); 
  37. $translator->trans($msgid); 
  38.  
  39. // Try again and see we get a valid result whilst no loader can be used 
  40. $translator = new Translator($locale, null, $this->tmpDir, $debug); 
  41. $translator->addLoader($format, $this->createFailingLoader()); 
  42. $translator->addResource($format, array($msgid => 'OK'), $locale); 
  43. $this->assertEquals('OK', $translator->trans($msgid), '-> caching does not work in '.($debug ? 'debug' : 'production')); 
  44.  
  45. public function testCatalogueIsReloadedWhenResourcesAreNoLongerFresh() 
  46. /** 
  47. * The testThatACacheIsUsed() test showed that we don't need the loader as long as the cache 
  48. * is fresh. 
  49. * Now we add a Resource that is never fresh and make sure that the 
  50. * cache is discarded (the loader is called twice). 
  51. * We need to run this for debug=true only because in production the cache 
  52. * will never be revalidated. 
  53. */ 
  54.  
  55. $locale = 'any_locale'; 
  56. $format = 'some_format'; 
  57. $msgid = 'test'; 
  58.  
  59. $catalogue = new MessageCatalogue($locale, array()); 
  60. $catalogue->addResource(new StaleResource()); // better use a helper class than a mock, because it gets serialized in the cache and re-loaded 
  61.  
  62. /** @var LoaderInterface|\PHPUnit_Framework_MockObject_MockObject $loader */ 
  63. $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); 
  64. $loader 
  65. ->expects($this->exactly(2)) 
  66. ->method('load') 
  67. ->will($this->returnValue($catalogue)) 
  68.  
  69. // 1st pass 
  70. $translator = new Translator($locale, null, $this->tmpDir, true); 
  71. $translator->addLoader($format, $loader); 
  72. $translator->addResource($format, null, $locale); 
  73. $translator->trans($msgid); 
  74.  
  75. // 2nd pass 
  76. $translator = new Translator($locale, null, $this->tmpDir, true); 
  77. $translator->addLoader($format, $loader); 
  78. $translator->addResource($format, null, $locale); 
  79. $translator->trans($msgid); 
  80.  
  81. /** 
  82. * @dataProvider runForDebugAndProduction 
  83. */ 
  84. public function testDifferentTranslatorsForSameLocaleDoNotOverwriteEachOthersCache($debug) 
  85. /** 
  86. * Similar to the previous test. After we used the second translator, make 
  87. * sure there's still a useable cache for the first one. 
  88. */ 
  89.  
  90. $locale = 'any_locale'; 
  91. $format = 'some_format'; 
  92. $msgid = 'test'; 
  93.  
  94. // Create a Translator and prime its cache 
  95. $translator = new Translator($locale, null, $this->tmpDir, $debug); 
  96. $translator->addLoader($format, new ArrayLoader()); 
  97. $translator->addResource($format, array($msgid => 'OK'), $locale); 
  98. $translator->trans($msgid); 
  99.  
  100. // Create another Translator with a different catalogue for the same locale 
  101. $translator = new Translator($locale, null, $this->tmpDir, $debug); 
  102. $translator->addLoader($format, new ArrayLoader()); 
  103. $translator->addResource($format, array($msgid => 'FAIL'), $locale); 
  104. $translator->trans($msgid); 
  105.  
  106. // Now the first translator must still have a useable cache. 
  107. $translator = new Translator($locale, null, $this->tmpDir, $debug); 
  108. $translator->addLoader($format, $this->createFailingLoader()); 
  109. $translator->addResource($format, array($msgid => 'OK'), $locale); 
  110. $this->assertEquals('OK', $translator->trans($msgid), '-> the cache was overwritten by another translator instance in '.($debug ? 'debug' : 'production')); 
  111.  
  112. public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales() 
  113. /** 
  114. * Because the cache file contains a catalogue including all of its fallback 
  115. * catalogues, we must take the set of fallback locales into consideration when 
  116. * loading a catalogue from the cache. 
  117. */ 
  118. $translator = new Translator('a', null, $this->tmpDir); 
  119. $translator->setFallbackLocales(array('b')); 
  120.  
  121. $translator->addLoader('array', new ArrayLoader()); 
  122. $translator->addResource('array', array('foo' => 'foo (a)'), 'a'); 
  123. $translator->addResource('array', array('bar' => 'bar (b)'), 'b'); 
  124.  
  125. $this->assertEquals('bar (b)', $translator->trans('bar')); 
  126.  
  127. // Remove fallback locale 
  128. $translator->setFallbackLocales(array()); 
  129. $this->assertEquals('bar', $translator->trans('bar')); 
  130.  
  131. // Use a fresh translator with no fallback locales, result should be the same 
  132. $translator = new Translator('a', null, $this->tmpDir); 
  133.  
  134. $translator->addLoader('array', new ArrayLoader()); 
  135. $translator->addResource('array', array('foo' => 'foo (a)'), 'a'); 
  136. $translator->addResource('array', array('bar' => 'bar (b)'), 'b'); 
  137.  
  138. $this->assertEquals('bar', $translator->trans('bar')); 
  139.  
  140. public function testPrimaryAndFallbackCataloguesContainTheSameMessagesRegardlessOfCaching() 
  141. /** 
  142. * As a safeguard against potential BC breaks, make sure that primary and fallback 
  143. * catalogues (reachable via getFallbackCatalogue()) always contain the full set of 
  144. * messages provided by the loader. This must also be the case when these catalogues 
  145. * are (internally) read from a cache. 
  146. * Optimizations inside the translator must not change this behaviour. 
  147. */ 
  148.  
  149. /** 
  150. * Create a translator that loads two catalogues for two different locales. 
  151. * The catalogues contain distinct sets of messages. 
  152. */ 
  153. $translator = new Translator('a', null, $this->tmpDir); 
  154. $translator->setFallbackLocales(array('b')); 
  155.  
  156. $translator->addLoader('array', new ArrayLoader()); 
  157. $translator->addResource('array', array('foo' => 'foo (a)'), 'a'); 
  158. $translator->addResource('array', array('foo' => 'foo (b)'), 'b'); 
  159. $translator->addResource('array', array('bar' => 'bar (b)'), 'b'); 
  160.  
  161. $catalogue = $translator->getCatalogue('a'); 
  162. $this->assertFalse($catalogue->defines('bar')); // Sure, the "a" catalogue does not contain that message. 
  163.  
  164. $fallback = $catalogue->getFallbackCatalogue(); 
  165. $this->assertTrue($fallback->defines('foo')); // "foo" is present in "a" and "b" 
  166.  
  167. /** 
  168. * Now, repeat the same test. 
  169. * Behind the scenes, the cache is used. But that should not matter, right? 
  170. */ 
  171. $translator = new Translator('a', null, $this->tmpDir); 
  172. $translator->setFallbackLocales(array('b')); 
  173.  
  174. $translator->addLoader('array', new ArrayLoader()); 
  175. $translator->addResource('array', array('foo' => 'foo (a)'), 'a'); 
  176. $translator->addResource('array', array('foo' => 'foo (b)'), 'b'); 
  177. $translator->addResource('array', array('bar' => 'bar (b)'), 'b'); 
  178.  
  179. $catalogue = $translator->getCatalogue('a'); 
  180. $this->assertFalse($catalogue->defines('bar')); 
  181.  
  182. $fallback = $catalogue->getFallbackCatalogue(); 
  183. $this->assertTrue($fallback->defines('foo')); 
  184.  
  185. public function testRefreshCacheWhenResourcesAreNoLongerFresh() 
  186. $resource = $this->getMock('Symfony\Component\Config\Resource\SelfCheckingResourceInterface'); 
  187. $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); 
  188. $resource->method('isFresh')->will($this->returnValue(false)); 
  189. $loader 
  190. ->expects($this->exactly(2)) 
  191. ->method('load') 
  192. ->will($this->returnValue($this->getCatalogue('fr', array(), array($resource)))); 
  193.  
  194. // prime the cache 
  195. $translator = new Translator('fr', null, $this->tmpDir, true); 
  196. $translator->addLoader('loader', $loader); 
  197. $translator->addResource('loader', 'foo', 'fr'); 
  198. $translator->trans('foo'); 
  199.  
  200. // prime the cache second time 
  201. $translator = new Translator('fr', null, $this->tmpDir, true); 
  202. $translator->addLoader('loader', $loader); 
  203. $translator->addResource('loader', 'foo', 'fr'); 
  204. $translator->trans('foo'); 
  205.  
  206. protected function getCatalogue($locale, $messages, $resources = array()) 
  207. $catalogue = new MessageCatalogue($locale); 
  208. foreach ($messages as $key => $translation) { 
  209. $catalogue->set($key, $translation); 
  210. foreach ($resources as $resource) { 
  211. $catalogue->addResource($resource); 
  212.  
  213. return $catalogue; 
  214.  
  215. public function runForDebugAndProduction() 
  216. return array(array(true), array(false)); 
  217.  
  218. /** 
  219. * @return LoaderInterface 
  220. */ 
  221. private function createFailingLoader() 
  222. $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); 
  223. $loader 
  224. ->expects($this->never()) 
  225. ->method('load'); 
  226.  
  227. return $loader;