8000 [Translator]Test case refactoring and simplifications for the Translator class by mpdude · Pull Request #14291 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translator]Test case refactoring and simplifications for the Translator class #14291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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