ExportCommandTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sleepness\UberTranslationBundle\Tests\Command;
4
5
use Sleepness\UberTranslationBundle\Command\ExportCommand;
6
7
/**
8
 * Test ExportCommand executing cases
9
 *
10
 * @author Alexandr Zhulev <[email protected]>
11
 * @author Viktor Novikov <[email protected]>
12
 */
13
class ExportCommandTest extends CommandTestCase
14
{
15
    /**
16
     * @var \Symfony\Component\Console\Tester\CommandTester;
17
     */
18
    protected $commandTester;
19
20
    /**
21
     * @var \Sleepness\UberTranslationBundle\Storage\UberMemcached;
22
     */
23
    private $uberMemcached;
24
25
    /**
26
     * @var String;
27
     */
28
    protected static $exportResource;
29
30
    /**
31
     * Test command success execution
32
     */
33
    public function testSuccessExecute()
34
    {
35
        $commandTester = $this->commandTester;
36
        $commandTester->execute(
37
            array(
38
                'bundle' => 'TestBundle',
39
            )
40
        );
41
        $this->assertTrue(is_string($commandTester->getDisplay()));
42
        $this->assertFileExists(static::$exportResource . 'messages.en_XX.yml');
43
        $this->assertRegExp(
44
            '/key:\n\s+not:\n\s+blank:/',
45
            file_get_contents(static::$exportResource . 'validators.en_XX.yml')
46
        );
47
        $this->assertEquals(
48
            "\033[37;42m Translations exported successfully into TestBundle/Resources/translations/ ! \033[0m",
49
            trim($commandTester->getDisplay())
50
        );
51
    }
52
53
    /**
54
     * Test command failure
55
     *
56
     * @expectedException InvalidArgumentException
57
     */
58
    public function testException()
59
    {
60
        $commandTester = $this->commandTester;
61
        $commandTester->execute(
62
            array(
63
                'bundle' => 'NotExistedTestBundle',
64
            )
65
        );
66
        $this->setExpectedException('InvalidArgumentException');
67
    }
68
69
    /**
70
     * Set up fixtures for testing
71
     */
72
    public function setUp()
73
    {
74
        parent::setUp();
75
        $values = $this->getMessagesArray();
76
        $container = $this->getKernel()->getContainer();
77
        $this->uberMemcached = $container->get('uber.memcached');
78
        $this->uberMemcached->addItem('en_XX', $values);
79
        $bundlePath = $this->getKernel()->getBundle('TestBundle')->getPath();
80
        static::$exportResource = $bundlePath . '/Resources/translations/';
81
    }
82
83
    /**
84
     * Get messages for testing
85
     *
86
     * @return array - messages
87
     */
88
    private function getMessagesArray()
89
    {
90
        return array(
91
            'messages' => array(
92
                'key.hello' => 'value.Hello',
93
                'key.foo' => 'value.Foo',
94
            ),
95
            'validators' => array(
96
                'key.not.blank' => 'value.NotBlank',
97
                'key.max.length' => 'value.MaxLength',
98
            ),
99
        );
100
    }
101
102
    /**
103
     * Tear down fixtures after testing
104
     */
105
    public function tearDown()
106
    {
107
        $this->uberMemcached->deleteItem('en_XX');
108
    }
109
110
    /**
111
     * Tear down fixtures after test class
112
     */
113
    public static function tearDownAfterClass()
114
    {
115
        $files = array_diff(scandir(static::$exportResource . '/'), array('.','..'));
116
        foreach ($files as $file) {
117
            if (strstr($file, '_XX') !== false) {
118
                unlink(static::$exportResource . '/' . $file);
119
            }
120
        }
121
    }
122
123
    /**
124
     * Get instance of console command
125
     *
126
     * @return mixed
127
     */
128
    protected function getCommandInstance()
129
    {
130
        return new ExportCommand();
131
    }
132
133
    /**
134
     * Get command text
135
     *
136
     * @return mixed
137
     */
138
    protected function getCommand()
139
    {
140
       return 'uber:translations:export';
141
    }
142
}
143