minor #14291 [Translator]Test case refactoring and simplifications for the Translator class (mpdude)
This PR was merged into the 2.7 branch. Discussion ---------- [Translator]Test case refactoring and simplifications for the Translator class | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Fixed tickets | ~ | Tests pass? | yes | License | MIT Also see #14280 and #14281. This also tests for the fix in [b5da2ae876
](b5da2ae876 (diff-3126bfacb25e1e304b8cc720e8527a19)
) Commits -------c0300f5
Remaining tweaks for translator tests on 2.7
This commit is contained in:
commit
0cd300f2d9
@ -12,7 +12,6 @@
|
||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
|
||||
use Symfony\Component\Translation\Loader\ArrayLoader;
|
||||
use Symfony\Component\Translation\MessageCatalogue;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Translation\MessageSelector;
|
||||
@ -105,7 +104,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$translator->trans('foo');
|
||||
}
|
||||
|
||||
public function testLoadRessourcesWithCaching()
|
||||
public function testLoadResourcesWithCaching()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$resourceFiles = array(
|
||||
@ -133,7 +132,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('folder', $translator->trans('folder'));
|
||||
}
|
||||
|
||||
public function testLoadRessourcesWithoutCaching()
|
||||
public function testLoadResourcesWithoutCaching()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$resourceFiles = array(
|
||||
@ -271,17 +270,20 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
|
||||
),
|
||||
);
|
||||
$catalogueHash = sha1(serialize(array(
|
||||
'resources' => array(),
|
||||
'fallback_locales' => array(),
|
||||
)));
|
||||
|
||||
// prime the cache
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles), 'yml');
|
||||
|
||||
$this->assertFalse(file_exists($this->tmpDir.'/catalogue.fr.'.$catalogueHash.'.php'));
|
||||
$translator->setLocale('fr');
|
||||
$translator->warmup($this->tmpDir);
|
||||
$this->assertTrue(file_exists($this->tmpDir.'/catalogue.fr.'.$catalogueHash.'.php'));
|
||||
|
||||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
||||
$loader
|
||||
->expects($this->never())
|
||||
->method('load');
|
||||
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles), 'yml');
|
||||
$translator->setLocale('fr');
|
||||
$this->assertEquals('répertoire', $translator->trans('folder'));
|
||||
}
|
||||
|
||||
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader')
|
||||
|
@ -150,6 +150,32 @@ class TranslatorCacheTest extends \PHPUnit_Framework_TestCase
|
||||
$translator->trans($msgid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider runForDebugAndProduction
|
||||
*/
|
||||
public function testDifferentTranslatorsForSameLocaleDoNotInterfere($debug)
|
||||
{
|
||||
$locale = 'any_locale';
|
||||
$format = 'some_format';
|
||||
$msgid = 'test';
|
||||
|
||||
// Create a Translator and prime its cache
|
||||
$translator = new Translator($locale, null, $this->tmpDir, $debug);
|
||||
$translator->addLoader($format, new ArrayLoader());
|
||||
$translator->addResource($format, array($msgid => 'FAIL'), $locale);
|
||||
$translator->trans($msgid);
|
||||
|
||||
/*
|
||||
* Create another Translator with the same locale but a different resource.
|
||||
* It should not use the first translator's cache but return the value from its own resource.
|
||||
*/
|
||||
$translator = new Translator($locale, null, $this->tmpDir, $debug);
|
||||
$translator->addLoader($format, new ArrayLoader());
|
||||
$translator->addResource($format, array($msgid => 'OK'), $locale);
|
||||
|
||||
$this->assertEquals('OK', $translator->trans($msgid), '-> different translators for the same domain interfere in '.($debug ? 'debug' : 'production'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider runForDebugAndProduction
|
||||
*/
|
||||
|
Reference in New Issue
Block a user