11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Tests \Command ;
13
13
14
- use Symfony \Component \Yaml \Tests \Command \LintCommandTest as BaseTest ;
15
14
use Symfony \Bundle \FrameworkBundle \Command \YamlLintCommand ;
16
15
use Symfony \Component \Console \Application ;
16
+ use Symfony \Component \Console \Output \OutputInterface ;
17
17
use Symfony \Component \Console \Tester \CommandTester ;
18
18
19
19
/**
20
20
* Tests the YamlLintCommand.
21
21
*
22
22
* @author Robin Chalas <robin.chalas@gmail.com>
23
23
*/
24
- final class YamlLintCommandTest extends BaseTest
24
+ class YamlLintCommandTest extends \PHPUnit_Framework_TestCase
25
25
{
26
+ private $ files ;
27
+
28
+ public function testLintCorrectFile ()
29
+ {
30
+ $ tester = $ this ->createCommandTester ();
31
+ $ filename = $ this ->createFile ('foo: bar ' );
32
+
33
+ $ ret = $ tester ->execute (array ('filename ' => $ filename ), array ('verbosity ' => OutputInterface::VERBOSITY_VERBOSE , 'decorated ' => false ));
34
+
35
+ $ this ->assertEquals (0 , $ ret , 'Returns 0 in case of success ' );
36
+ $ this ->assertRegExp ('/^\/\/ OK in / ' , trim ($ tester ->getDisplay ()));
37
+ }
38
+
39
+ public function testLintIncorrectFile ()
40
+ {
41
+ $ incorrectContent = '
42
+ foo:
43
+ bar ' ;
44
+ $ tester = $ this ->createCommandTester ();
45
+ $ filename = $ this ->createFile ($ incorrectContent );
46
+
47
+ $ ret = $ tester ->execute (array ('filename ' => $ filename ), array ('decorated ' => false ));
48
+
49
+ $ this ->assertEquals (1 , $ ret , 'Returns 1 in case of error ' );
50
+ $ this ->assertContains ('Unable to parse at line 3 (near "bar"). ' , trim ($ tester ->getDisplay ()));
51
+ }
52
+
53
+ /**
54
+ * @expectedException \RuntimeException
55
+ */
56
+ public function testLintFileNotReadable ()
57
+ {
58
+ $ tester = $ this ->createCommandTester ();
59
+ $ filename = $ this ->createFile ('' );
60
+ unlink ($ filename );
61
+
62
+ $ ret = $ tester ->execute (array ('filename ' => $ filename ), array ('decorated ' => false ));
63
+ }
64
+
65
+ public function testGetHelp ()
66
+ {
67
+ $ command = new YamlLintCommand ();
68
+ $ expected = <<<EOF
69
+ The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
70
+ the first encountered syntax error.
71
+
72
+ You can validate the syntax of a file:
73
+
74
+ <info>php %command.full_name% filename</info>
75
+
76
+ Or of a whole directory:
77
+
78
+ <info>php %command.full_name% dirname</info>
79
+ <info>php %command.full_name% dirname --format=json</info>Or find all files in a bundle:
80
+
81
+ <info>php %command.full_name% @AcmeDemoBundle</info>
82
+
83
+
84
+ You can also pass the YAML contents from STDIN:
85
+
86
+ <info>cat filename | php %command.full_name%</info>
87
+
88
+ EOF ;
89
+
90
+ $ this ->assertEquals ($ expected , $ command ->getHelp ());
91
+ }
92
+
93
+ /**
94
+ * @return string Path to the new file
95
+ */
96
+ private function createFile ($ content )
97
+ {
98
+ $ filename = tempnam (sys_get_temp_dir (), 'sf- ' );
99
+ file_put_contents ($ filename , $ content );
100
+
101
+ $ this ->files [] = $ filename ;
102
+
103
+ return $ filename ;
104
+ }
105
+
26
106
/**
27
107
* @return CommandTester
28
108
*/
29
- protected function createCommandTester ()
109
+ private function createCommandTester ()
30
110
{
31
111
$ command = new YamlLintCommand ();
32
112
$ application = new Application ();
@@ -35,4 +115,18 @@ protected function createCommandTester()
35
115
36
116
return new CommandTester ($ command );
37
117
}
118
+
119
+ protected function setUp ()
120
+ {
121
+ $ this ->files = array ();
122
+ }
123
+
124
+ protected function tearDown ()
125
+ {
126
+ foreach ($ this ->files as $ file ) {
127
+ if (file_exists ($ file )) {
128
+ unlink ($ file );
129
+ }
130
+ }
131
+ }
38
132
}
0 commit comments