Skip to content

Commit c26587f

Browse files
committed
[Framework][Translation] added test for debug command.
1 parent afdf5c4 commit c26587f

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
13+
14+
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Tester\CommandTester;
16+
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
17+
use Symfony\Component\Filesystem\Filesystem;
18+
19+
class TranslationDebugCommandTest extends \PHPUnit_Framework_TestCase
20+
{
21+
private $fs;
22+
private $translationDir;
23+
24+
public function testDebugMissingMessages()
25+
{
26+
$tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
27+
$tester->execute(array('locale' => 'en', 'bundle' => 'foo'));
28+
29+
$this->assertRegExp('/x (\s|\|)+foo/', $tester->getDisplay(), 'Display x in case of missing message');
30+
}
31+
32+
public function testDebugUnusedMessages()
33+
{
34+
$tester = $this->createCommandTester($this->getContainer(array(), array('foo' => 'foo')));
35+
$tester->execute(array('locale' => 'en', 'bundle' => 'foo'));
36+
37+
$this->assertRegExp('/o (\s|\|)+foo/', $tester->getDisplay(), 'Display o in case of unused message');
38+
}
39+
40+
public function testDebugFallbackMessages()
41+
{
42+
$tester = $this->createCommandTester($this->getContainer(array(), array('foo' => 'foo')));
43+
$tester->execute(array('locale' => 'fr', 'bundle' => 'foo'));
44+
45+
$this->assertRegExp('/= (\s|\|)+foo/', $tester->getDisplay(), 'Display = in case of fallback message');
46+
}
47+
48+
public function testNoDefinedMessages()
49+
{
50+
$tester = $this->createCommandTester($this->getContainer());
51+
$tester->execute(array('locale' => 'fr', 'bundle' => 'test'));
52+
53+
$this->assertRegExp('/^No defined or extracted messages for locale "fr"/', $tester->getDisplay());
54+
}
55+
56+
protected function setUp()
57+
{
58+
$this->fs = new Filesystem();
59+
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation');
60+
$this->fs->mkdir($this->translationDir.'/Resources/translations');
61+
}
62+
63+
protected function tearDown()
64+
{
65+
$this->fs->remove($this->translationDir);
66+
}
67+
68+
/**
69+
* @return CommandTester
70+
*/
71+
private function createCommandTester($container)
72+
{
73+
$command = new TranslationDebugCommand();
74+
$command->setContainer($container);
75+
76+
$application = new Application();
77+
$application->add($command);
78+
79+
return new CommandTester($application->find('debug:translation'));
80+
}
81+
82+
private function getContainer($extractedMessages = array(), $loadedMessages = array())
83+
{
84+
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
85+
->disableOriginalConstructor()
86+
->getMock();
87+
88+
$translator
89+
->expects($this->any())
90+
->method('getFallbackLocales')
91+
->will($this->returnValue(array('en')));
92+
93+
$extractor = $this->getMock('Symfony\Component\Translation\Extractor\ExtractorInterface');
94+
$extractor
95+
->expects($this->any())
96+
->method('extract')
97+
->will(
98+
$this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
99+
$catalogue->add($extractedMessages);
100+
})
101+
);
102+
103+
$loader = $this->getMock('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader');
104+
$loader
105+
->expects($this->any())
106+
->method('loadMessages')
107+
->will(
108+
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
109+
$catalogue->add($loadedMessages);
110+
})
111+
);
112+
113+
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
114+
$kernel
115+
->expects($this->any())
116+
->method('getBundle')
117+
->will($this->returnValueMap(array(
118+
array('foo', true, $this->getBundle($this->translationDir)),
119+
array('test', true, $this->getBundle('test')),
120+
)));
121+
122+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
123+
$container
124+
->expects($this->any())
125+
->method('get')
126+
->will($this->returnValueMap(array(
127+
array('translation.extractor', 1, $extractor),
128+
array('translation.loader', 1, $loader),
129+
array('translator', 1, $translator),
130+
array('kernel', 1, $kernel),
131+
)));
132+
133+
return $container;
134+
}
135+
136+
private function getBundle($path)
137+
{
138+
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
139+
$bundle
140+
->expects($this->any())
141+
->method('getPath')
142+
->will($this->returnValue($path))
143+
;
144+
145+
return $bundle;
146+
}
147+
}

0 commit comments

Comments
 (0)