Skip to content

Commit

Permalink
Added more tests for extract command (#134)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nyholm authored Aug 20, 2017
1 parent 4e8bc91 commit f46df85
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Catalogue/CatalogueCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
foreach ($domains as $domain) {
$result[$domain]['new'] = 0;
$result[$domain]['obsolete'] = 0;
$result[$domain]['approved'] = 0;

foreach ($catalogue->all($domain) as $key => $text) {
$metadata = new Metadata($catalogue->getMetadata($key, $domain));
Expand All @@ -85,6 +86,7 @@ public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
// Sum the number of new and obsolete messages.
$result['_total']['new'] = 0;
$result['_total']['obsolete'] = 0;
$result['_total']['approved'] = 0;
foreach ($domains as $domain) {
$result['_total']['new'] += $result[$domain]['new'];
$result['_total']['obsolete'] += $result[$domain]['obsolete'];
Expand Down
1 change: 1 addition & 0 deletions Catalogue/CatalogueWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function writeCatalogues(Configuration $config, array $catalogues)
[
'path' => $config->getOutputDir(),
'default_locale' => $this->defaultLocale,
'xliff_version' => $config->getXliffVersion(),
]
);
}
Expand Down
8 changes: 7 additions & 1 deletion Catalogue/Operation/ReplaceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ protected function processDomain($domain)
}

foreach ($sourceMessages as $id => $message) {
$this->messages[$domain]['all'][$id] = $message;
if (!empty($message)) {
$this->messages[$domain]['all'][$id] = $message;
}

if (!$this->target->has($id, $domain)) {
$this->messages[$domain]['new'][$id] = $message;

// Make sure to add it to the source if even if empty($message)
$this->messages[$domain]['all'][$id] = $message;
}
}

Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ private function configsNode(ArrayNodeDefinition $root)
->prototype('scalar')->end()
->end()
->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
->variableNode('local_file_storage_options')
->info('Options passed to the local file storage\'s dumper.')
->defaultValue([])
Expand Down
14 changes: 14 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ final class Configuration
*/
private $whitelistDomains;

/**
* @var string
*/
private $xliffVersion;

/**
* @param array $data
*/
Expand All @@ -103,6 +108,7 @@ public function __construct(array $data)
$this->outputFormat = $data['output_format'];
$this->blacklistDomains = $data['blacklist_domains'];
$this->whitelistDomains = $data['whitelist_domains'];
$this->xliffVersion = $data['xliff_version'];
}

/**
Expand Down Expand Up @@ -203,4 +209,12 @@ public function getPathsToTranslationFiles()
{
return array_merge($this->externalTranslationsDirs, [$this->getOutputDir()]);
}

/**
* @return string
*/
public function getXliffVersion()
{
return $this->xliffVersion;
}
}
107 changes: 107 additions & 0 deletions Tests/Functional/Command/ExtractCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Tests\Functional\Command;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Translation\Bundle\Command\ExtractCommand;
use Translation\Bundle\Model\Metadata;
use Translation\Bundle\Tests\Functional\BaseTestCase;

class ExtractCommandTest extends BaseTestCase
{
protected function setUp()
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');

file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
<file id="messages.en_US">
<unit id="xx1">
<segment>
<source>translated.heading</source>
<target>My translated heading</target>
</segment>
</unit>
<unit id="xx2">
<segment>
<source>translated.paragraph0</source>
<target>My translated paragraph0</target>
</segment>
</unit>
<unit id="xx3">
<notes>
<note category="file-source" priority="1">foobar.html.twig:9</note>
</notes>
<segment>
<source>translated.paragraph1</source>
<target>My translated paragraph1</target>
</segment>
</unit>
<unit id="xx4">
<segment>
<source>not.in.source</source>
<target>This is not in the source code</target>
</segment>
</unit>
</file>
</xliff>

XML
);
}

public function testExecute()
{
$this->bootKernel();
$application = new Application($this->kernel);

$application->add(new ExtractCommand());

$command = $application->find('translation:extract');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'configuration' => 'app',
'locale' => 'sv',
]);

// the output of the command in the console
$output = $commandTester->getDisplay();

// Make sure we have 4 new messages
$this->assertRegExp('|New messages +4|s', $output);
$this->assertRegExp('|Total defined messages +8|s', $output);

$container = $this->getContainer();
$config = $container->get('php_translation.configuration_manager')->getConfiguration('app');
$catalogues = $container->get('php_translation.catalogue_fetcher')->getCatalogues($config, ['sv']);

$catalogue = $catalogues[0];
$this->assertEquals('My translated heading', $catalogue->get('translated.heading'), 'Translated strings MUST NOT disappear.');

// Test meta, source-location
$meta = new Metadata($catalogue->getMetadata('translated.paragraph1'));
$this->assertFalse($meta->getState() === 'new');
foreach ($meta->getSourceLocations() as $sourceLocation) {
$this->assertNotEquals('foobar.html.twig', $sourceLocation['path']);
}

$meta = new Metadata($catalogue->getMetadata('not.in.source'));
$this->assertTrue($meta->getState() === 'obsolete');

$meta = new Metadata($catalogue->getMetadata('translated.title'));
$this->assertTrue($meta->getState() === 'new');
}
}
61 changes: 61 additions & 0 deletions Tests/Functional/Command/StatusCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Tests\Functional\Command;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Translation\Bundle\Command\StatusCommand;
use Translation\Bundle\Tests\Functional\BaseTestCase;

class StatusCommandTest extends BaseTestCase
{
protected function setUp()
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');
}

public function testExecute()
{
$this->bootKernel();
$application = new Application($this->kernel);

$application->add(new StatusCommand());

$command = $application->find('translation:status');
$commandTester = new CommandTester($command);
$commandTester->execute([
'command' => $command->getName(),
'configuration' => 'app',
'locale' => 'en',
'--json' => true,
]);

// the output of the command in the console
$output = $commandTester->getDisplay();
$data = json_decode($output, true);

$this->assertArrayHasKey('en', $data);
$this->assertArrayHasKey('messages', $data['en']);
$this->assertArrayHasKey('_total', $data['en']);

$total = $data['en']['_total'];
$this->assertArrayHasKey('defined', $total);
$this->assertArrayHasKey('new', $total);
$this->assertArrayHasKey('obsolete', $total);
$this->assertArrayHasKey('approved', $total);
$this->assertEquals(2, $total['defined']);
$this->assertEquals(1, $total['new']);
$this->assertEquals(0, $total['obsolete']);
$this->assertEquals(1, $total['approved']);
}
}
1 change: 1 addition & 0 deletions Tests/Functional/app/Resources/translations/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
messages.sv.xlf
*~
1 change: 1 addition & 0 deletions Tests/Functional/app/config/normal_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ translation:
app:
dirs: ["%test.root_dir%/Resources/views"]
output_dir: "%test.root_dir%/Resources/translations"
project_root: "%test.root_dir%"
1 change: 1 addition & 0 deletions Tests/Unit/Model/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static function getDefaultData()
'output_format' => 'getOutputFormat',
'blacklist_domains' => ['getBlacklistDomains'],
'whitelist_domains' => ['getWhitelistDomains'],
'xliff_version' => ['getXliffVersion'],
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"php-http/curl-client": "^1.7",
"php-http/message": "^1.6",
"php-http/message-factory": "^1.0.2",
"symfony/console": "^2.7 || ^3.0",
"symfony/twig-bundle": "^2.7 || ^3.0",
"symfony/twig-bridge": "^2.7 || ^3.0",
"symfony/asset": "^2.7 || ^3.0",
Expand Down

0 comments on commit f46df85

Please sign in to comment.