FindMissingCommandTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A provideCommandData() 0 39 1
A testExecute() 0 23 2
1
<?php
2
3
namespace Incenteev\TranslationCheckerBundle\Tests\Command;
4
5
use Incenteev\TranslationCheckerBundle\Command\FindMissingCommand;
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\PhpUnit\ProphecyTrait;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Tester\CommandTester;
11
use Symfony\Component\Translation\MessageCatalogue;
12
use Symfony\Component\Translation\TranslatorBagInterface;
13
14
class FindMissingCommandTest extends TestCase
15
{
16
    use ProphecyTrait;
17
18
    /**
19
     * @param array<string, array<string, string>> $sourceMessages
20
     * @param array<string, array<string, string>> $extractedMessages
21
     * @param string|string[] $expectedMessages
22
     *
23
     * @dataProvider provideCommandData
24
     */
25
    public function testExecute(string $locale, array $sourceMessages, array $extractedMessages, int $expectedExitCode, $expectedMessages, int $verbosity = OutputInterface::VERBOSITY_NORMAL)
26
    {
27
        $loader = $this->prophesize(TranslatorBagInterface::class);
28
        $extractor = $this->prophesize('Incenteev\TranslationCheckerBundle\Translator\Extractor\ExtractorInterface');
29
30
        $loader->getCatalogue($locale)->willReturn(new MessageCatalogue($locale, $sourceMessages));
31
32
        $extractor->extract(Argument::type('Symfony\Component\Translation\MessageCatalogue'))->will(function ($args) use ($extractedMessages) {
33
            /** @var MessageCatalogue $catalogue */
34
            $catalogue = $args[0];
35
36
            $catalogue->addCatalogue(new MessageCatalogue($catalogue->getLocale(), $extractedMessages));
37
        });
38
39
        $command = new FindMissingCommand($loader->reveal(), $extractor->reveal());
40
41
        $tester = new CommandTester($command);
42
        $exitCode = $tester->execute(array('locale' => $locale), array('decorated' => false, 'verbosity' => $verbosity));
43
44
        $this->assertEquals($expectedExitCode, $exitCode);
45
46
        foreach ((array) $expectedMessages as $message) {
47
            $this->assertStringContainsString($message, $tester->getDisplay());
48
        }
49
    }
50
51
    public function provideCommandData(): iterable
52
    {
53
        return array(
54
            'sync with en' => array(
55
                'en',
56
                array('messages' => array('foo' => 'bar')),
57
                array('messages' => array('foo' => 'baz')),
58
                0,
59
                'The en catalogue is in sync with the extracted one.',
60
            ),
61
            'extra messages' => array(
62
                'en',
63
                array('messages' => array('foo' => 'bar'), 'test' => array('hello' => 'world')),
64
                array('messages' => array('foo' => 'baz')),
65
                0,
66
                'The en catalogue is in sync with the extracted one.',
67
                OutputInterface::VERBOSITY_VERBOSE,
68
            ),
69
            'missing message' => array(
70
                'en',
71
                array('messages' => array('foo' => 'bar')),
72
                array('messages' => array('foo' => 'bar'), 'test' => array('hello' => 'world')),
73
                1,
74
                '1 messages are missing in the test domain',
75
            ),
76
            'missing message verbose' => array(
77
                'en',
78
                array('messages' => array('foo' => 'bar')),
79
                array('messages' => array('foo' => 'bar'), 'test' => array('hello' => 'world')),
80
                1,
81
                array('1 messages are missing in the test domain', '    hello'),
82
                OutputInterface::VERBOSITY_VERBOSE,
83
            ),
84
            'missing multiple domains' => array(
85
                'en',
86
                array('messages' => array('foo' => 'bar')),
87
                array('messages' => array('foo' => 'bar', 'bar' => 'baz'), 'test' => array('hello' => 'world', 'world' => 'wide')),
88
                1,
89
                array('1 messages are missing in the messages domain', '2 messages are missing in the test domain'),
90
            ),
91
        );
92
    }
93
}
94