Няма описание

TranslatorCacheTest.php 11KB

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