Skip to content

Commit

Permalink
minor #14291 [Translator]Test case refactoring and simplifications fo…
Browse files Browse the repository at this point in the history
…r 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 [b5da2ae](b5da2ae#diff-3126bfacb25e1e304b8cc720e8527a19)

Commits
-------

c0300f5 Remaining tweaks for translator tests on 2.7
  • Loading branch information
aitboudad committed Apr 25, 2015
2 parents 4cc4f07 + c0300f5 commit 0cd300f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -105,7 +104,7 @@ public function testTransWithCachingWithInvalidLocale()
$translator->trans('foo');
}

public function testLoadRessourcesWithCaching()
public function testLoadResourcesWithCaching()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$resourceFiles = array(
Expand Down Expand Up @@ -133,7 +132,7 @@ public function testLoadRessourcesWithCaching()
$this->assertEquals('folder', $translator->trans('folder'));
}

public function testLoadRessourcesWithoutCaching()
public function testLoadResourcesWithoutCaching()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$resourceFiles = array(
Expand Down Expand Up @@ -271,17 +270,20 @@ public function testWarmup()
__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')
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Expand Up @@ -150,6 +150,32 @@ public function testCatalogueIsReloadedWhenResourcesAreNoLongerFresh()
$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
*/
Expand Down

0 comments on commit 0cd300f

Please sign in to comment.