Passed
Pull Request — master (#20)
by Christophe
03:13
created

FindMissingCommandTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 75
rs 10
wmc 3

2 Methods

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