From 189313caee5badbc0ec345d6e1e1e6bfead64d42 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Sun, 19 Apr 2015 13:37:15 +0100 Subject: [PATCH] [2.6][Translation] fix legacy tests. --- .../Tests/Translation/TranslatorTest.php | 125 +----------------- .../Translation/Tests/TranslatorCacheTest.php | 28 +++- 2 files changed, 32 insertions(+), 121 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php index 3617d3858a..331deea2ba 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php @@ -95,27 +95,6 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax')); } - public function testRefreshCacheWhenResourcesAreNoLongerFresh() - { - $resource = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface'); - $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); - $resource->method('isFresh')->will($this->returnValue(false)); - $loader - ->expects($this->exactly(2)) - ->method('load') - ->will($this->returnValue($this->getCatalogue('fr', array(), array($resource)))); - - // prime the cache - $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'debug' => true)); - $translator->setLocale('fr'); - $translator->trans('foo'); - - // prime the cache second time - $translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'debug' => true)); - $translator->setLocale('fr'); - $translator->trans('foo'); - } - public function testTransWithCachingWithInvalidLocale() { $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); @@ -126,115 +105,21 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase $translator->trans('foo'); } - public function testGetLocale() + public function testGetDefaultLocale() { - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - - $request + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container ->expects($this->once()) - ->method('getLocale') + ->method('getParameter') + ->with('kernel.default_locale') ->will($this->returnValue('en')) ; - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - - $container - ->expects($this->exactly(2)) - ->method('isScopeActive') - ->with('request') - ->will($this->onConsecutiveCalls(false, true)) - ; - - $container - ->expects($this->once()) - ->method('has') - ->with('request') - ->will($this->returnValue(true)) - ; - - $container - ->expects($this->once()) - ->method('get') - ->with('request') - ->will($this->returnValue($request)) - ; - $translator = new Translator($container, new MessageSelector()); - $this->assertNull($translator->getLocale()); $this->assertSame('en', $translator->getLocale()); } - public function testGetLocaleWithInvalidLocale() - { - $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); - - $request - ->expects($this->once()) - ->method('getLocale') - ->will($this->returnValue('foo bar')) - ; - $request - ->expects($this->once()) - ->method('getDefaultLocale') - ->will($this->returnValue('en-US')) - ; - - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - - $container - ->expects($this->once()) - ->method('isScopeActive') - ->with('request') - ->will($this->returnValue(true)) - ; - - $container - ->expects($this->once()) - ->method('has') - ->with('request') - ->will($this->returnValue(true)) - ; - - $container - ->expects($this->any()) - ->method('get') - ->with('request') - ->will($this->returnValue($request)) - ; - - $translator = new Translator($container, new MessageSelector()); - $this->assertSame('en-US', $translator->getLocale()); - } - - public function testDifferentCacheFilesAreUsedForDifferentSetsOfFallbackLocales() - { - /* - * Because the cache file contains a catalogue including all of its fallback - * catalogues, we must take the active set of fallback locales into - * consideration when loading a catalogue from the cache. - */ - $translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir)); - $translator->setLocale('a'); - $translator->setFallbackLocales(array('b')); - $translator->addResource('loader', array('foo' => 'foo (a)'), 'a'); - $translator->addResource('loader', array('bar' => 'bar (b)'), 'b'); - - $this->assertEquals('bar (b)', $translator->trans('bar')); - - // Remove fallback locale - $translator->setFallbackLocales(array()); - $this->assertEquals('bar', $translator->trans('bar')); - - // Use a fresh translator with no fallback locales, result should be the same - $translator = $this->createTranslator(new ArrayLoader(), array('cache_dir' => $this->tmpDir)); - $translator->setLocale('a'); - $translator->addResource('loader', array('foo' => 'foo (a)'), 'a'); - $translator->addResource('loader', array('bar' => 'bar (b)'), 'b'); - - $this->assertEquals('bar', $translator->trans('bar')); - } - protected function getCatalogue($locale, $messages, $resources = array()) { $catalogue = new MessageCatalogue($locale); diff --git a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php index 0cd4a7619a..14631ba910 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php @@ -161,12 +161,38 @@ class TranslatorCacheTest extends \PHPUnit_Framework_TestCase $this->assertEquals('bar', $translator->trans('bar')); } - protected function getCatalogue($locale, $messages) + public function testRefreshCacheWhenResourcesAreNoLongerFresh() + { + $resource = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface'); + $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); + $resource->method('isFresh')->will($this->returnValue(false)); + $loader + ->expects($this->exactly(2)) + ->method('load') + ->will($this->returnValue($this->getCatalogue('fr', array(), array($resource)))); + + // prime the cache + $translator = new Translator('fr', null, $this->tmpDir, true); + $translator->addLoader('loader', $loader); + $translator->addResource('loader', 'foo', 'fr'); + $translator->trans('foo'); + + // prime the cache second time + $translator = new Translator('fr', null, $this->tmpDir, true); + $translator->addLoader('loader', $loader); + $translator->addResource('loader', 'foo', 'fr'); + $translator->trans('foo'); + } + + protected function getCatalogue($locale, $messages, $resources = array()) { $catalogue = new MessageCatalogue($locale); foreach ($messages as $key => $translation) { $catalogue->set($key, $translation); } + foreach ($resources as $resource) { + $catalogue->addResource($resource); + } return $catalogue; }