|
16 | 16 | use Symfony\Component\Translation\MessageSelector;
|
17 | 17 | use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCache;
|
18 | 18 | use Doctrine\Common\Cache\ArrayCache;
|
| 19 | +use Symfony\Component\Translation\Loader\PhpFileLoader; |
| 20 | +use Symfony\Component\Translation\Dumper\PhpFileDumper; |
19 | 21 |
|
20 | 22 | class TranslatorDoctrineCacheTest extends \PHPUnit_Framework_TestCase
|
21 | 23 | {
|
| 24 | + protected $tmpDir; |
| 25 | + |
22 | 26 | protected function setUp()
|
23 | 27 | {
|
24 | 28 | if (!interface_exists('Doctrine\Common\Cache\Cache')) {
|
25 | 29 | $this->markTestSkipped('The "Doctrine Cache" is not available');
|
26 | 30 | }
|
| 31 | + |
| 32 | + $this->tmpDir = sys_get_temp_dir().'/sf2_translation'; |
| 33 | + $this->deleteTmpDir(); |
| 34 | + } |
| 35 | + protected function tearDown() |
| 36 | + { |
| 37 | + $this->deleteTmpDir(); |
| 38 | + } |
| 39 | + |
| 40 | + protected function deleteTmpDir() |
| 41 | + { |
| 42 | + if (!file_exists($dir = $this->tmpDir)) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->tmpDir), \RecursiveIteratorIterator::CHILD_FIRST); |
| 47 | + foreach ($iterator as $path) { |
| 48 | + if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + if ($path->isDir()) { |
| 52 | + rmdir($path->__toString()); |
| 53 | + } else { |
| 54 | + unlink($path->__toString()); |
| 55 | + } |
| 56 | + } |
| 57 | + rmdir($this->tmpDir); |
27 | 58 | }
|
28 | 59 |
|
29 | 60 | public function testTrans()
|
@@ -98,6 +129,34 @@ public function testRefreshCacheWhenResourcesChange()
|
98 | 129 | $this->assertEquals('foo B', $translator->trans('foo'));
|
99 | 130 | }
|
100 | 131 |
|
| 132 | + public function testRefreshCacheWhenResourcesFileChange() |
| 133 | + { |
| 134 | + $resourceFile = $this->tmpDir.'/messages.fr.php'; |
| 135 | + $loader = new PhpFileLoader(); |
| 136 | + $dumper = new PhpFileDumper(); |
| 137 | + |
| 138 | + // prime the cache |
| 139 | + $cache = new DoctrineMessageCache(new ArrayCache(), true); |
| 140 | + $dumper->dump(new MessageCatalogue('fr', array('messages' => array('foo' => 'foo A'))), array('path' => $this->tmpDir)); |
| 141 | + |
| 142 | + $translator = new Translator('fr', new MessageSelector(), $cache); |
| 143 | + $translator->addLoader('loader', $loader); |
| 144 | + $translator->addResource('loader', $resourceFile, 'fr'); |
| 145 | + |
| 146 | + $this->assertEquals('foo A', $translator->trans('foo')); |
| 147 | + |
| 148 | + // add a new resource to refresh the cache |
| 149 | + $dumper->dump(new MessageCatalogue('fr', array('messages' => array('foo' => 'foo B'))), array('path' => $this->tmpDir)); |
| 150 | + touch($resourceFile, time() + 3600); |
| 151 | + clearstatcache(true, $resourceFile); |
| 152 | + |
| 153 | + $translator = new Translator('fr', new MessageSelector(), $cache); |
| 154 | + $translator->addLoader('loader', $loader); |
| 155 | + $translator->addResource('loader', $resourceFile, 'fr'); |
| 156 | + |
| 157 | + $this->assertEquals('foo B', $translator->trans('foo')); |
| 158 | + } |
| 159 | + |
101 | 160 | protected function getLoader()
|
102 | 161 | {
|
103 | 162 | $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
|
0 commit comments