Skip to content

Commit

Permalink
test that the translator service definition exists before adding meth…
Browse files Browse the repository at this point in the history
…od calls to it
  • Loading branch information
Christian Flothmann committed Jan 23, 2014
1 parent e66d5b4 commit 290dd4a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
24 changes: 13 additions & 11 deletions DependencyInjection/Compiler/AddResourcePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ public function process(ContainerBuilder $container)
$resources = $container->getParameter('asm_translation_loader.resources');
$translator = $container->findDefinition('translator.default');

foreach ($resources as $locale => $domains) {
foreach ($domains as $domain) {
$translator->addMethodCall(
'addResource',
array(
'db',
new Reference('asm_translation_loader.translation_manager_resource'),
$locale,
$domain,
)
);
if (null !== $translator) {
foreach ($resources as $locale => $domains) {
foreach ($domains as $domain) {
$translator->addMethodCall(
'addResource',
array(
'db',
new Reference('asm_translation_loader.translation_manager_resource'),
$locale,
$domain,
)
);
}
}
}
}
Expand Down
76 changes: 57 additions & 19 deletions Tests/DependencyInjection/Compiler/AddResourcePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class AddResourcePassTest extends \PHPUnit_Framework_TestCase
*/
private $compilerPass;

/**
* @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private $container;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -41,72 +36,115 @@ protected function setUp()

public function testWithoutLocales()
{
$container = $this->createContainer();
$this->translator
->expects($this->never())
->method('addMethodCall');

$this->registerConfig(array());
$this->compilerPass->process($this->container);
$this->registerConfig($container, array());
$this->compilerPass->process($container);
}

public function testLocaleWithoutDomain()
{
$container = $this->createContainer();
$this->translator
->expects($this->once())
->method('addMethodCall');

$this->registerConfig(array('en' => array(null)));
$this->compilerPass->process($this->container);
$this->registerConfig($container, array('en' => array(null)));
$this->compilerPass->process($container);
}

public function testLocaleWithOneDomain()
{
$container = $this->createContainer();
$this->translator
->expects($this->once())
->method('addMethodCall');

$this->registerConfig(array('en' => array('foo')));
$this->compilerPass->process($this->container);
$this->registerConfig($container, array('en' => array('foo')));
$this->compilerPass->process($container);
}

public function testLocaleWithMultipleDomains()
{
$container = $this->createContainer();
$this->translator
->expects($this->exactly(2))
->method('addMethodCall');

$this->registerConfig(array('en' => array('foo', 'bar')));
$this->compilerPass->process($this->container);
$this->registerConfig($container, array('en' => array('foo', 'bar')));
$this->compilerPass->process($container);
}

public function testMultipleLocalesWithMultipleDomains()
{
$container = $this->createContainer();
$this->translator
->expects($this->exactly(6))
->method('addMethodCall');

$this->registerConfig(array(
$this->registerConfig($container, array(
'en' => array('foo', 'bar'),
'de' => array('baz'),
'fr' => array('x', 'y', 'z')
));
$this->compilerPass->process($container);
}

public function testWithoutTranslator()
{
$container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
$container
->expects($this->once())
->method('findDefinition')
->with('translator.default')
->will($this->returnValue(null));

$this->registerConfig($container, array(
'en' => array('foo', 'bar'),
'de' => array('baz'),
'fr' => array('x', 'y', 'z')
));
$this->compilerPass->process($this->container);
$this->compilerPass->process($container);
}

public function testWithoutTranslatorAndWithoutLocales()
{
$container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
$container
->expects($this->once())
->method('findDefinition')
->with('translator.default')
->will($this->returnValue(null));
$this->translator
->expects($this->never())
->method('addMethodCall');

$this->registerConfig($container, array());
$this->compilerPass->process($container);
}

/**
* @return \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private function createContainer()
{
$this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
$container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
$this->translator = $this->getMock('\Symfony\Component\DependencyInjection\Definition');
$this->container
$container
->expects($this->any())
->method('findDefinition')
->with('translator.default')
->will($this->returnValue($this->translator));

return $container;
}

private function registerConfig(array $config)
private function registerConfig($container, array $config)
{
$this->container
$container
->expects($this->any())
->method('getParameter')
->will($this->returnValue($config));
Expand Down

0 comments on commit 290dd4a

Please sign in to comment.