8000 minor #14291 [Translator]Test case refactoring and simplifications fo… · symfony/symfony@0cd300f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cd300f

Browse files
committed
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 [b5da2ae](b5da2ae#diff-3126bfacb25e1e304b8cc720e8527a19) Commits ------- c0300f5 Remaining tweaks for translator tests on 2.7
2 parents 4cc4f07 + c0300f5 commit 0cd300f

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Translation;
1313

1414
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
15-
use Symfony\Component\Translation\Loader\ArrayLoader;
1615
use Symfony\Component\Translation\MessageCatalogue;
1716
use Symfony\Component\Filesystem\Filesystem;
1817
use Symfony\Component\Translation\MessageSelector;
@@ -105,7 +104,7 @@ public function testTransWithCachingWithInvalidLocale()
105104
$translator->trans('foo');
106105
}
107106

108-
public function testLoadRessourcesWithCaching()
107+
public function testLoadResourcesWithCaching()
109108
{
110109
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
111110
$resourceFiles = array(
@@ -133,7 +132,7 @@ public function testLoadRessourcesWithCaching()
133132
$this->assertEquals('folder', $translator->trans('folder'));
134133
}
135134

136-
public function testLoadRessourcesWithoutCaching()
135+
public function testLoadResourcesWithoutCaching()
137136
{
138137
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
139138
$resourceFiles = array(
@@ -271,17 +270,20 @@ public function testWarmup()
271270
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
272271
),
273272
);
274-
$catalogueHash = sha1(serialize(array(
275-
'resources' => array(),
276-
'fallback_locales' => array(),
277-
)));
278273

279274
// prime the cache
280275
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles), 'yml');
281-
282-
$this->assertFalse(file_exists($this->tmpDir.'/catalogue.fr.'.$catalogueHash.'.php'));
276+
$translator->setLocale('fr');
283277
$translator->warmup($this->tmpDir);
284-
$this->assertTrue(file_exists($this->tmpDir.'/catalogue.fr.'.$catalogueHash.'.php'));
278+
279+
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
280+
$loader
281+
->expects($this->never())
282+
->method('load');
283+
284+
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles), 'yml');
285+
$translator->setLocale('fr');
286+
$this->assertEquals('répertoire', $translator->trans('folder'));
285287
}
286288

287289
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader')

src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,32 @@ public function testCatalogueIsReloadedWhenResourcesAreNoLongerFresh()
150150
$translator->trans($msgid);
151151
}
152152

153+
/**
154+
* @dataProvider runForDebugAndProduction
155+
*/
156+
public function testDifferentTranslatorsForSameLocaleDoNotInterfere($debug)
157+
{
158+
$locale = 'any_locale';
159+
$format = 'some_format';
160+
$msgid = 'test';
161+
162+
// Create a Translator and prime its cache
163+
$translator = new Translator($locale, null, $this->tmpDir, $debug);
164+
$translator->addLoader($format, new ArrayLoader());
165+
$translator->addResource($format, array($msgid => 'FAIL'), $locale);
166+
$translator->trans($msgid);
167+
168+
/*
169+
* Create another Translator with the same locale but a different resource.
170+
* It should not use the first translator's cache but return the value from its own resource.
171+
*/
172+
$translator = new Translator($locale, null, $this->tmpDir, $debug);
173+
$translator->addLoader($format, new ArrayLoader());
174+
$translator->addResource($format, array($msgid => 'OK'), $locale);
175+
176+
$this->assertEquals('OK', $translator->trans($msgid), '-> different translators for the same domain interfere in '.($debug ? 'debug' : 'production'));
177+
}
178+
153179
/**
154180
* @dataProvider runForDebugAndProduction
155181
*/

0 commit comments

Comments
 (0)
0