Skip to content

Commit f46df85

Browse files
authored
Added more tests for extract command (#134)
* Adding test for extract command * Added more tests * Improve gitignore for test files * Applied changes from StyleCI * Added test for Status command * Allow user to specify xliff_version * Applied changes from StyleCI * Bugfix * Make sure to boot kernel * Removed unused import
1 parent 4e8bc91 commit f46df85

File tree

11 files changed

+198
-2
lines changed

11 files changed

+198
-2
lines changed

Catalogue/CatalogueCounter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
6464
foreach ($domains as $domain) {
6565
$result[$domain]['new'] = 0;
6666
$result[$domain]['obsolete'] = 0;
67+
$result[$domain]['approved'] = 0;
6768

6869
foreach ($catalogue->all($domain) as $key => $text) {
6970
$metadata = new Metadata($catalogue->getMetadata($key, $domain));
@@ -85,6 +86,7 @@ public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
8586
// Sum the number of new and obsolete messages.
8687
$result['_total']['new'] = 0;
8788
$result['_total']['obsolete'] = 0;
89+
$result['_total']['approved'] = 0;
8890
foreach ($domains as $domain) {
8991
$result['_total']['new'] += $result[$domain]['new'];
9092
$result['_total']['obsolete'] += $result[$domain]['obsolete'];

Catalogue/CatalogueWriter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function writeCatalogues(Configuration $config, array $catalogues)
5959
[
6060
'path' => $config->getOutputDir(),
6161
'default_locale' => $this->defaultLocale,
62+
'xliff_version' => $config->getXliffVersion(),
6263
]
6364
);
6465
}

Catalogue/Operation/ReplaceOperation.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ protected function processDomain($domain)
4848
}
4949

5050
foreach ($sourceMessages as $id => $message) {
51-
$this->messages[$domain]['all'][$id] = $message;
51+
if (!empty($message)) {
52+
$this->messages[$domain]['all'][$id] = $message;
53+
}
54+
5255
if (!$this->target->has($id, $domain)) {
5356
$this->messages[$domain]['new'][$id] = $message;
57+
58+
// Make sure to add it to the source if even if empty($message)
59+
$this->messages[$domain]['all'][$id] = $message;
5460
}
5561
}
5662

DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ private function configsNode(ArrayNodeDefinition $root)
158158
->prototype('scalar')->end()
159159
->end()
160160
->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
161-
->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
161+
->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
162+
->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
162163
->variableNode('local_file_storage_options')
163164
->info('Options passed to the local file storage\'s dumper.')
164165
->defaultValue([])

Model/Configuration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ final class Configuration
8787
*/
8888
private $whitelistDomains;
8989

90+
/**
91+
* @var string
92+
*/
93+
private $xliffVersion;
94+
9095
/**
9196
* @param array $data
9297
*/
@@ -103,6 +108,7 @@ public function __construct(array $data)
103108
$this->outputFormat = $data['output_format'];
104109
$this->blacklistDomains = $data['blacklist_domains'];
105110
$this->whitelistDomains = $data['whitelist_domains'];
111+
$this->xliffVersion = $data['xliff_version'];
106112
}
107113

108114
/**
@@ -203,4 +209,12 @@ public function getPathsToTranslationFiles()
203209
{
204210
return array_merge($this->externalTranslationsDirs, [$this->getOutputDir()]);
205211
}
212+
213+
/**
214+
* @return string
215+
*/
216+
public function getXliffVersion()
217+
{
218+
return $this->xliffVersion;
219+
}
206220
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\Command\ExtractCommand;
17+
use Translation\Bundle\Model\Metadata;
18+
use Translation\Bundle\Tests\Functional\BaseTestCase;
19+
20+
class ExtractCommandTest extends BaseTestCase
21+
{
22+
protected function setUp()
23+
{
24+
parent::setUp();
25+
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');
26+
27+
file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
28+
<?xml version="1.0" encoding="utf-8"?>
29+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
30+
<file id="messages.en_US">
31+
<unit id="xx1">
32+
<segment>
33+
<source>translated.heading</source>
34+
<target>My translated heading</target>
35+
</segment>
36+
</unit>
37+
<unit id="xx2">
38+
<segment>
39+
<source>translated.paragraph0</source>
40+
<target>My translated paragraph0</target>
41+
</segment>
42+
</unit>
43+
<unit id="xx3">
44+
<notes>
45+
<note category="file-source" priority="1">foobar.html.twig:9</note>
46+
</notes>
47+
<segment>
48+
<source>translated.paragraph1</source>
49+
<target>My translated paragraph1</target>
50+
</segment>
51+
</unit>
52+
<unit id="xx4">
53+
<segment>
54+
<source>not.in.source</source>
55+
<target>This is not in the source code</target>
56+
</segment>
57+
</unit>
58+
</file>
59+
</xliff>
60+
61+
XML
62+
);
63+
}
64+
65+
public function testExecute()
66+
{
67+
$this->bootKernel();
68+
$application = new Application($this->kernel);
69+
70+
$application->add(new ExtractCommand());
71+
72+
$command = $application->find('translation:extract');
73+
$commandTester = new CommandTester($command);
74+
$commandTester->execute([
75+
'command' => $command->getName(),
76+
'configuration' => 'app',
77+
'locale' => 'sv',
78+
]);
79+
80+
// the output of the command in the console
81+
$output = $commandTester->getDisplay();
82+
83+
// Make sure we have 4 new messages
84+
$this->assertRegExp('|New messages +4|s', $output);
85+
$this->assertRegExp('|Total defined messages +8|s', $output);
86+
87+
$container = $this->getContainer();
88+
$config = $container->get('php_translation.configuration_manager')->getConfiguration('app');
89+
$catalogues = $container->get('php_translation.catalogue_fetcher')->getCatalogues($config, ['sv']);
90+
91+
$catalogue = $catalogues[0];
92+
$this->assertEquals('My translated heading', $catalogue->get('translated.heading'), 'Translated strings MUST NOT disappear.');
93+
94+
// Test meta, source-location
95+
$meta = new Metadata($catalogue->getMetadata('translated.paragraph1'));
96+
$this->assertFalse($meta->getState() === 'new');
97+
foreach ($meta->getSourceLocations() as $sourceLocation) {
98+
$this->assertNotEquals('foobar.html.twig', $sourceLocation['path']);
99+
}
100+
101+
$meta = new Metadata($catalogue->getMetadata('not.in.source'));
102+
$this->assertTrue($meta->getState() === 'obsolete');
103+
104+
$meta = new Metadata($catalogue->getMetadata('translated.title'));
105+
$this->assertTrue($meta->getState() === 'new');
106+
}
107+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Command\StatusCommand;
17+
use Translation\Bundle\Tests\Functional\BaseTestCase;
18+
19+
class StatusCommandTest extends BaseTestCase
20+
{
21+
protected function setUp()
22+
{
23+
parent::setUp();
24+
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');
25+
}
26+
27+
public function testExecute()
28+
{
29+
$this->bootKernel();
30+
$application = new Application($this->kernel);
31+
32+
$application->add(new StatusCommand());
33+
34+
$command = $application->find('translation:status');
35+
$commandTester = new CommandTester($command);
36+
$commandTester->execute([
37+
'command' => $command->getName(),
38+
'configuration' => 'app',
39+
'locale' => 'en',
40+
'--json' => true,
41+
]);
42+
43+
// the output of the command in the console
44+
$output = $commandTester->getDisplay();
45+
$data = json_decode($output, true);
46+
47+
$this->assertArrayHasKey('en', $data);
48+
$this->assertArrayHasKey('messages', $data['en']);
49+
$this->assertArrayHasKey('_total', $data['en']);
50+
51+
$total = $data['en']['_total'];
52+
$this->assertArrayHasKey('defined', $total);
53+
$this->assertArrayHasKey('new', $total);
54+
$this->assertArrayHasKey('obsolete', $total);
55+
$this->assertArrayHasKey('approved', $total);
56+
$this->assertEquals(2, $total['defined']);
57+
$this->assertEquals(1, $total['new']);
58+
$this->assertEquals(0, $total['obsolete']);
59+
$this->assertEquals(1, $total['approved']);
60+
}
61+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
messages.sv.xlf
2+
*~

Tests/Functional/app/config/normal_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ translation:
88
app:
99
dirs: ["%test.root_dir%/Resources/views"]
1010
output_dir: "%test.root_dir%/Resources/translations"
11+
project_root: "%test.root_dir%"

Tests/Unit/Model/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static function getDefaultData()
5858
'output_format' => 'getOutputFormat',
5959
'blacklist_domains' => ['getBlacklistDomains'],
6060
'whitelist_domains' => ['getWhitelistDomains'],
61+
'xliff_version' => ['getXliffVersion'],
6162
];
6263
}
6364
}

0 commit comments

Comments
 (0)