Skip to content

Commit 234a082

Browse files
Olivier DolbeauNyholm
authored andcommitted
Ensure storage exists before using it in commands (#239)
1 parent 9bf308c commit 234a082

File tree

5 files changed

+124
-21
lines changed

5 files changed

+124
-21
lines changed

Command/DeleteObsoleteCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,10 @@
2828
*/
2929
class DeleteObsoleteCommand extends Command
3030
{
31-
use BundleTrait;
31+
use BundleTrait, StorageTrait;
3232

3333
protected static $defaultName = 'translation:delete-obsolete';
3434

35-
/**
36-
* @var StorageManager
37-
*/
38-
private $storageManager;
39-
4035
/**
4136
* @var ConfigurationManager
4237
*/
@@ -94,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9489
$this->configureBundleDirs($input, $config);
9590
$this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
9691

97-
$storage = $this->storageManager->getStorage($configName);
92+
$storage = $this->getStorage($configName);
9893
$messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
9994

10095
$messageCount = count($messages);

Command/DownloadCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,10 @@
2727
*/
2828
class DownloadCommand extends Command
2929
{
30-
use BundleTrait;
30+
use BundleTrait, StorageTrait;
3131

3232
protected static $defaultName = 'translation:download';
3333

34-
/**
35-
* @var StorageManager
36-
*/
37-
private $storageManager;
38-
3934
/**
4035
* @var ConfigurationManager
4136
*/
@@ -76,7 +71,7 @@ protected function configure()
7671
protected function execute(InputInterface $input, OutputInterface $output)
7772
{
7873
$configName = $input->getArgument('configuration');
79-
$storage = $this->storageManager->getStorage($configName);
74+
$storage = $this->getStorage($configName);
8075
$configuration = $this->configurationManager->getConfiguration($configName);
8176

8277
$this->configureBundleDirs($input, $configuration);

Command/StorageTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.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 Translation\Bundle\Command;
13+
14+
use Translation\Bundle\Service\StorageManager;
15+
16+
trait StorageTrait
17+
{
18+
/**
19+
* @var StorageManager
20+
*/
21+
private $storageManager;
22+
23+
private function getStorage($configName)
24+
{
25+
if (null === $storage = $this->storageManager->getStorage($configName)) {
26+
$availableStorages = $this->storageManager->getNames();
27+
28+
throw new \InvalidArgumentException(sprintf('Unknown storage "%s". Available storages are "%s".', $configName, implode('", "', $availableStorages)));
29+
}
30+
}
31+
}

Command/SyncCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@
2323
*/
2424
class SyncCommand extends Command
2525
{
26-
protected static $defaultName = 'translation:sync';
26+
use StorageTrait;
2727

28-
/**
29-
* @var StorageManager
30-
*/
31-
private $storageManager;
28+
protected static $defaultName = 'translation:sync';
3229

3330
/**
3431
* @param StorageManager $storageManager
@@ -66,7 +63,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6663
return;
6764
}
6865

69-
$configName = $input->getArgument('configuration');
70-
$this->storageManager->getStorage($configName)->sync($direction);
66+
$this->getStorage($input->getArgument('configuration'))->sync($direction);
7167
}
7268
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.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 Translation\Bundle\Tests\Functional\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Component\Console\Tester\CommandTester;
16+
use Translation\Bundle\Tests\Functional\BaseTestCase;
17+
18+
class SyncCommandTest extends BaseTestCase
19+
{
20+
protected function setUp()
21+
{
22+
parent::setUp();
23+
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');
24+
25+
file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
26+
<?xml version="1.0" encoding="utf-8"?>
27+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
28+
<file id="messages.en_US">
29+
<unit id="xx1">
30+
<segment>
31+
<source>translated.heading</source>
32+
<target>My translated heading</target>
33+
</segment>
34+
</unit>
35+
<unit id="xx2">
36+
<segment>
37+
<source>translated.paragraph0</source>
38+
<target>My translated paragraph0</target>
39+
</segment>
40+
</unit>
41+
<unit id="xx3">
42+
<notes>
43+
<note category="file-source" priority="1">foobar.html.twig:9</note>
44+
</notes>
45+
<segment>
46+
<source>translated.paragraph1</source>
47+
<target>My translated paragraph1</target>
48+
</segment>
49+
</unit>
50+
<unit id="xx4">
51+
<segment>
52+
<source>not.in.source</source>
53+
<target>This is not in the source code</target>
54+
</segment>
55+
</unit>
56+
</file>
57+
</xliff>
58+
59+
XML
60+
);
61+
}
62+
63+
public function testExecute()
64+
{
65+
$this->bootKernel();
66+
$application = new Application($this->kernel);
67+
68+
$container = $this->getContainer();
69+
$application->add($container->get('php_translator.console.sync'));
70+
71+
$command = $application->find('translation:sync');
72+
$commandTester = new CommandTester($command);
73+
74+
try {
75+
$commandTester->execute([
76+
'command' => $command->getName(),
77+
'configuration' => 'fail',
78+
]);
79+
80+
$this->fail('The command should fail when called with an unknown configuration key.');
81+
} catch (\InvalidArgumentException $e) {
82+
$this->assertRegExp('|Unknown storage "fail"\.|s', $e->getMessage());
83+
$this->assertRegExp('|Available storages are "app"\.|s', $e->getMessage());
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)