Skip to content

Commit

Permalink
Add tests and more assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy Derussé committed Sep 2, 2014
1 parent 101a3b7 commit 9e1bc22
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public function testTransWithCaching()
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
}

public function testTransWithCachingWithInvalidLocale()
{
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
$translator->setLocale('invalid locale');

$this->setExpectedException('\InvalidArgumentException');
$translator->trans('foo');
}

public function testGetLocale()
{
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
Expand Down Expand Up @@ -131,6 +141,49 @@ public function testGetLocale()
$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());
}


protected function getCatalogue($locale, $messages)
{
$catalogue = new MessageCatalogue($locale);
Expand Down Expand Up @@ -211,9 +264,9 @@ protected function getContainer($loader)
return $container;
}

public function getTranslator($loader, $options = array())
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
{
$translator = new Translator(
$translator = new $translatorClass(
$this->getContainer($loader),
new MessageSelector(),
array('loader' => array('loader')),
Expand All @@ -231,3 +284,14 @@ public function getTranslator($loader, $options = array())
return $translator;
}
}

class TranslatorWithInvalidLocale extends Translator
{
/**
* {@inheritdoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
public function getLocale()
{
if (null === $this->locale && $this->container->isScopeActive('request') && $this->container->has('request')) {
$this->setLocale($this->container->get('request')->getLocale());
try {
$this->setLocale($this->container->get('request')->getLocale());
} catch (\InvalidArgumentException $e) {
$this->setLocale($this->container->get('request')->getDefaultLocale());
}
}

return $this->locale;
Expand All @@ -89,6 +93,8 @@ protected function loadCatalogue($locale)
return parent::loadCatalogue($locale);
}

$this->assertValidLocale($locale);

$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
if (!$cache->isFresh()) {
$this->initialize();
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,16 @@ public function setDefaultLocale($locale)
}
}

/**
* Get the default locale.
*
* @return string
*/
public function getDefaultLocale()
{
return $this->defaultLocale;
}

/**
* Sets the locale.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ protected function computeFallbackLocales($locale)
*
* @throws \InvalidArgumentException If the locale contains invalid characters
*/
private function assertValidLocale($locale)
protected function assertValidLocale($locale)
{
if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
Expand Down

0 comments on commit 9e1bc22

Please sign in to comment.