1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[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 Symfony\Component\Yaml\Tests\Command; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\Yaml\Command\LintCommand; |
16
|
|
|
use Symfony\Component\Console\Application; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Tests the YamlLintCommand. |
22
|
|
|
* |
23
|
|
|
* @author Robin Chalas <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class LintCommandTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
private $files; |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testLintCorrectFile() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$tester = $this->createCommandTester(); |
32
|
|
|
$filename = $this->createFile('foo: bar'); |
33
|
|
|
|
34
|
|
|
$ret = $tester->execute(array('filename' => $filename), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)); |
35
|
|
|
|
36
|
|
|
$this->assertEquals(0, $ret, 'Returns 0 in case of success'); |
37
|
|
|
$this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testLintIncorrectFile() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$incorrectContent = ' |
43
|
|
|
foo: |
44
|
|
|
bar'; |
45
|
|
|
$tester = $this->createCommandTester(); |
46
|
|
|
$filename = $this->createFile($incorrectContent); |
47
|
|
|
|
48
|
|
|
$ret = $tester->execute(array('filename' => $filename), array('decorated' => false)); |
49
|
|
|
|
50
|
|
|
$this->assertEquals(1, $ret, 'Returns 1 in case of error'); |
51
|
|
|
$this->assertContains('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay())); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \RuntimeException |
56
|
|
|
*/ |
57
|
|
|
public function testLintFileNotReadable() |
58
|
|
|
{ |
59
|
|
|
$tester = $this->createCommandTester(); |
60
|
|
|
$filename = $this->createFile(''); |
61
|
|
|
unlink($filename); |
62
|
|
|
|
63
|
|
|
$ret = $tester->execute(array('filename' => $filename), array('decorated' => false)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string Path to the new file |
68
|
|
|
*/ |
69
|
|
|
private function createFile($content) |
70
|
|
|
{ |
71
|
|
|
$filename = tempnam(sys_get_temp_dir().'/framework-yml-lint-test', 'sf-'); |
72
|
|
|
file_put_contents($filename, $content); |
73
|
|
|
|
74
|
|
|
$this->files[] = $filename; |
75
|
|
|
|
76
|
|
|
return $filename; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return CommandTester |
81
|
|
|
*/ |
82
|
|
|
protected function createCommandTester() |
83
|
|
|
{ |
84
|
|
|
$application = new Application(); |
85
|
|
|
$application->add(new LintCommand()); |
86
|
|
|
$command = $application->find('lint:yaml'); |
87
|
|
|
|
88
|
|
|
return new CommandTester($command); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function setUp() |
92
|
|
|
{ |
93
|
|
|
$this->files = array(); |
94
|
|
|
@mkdir(sys_get_temp_dir().'/framework-yml-lint-test'); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function tearDown() |
98
|
|
|
{ |
99
|
|
|
foreach ($this->files as $file) { |
100
|
|
|
if (file_exists($file)) { |
101
|
|
|
unlink($file); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
rmdir(sys_get_temp_dir().'/framework-yml-lint-test'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.