8000 minor #15673 [framework-bundle] Add Test for TranslationUpdateCommand… · symfony/symfony@7211133 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7211133

Browse files
committed
minor #15673 [framework-bundle] Add Test for TranslationUpdateCommand (zerustech)
This PR was merged into the 2.3 branch. Discussion ---------- [framework-bundle] Add Test for TranslationUpdateCommand Added the test script as per the discussion in PR #15562 | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 232f6fd [framework-bundle] Add Test for TranslationUpdateCommand
2 parents 71ef86e + 232f6fd commit 7211133

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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\TranslationUpdateCommand;
17+
use Symfony\Component\Filesystem\Filesystem;
18+
use Symfony\Component\DependencyInjection;
19+
use Symfony\Component\HttpKernel;
20+
21+
class TranslationUpdateCommandTest extends \PHPUnit_Framework_TestCase
22+
{
23+
private $fs;
24+
private $translationDir;
25+
26+
public function testDumpMessagesAndClean()
27+
{
28+
$tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
29+
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true));
30+
$this->assertRegExp('/foo/', $tester->getDisplay());
31+
}
32+
33+
protected function setUp()
34+
{
35+
$this->fs = new Filesystem();
36+
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
37+
$this->fs->mkdir($this->translationDir.'/Resources/translations');
38+
$this->fs->mkdir($this->translationDir.'/Resources/views');
39+
}
40+
41+
protected function tearDown()
42+
{
43+
$this->fs->remove($this->translationDir);
44+
}
45+
46+
/**
47+
* @return CommandTester
48+
*/
49+
private function createCommandTester(DependencyInjection\ContainerInterface $container)
50+
{
51+
$command = new TranslationUpdateCommand();
52+
$command->setContainer($container);
53+
54+
$application = new Application();
55+
$application->add($command);
56+
57+
return new CommandTester($application->find('translation:update'));
58+
}
59+
60+
private function getContainer($extractedMessages = array(), $loadedMessages = array(), HttpKernel\KernelInterface $kernel = null)
61+
{
62+
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
63+
->disableOriginalConstructor()
64+
->getMock();
65+
66+
$translator
67+
->expects($this->any())
68+
->method('getFallbackLocales')
69+
->will($this->returnValue(array('en')));
70+
71+
$extractor = $this->getMock('Symfony\Component\Translation\Extractor\ExtractorInterface');
72+
$extractor
73+
->expects($this->any())
74+
->method('extract')
75+
->will(
76+
$this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
77+
$catalogue->add($extractedMessages);
78+
})
79+
);
80+
81+
$loader = $this->getMock('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader');
82+
$loader
83+
->expects($this->any())
84+
->method('loadMessages')
85+
->will(
86+
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
87+
$catalogue->add($loadedMessages);
88+
})
89+
);
90+
91+
$writer = $this->getMock('Symfony\Component\Translation\Writer\TranslationWriter');
92+
$writer
93+
->expects($this->any())
94+
->method('getFormats')
95+
->will(
96+
$this->returnValue(array('xlf', 'yml'))
97+
);
98+
99+
if (null === $kernel) {
100+
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
101+
$kernel
102+
->expects($this->any())
103+
->method('getBundle')
104+
->will($this->returnValueMap(array(
105+
array('foo', true, $this->getBundle($this->translationDir)),
106+
array('test', true, $this->getBundle('test')),
107+
)));
108+
}
109+
110+
$kernel
111+
->expects($this->any())
112+
->method('getRootDir')
113+
->will($this->returnValue($this->translationDir));
114+
115+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
116+
$container
117+
->expects($this->any())
118+
->method('get')
119+
->will($this->returnValueMap(array(
120+
array('translation.extractor', 1, $extractor),
121+
array('translation.loader', 1, $loader),
122+
array('translation.writer', 1, $writer),
123+
array('translator', 1, $translator),
124+
array('kernel', 1, $kernel),
125+
)));
126+
127+
return $container;
128+
}
129+
130+
private function getBundle($path)
131+
{
132+
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
133+
$bundle
134+
->expects($this->any())
135+
->method('getPath')
136+
->will($this->returnValue($path))
137+
;
138+
139+
return $bundle;
140+
}
141+
}

0 commit comments

Comments
 (0)
0