Issues (83)

Tests/Functional/Command/ExtractCommandTest.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
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 Symfony\Component\HttpKernel\Kernel;
17
use Translation\Bundle\Catalogue\CatalogueFetcher;
18
use Translation\Bundle\Command\ExtractCommand;
19
use Translation\Bundle\Model\Metadata;
20
use Translation\Bundle\Service\ConfigurationManager;
21
use Translation\Bundle\Tests\Functional\BaseTestCase;
22
23
class ExtractCommandTest extends BaseTestCase
24
{
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
        $this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
29
30
        \file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
31
<?xml version="1.0" encoding="utf-8"?>
32
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
33
    <file id="messages.en_US">
34
    <unit id="xx1">
35
      <segment>
36
        <source>translated.heading</source>
37
        <target>My translated heading</target>
38
      </segment>
39
    </unit>
40
    <unit id="xx2">
41
      <segment>
42
        <source>translated.paragraph0</source>
43
        <target>My translated paragraph0</target>
44
      </segment>
45
    </unit>
46
    <unit id="xx3">
47
      <notes>
48
        <note category="file-source" priority="1">foobar.html.twig:9</note>
49
      </notes>
50
      <segment>
51
        <source>translated.paragraph1</source>
52
        <target>My translated paragraph1</target>
53
      </segment>
54
    </unit>
55
    <unit id="xx4">
56
      <segment>
57
        <source>not.in.source</source>
58
        <target>This is not in the source code</target>
59
      </segment>
60
    </unit>
61
    </file>
62
</xliff>
63
64
XML
65
        );
66
    }
67
68
    public function testExecute(): void
69
    {
70
        $this->bootKernel();
71
        $application = new Application($this->kernel);
72
73
        $container = $this->getContainer();
74
        $application->add($container->get(ExtractCommand::class));
0 ignored issues
show
It seems like $container->get(Translat...\ExtractCommand::class) can also be of type null; however, parameter $command of Symfony\Bundle\Framework...sole\Application::add() does only seem to accept Symfony\Component\Console\Command\Command, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $application->add(/** @scrutinizer ignore-type */ $container->get(ExtractCommand::class));
Loading history...
75
76
        // transchoice tag have been definively removed in sf ^5.0
77
        // Remove this condition & views_with_transchoice + associated config once sf ^5.0 is the minimum supported version.
78
        if (\version_compare(Kernel::VERSION, 5.0, '<')) {
79
            $configuration = 'app_with_transchoice';
80
        } else {
81
            $configuration = 'app';
82
        }
83
84
        $command = $application->find('translation:extract');
85
        $commandTester = new CommandTester($command);
86
        $commandTester->execute([
87
            'command' => $command->getName(),
88
            'configuration' => $configuration,
89
            'locale' => 'sv',
90
        ]);
91
92
        // the output of the command in the console
93
        $output = $commandTester->getDisplay();
94
95
        // Make sure we have 4 new messages
96
        $this->assertRegExp('|New messages +4|s', $output);
97
        $this->assertRegExp('|Total defined messages +8|s', $output);
98
99
        $container = $this->getContainer();
100
        $config = $container->get(ConfigurationManager::class)->getConfiguration('app');
101
        $catalogues = $container->get(CatalogueFetcher::class)->getCatalogues($config, ['sv']);
102
103
        $catalogue = $catalogues[0];
104
        $this->assertEquals('My translated heading', $catalogue->get('translated.heading'), 'Translated strings MUST NOT disappear.');
105
106
        // Test meta, source-location
107
        $meta = new Metadata($catalogue->getMetadata('translated.paragraph1'));
108
        $this->assertFalse('new' === $meta->getState());
109
        foreach ($meta->getSourceLocations() as $sourceLocation) {
110
            $this->assertNotEquals('foobar.html.twig', $sourceLocation['path']);
111
        }
112
113
        $meta = new Metadata($catalogue->getMetadata('not.in.source'));
114
        $this->assertTrue('obsolete' === $meta->getState());
115
116
        $meta = new Metadata($catalogue->getMetadata('translated.title'));
117
        $this->assertTrue('new' === $meta->getState());
118
    }
119
}
120