Skip to content

Commit e32eed2

Browse files
committed
Fix tests & YamlLintCommans help formatting
fabbot fixes
1 parent 5317d77 commit e32eed2

File tree

3 files changed

+140
-34
lines changed

3 files changed

+140
-34
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
* Validates YAML files syntax and outputs encountered errors.
1919
*
2020
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
* @author Robin Chalas <robin.chalas@gmail.com>
2122
*/
22-
final class YamlLintCommand extends BaseLintCommand
23+
class YamlLintCommand extends BaseLintCommand
2324
{
2425
/**
2526
* {@inheritdoc}
@@ -30,13 +31,7 @@ protected function configure()
3031

3132
$this
3233
->setHelp(
33-
$this->getHelp().<<<'EOF'
34-
35-
Or find all files in a bundle:
36-
37-
<info>php %command.full_name% @AcmeDemoBundle</info>
38-
39-
EOF
34+
$this->getFileRelatedHelp().$this->getBundleRelatedHelp().$this->getStdinRelatedHelp()
4035
)
4136
;
4237
}
@@ -54,4 +49,14 @@ protected function isReadable($fileOrDirectory)
5449
{
5550
return 0 === strpos($fileOrDirectory, '@') || parent::isReadable($fileOrDirectory);
5651
}
52+
53+
protected function getBundleRelatedHelp()
54+
{
55+
return <<<EOF
56+
Or find all files in a bundle:
57+
58+
<info>php %command.full_name% @AcmeDemoBundle</info>
59+
60+
EOF;
61+
}
5762
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php

+97-3
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,102 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
1313

14-
use Symfony\Component\Yaml\Tests\Command\LintCommandTest as BaseTest;
1514
use Symfony\Bundle\FrameworkBundle\Command\YamlLintCommand;
1615
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Output\OutputInterface;
1717
use Symfony\Component\Console\Tester\CommandTester;
1818

1919
/**
2020
* Tests the YamlLintCommand.
2121
*
2222
* @author Robin Chalas <robin.chalas@gmail.com>
2323
*/
24-
final class YamlLintCommandTest extends BaseTest
24+
class YamlLintCommandTest extends \PHPUnit_Framework_TestCase
2525
{
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+
26106
/**
27107
* @return CommandTester
28108
*/
29-
protected function createCommandTester()
109+
private function createCommandTester()
30110
{
31111
$command = new YamlLintCommand();
32112
$application = new Application();
@@ -35,4 +115,18 @@ protected function createCommandTester()
35115

36116
return new CommandTester($command);
37117
}
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+
}
38132
}

src/Symfony/Component/Yaml/Command/LintCommand.php

+30-23
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,11 @@ class LintCommand extends Command
3232
protected function configure()
3333
{
3434
$this
35+
->setHelp($this->getFileRelatedHelp().$this->getStdinRelatedHelp())
3536
->setName('lint:yaml')
3637
->setDescription('Lints a file and outputs encountered errors')
3738
->addArgument('filename', null, 'A file or a directory or STDIN')
3839
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
39-
->setHelp(<<<EOF
40-
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
41-
the first encountered syntax error.
42-
43-
You can validate the syntax of a file:
44-
45-
<info>php %command.full_name% filename</info>
46-
47-
Or of a whole directory:
48-
49-
<info>php %command.full_name% dirname</info>
50-
<info>php %command.full_name% dirname --format=json</info>
51-
52-
Or all YAML files in a bundle:
53-
54-
<info>php %command.full_name% @AcmeDemoBundle</info>
55-
56-
You can also pass the YAML contents from STDIN:
57-
58-
<info>cat filename | php %command.full_name%</info>
59-
60-
EOF
61-
)
6240
;
6341
}
6442

@@ -195,4 +173,33 @@ protected function findFilesInDirectory($directory)
195173

196174
return $files;
197175
}
176+
177+
protected function getFileRelatedHelp()
178+
{
179+
return <<<EOF
180+
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
181+
the first encountered syntax error.
182+
183+
You can validate the syntax of a file:
184+
185+
<info>php %command.full_name% filename</info>
186+
187+
Or of a whole directory:
188+
189+
<info>php %command.full_name% dirname</info>
190+
<info>php %command.full_name% dirname --format=json</info>
191+
EOF;
192+
}
193+
194+
protected function getStdinRelatedHelp()
195+
{
196+
return <<<EOF
197+
198+
199+
You can also pass the YAML contents from STDIN:
200+
201+
<info>cat filename | php %command.full_name%</info>
202+
203+
EOF;
204+
}
198205
}

0 commit comments

Comments
 (0)