1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Decline\TransformatBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Decline\TransformatBundle\Command\FormatCommand; |
6
|
|
|
use Decline\TransformatBundle\Tests\ContainerTestCase; |
7
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
8
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class FormatCommandTest |
12
|
|
|
* @package Decline\TransformatBundle\Tests\Command |
13
|
|
|
*/ |
14
|
|
|
class FormatCommandTest extends ContainerTestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
const EXECUTE_OUTPUT_RESULT_OK = '[OK] Done.'; |
18
|
|
|
|
19
|
|
|
const EXECUTE_OUTPUT_RESULT_FAIL = '[ERROR] No supported files could be found in the configured directory'; |
20
|
|
|
|
21
|
|
|
const EXECUTE_OUTPUT_RESULT_FAIL_TRANS_UNITS = '[ERROR] no-trans-units.de.xlf: No trans-units could be found'; |
22
|
|
|
|
23
|
|
|
const EXECUTE_OUTPUT_RESULT_FAIL_DUPLICATE_KEY = '[ERROR] duplicate-keys.de.xlf: Duplicate translation key'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Tests the execute method the FormatCommand |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function testExecute() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
list($result, $output) = $this->runCommand([FormatCommand::COMMAND_NAME]); |
31
|
|
|
|
32
|
|
|
// check result code |
33
|
|
|
$this->assertEquals(0, $result); |
34
|
|
|
|
35
|
|
|
// output must end as expected |
36
|
|
|
$this->assertStringEndsWith(self::EXECUTE_OUTPUT_RESULT_OK, trim($output->fetch())); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Tests the execute method the FormatCommand with a non-existing file |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function testExecuteWithNonExistingFile() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
list($result, $output) = $this->runCommand([FormatCommand::COMMAND_NAME, 'foo/bar/foobar.xlf']); |
45
|
|
|
|
46
|
|
|
// check result code |
47
|
|
|
$this->assertEquals(0, $result); |
48
|
|
|
|
49
|
|
|
// output must end as expected |
50
|
|
|
$this->assertContains(self::EXECUTE_OUTPUT_RESULT_FAIL, $output->fetch()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tests the execute method the FormatCommand with a file that should be ignored because of its file-ending |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function testExecuteWithIgnoredFile() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
list($result, $output) = $this->runCommand([FormatCommand::COMMAND_NAME, 'ignored.txt']); |
59
|
|
|
|
60
|
|
|
// check result code |
61
|
|
|
$this->assertEquals(0, $result); |
62
|
|
|
|
63
|
|
|
// output must end as expected |
64
|
|
|
$this->assertContains(self::EXECUTE_OUTPUT_RESULT_FAIL, $output->fetch()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tests the execute method the FormatCommand with a file that should generate an error because of missing trans-units |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function testExecuteWithoutTransUnits() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
list($result, $output) = $this->runCommand( |
73
|
|
|
[FormatCommand::COMMAND_NAME, '../translations-faulty/no-trans-units.de.xlf'] |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
// check result code |
77
|
|
|
$this->assertEquals(0, $result); |
78
|
|
|
|
79
|
|
|
// output must end as expected |
80
|
|
|
$this->assertContains(self::EXECUTE_OUTPUT_RESULT_FAIL_TRANS_UNITS, $output->fetch()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tests the execute method the FormatCommand with a file that should generate an error because of duplicate keys |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function testExecuteWithDuplicateKeys() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
list($result, $output) = $this->runCommand( |
89
|
|
|
[FormatCommand::COMMAND_NAME, '../translations-faulty/duplicate-keys.de.xlf'] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
// check result code |
93
|
|
|
$this->assertEquals(0, $result); |
94
|
|
|
|
95
|
|
|
// output must end as expected |
96
|
|
|
$this->assertContains(self::EXECUTE_OUTPUT_RESULT_FAIL_DUPLICATE_KEY, $output->fetch()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Runs the command and returns the result of it along with the output object |
101
|
|
|
* @param array $argv |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
private function runCommand(array $argv = []) |
105
|
|
|
{ |
106
|
|
|
$input = new ArgvInput($argv); |
107
|
|
|
$output = new BufferedOutput(); |
108
|
|
|
|
109
|
|
|
// create Command and set container |
110
|
|
|
$formatCommand = new FormatCommand(); |
111
|
|
|
$formatCommand->setContainer(self::$container); |
112
|
|
|
|
113
|
|
|
// run command |
114
|
|
|
$result = $formatCommand->run($input, $output); |
115
|
|
|
|
116
|
|
|
return [$result, $output]; |
117
|
|
|
} |
118
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.