3 namespace Stecman\Component\Symfony\Console\BashCompletion\Tests;
5 use PHPUnit\Framework\TestCase;
6 use Stecman\Component\Symfony\Console\BashCompletion\HookFactory;
8 class HookFactoryTest extends TestCase
15 protected function setUp()
17 $this->factory = new HookFactory();
21 * @dataProvider generateHookDataProvider
23 public function testBashSyntax($programPath, $programName, $multiple)
25 if ($this->hasProgram('bash')) {
26 $script = $this->factory->generateHook('bash', $programPath, $programName, $multiple);
27 $this->assertSyntaxIsValid($script, 'bash -n', 'BASH hook');
29 $this->markTestSkipped("Couldn't detect BASH program to run hook syntax check");
34 * @dataProvider generateHookDataProvider
36 public function testZshSyntax($programPath, $programName, $multiple)
38 if ($this->hasProgram('zsh')) {
39 $script = $this->factory->generateHook('zsh', $programPath, $programName, $multiple);
40 $this->assertSyntaxIsValid($script, 'zsh -n', 'ZSH hook');
42 $this->markTestSkipped("Couldn't detect ZSH program to run hook syntax check");
46 public function generateHookDataProvider()
49 array('/path/to/myprogram', null, false),
50 array('/path/to/myprogram', null, true),
51 array('/path/to/myprogram', 'myprogram', false),
52 array('/path/to/myprogram', 'myprogram', true),
53 array('/path/to/my-program', 'my-program', false)
57 protected function hasProgram($programName)
61 escapeshellarg($programName)
68 * @param string $code - code to pipe to the syntax checking command
69 * @param string $syntaxCheckCommand - equivalent to `bash -n`.
70 * @param string $context - what the syntax check is for
72 protected function assertSyntaxIsValid($code, $syntaxCheckCommand, $context)
75 escapeshellcmd($syntaxCheckCommand),
77 0 => array('pipe', 'r'),
78 1 => array('pipe', 'w'),
79 2 => array('pipe', 'w')
84 if (is_resource($process)) {
85 // Push code into STDIN for the syntax checking process
86 fwrite($pipes[0], $code);
89 $output = stream_get_contents($pipes[1]) . stream_get_contents($pipes[2]);
93 $status = proc_close($process);
95 $this->assertSame(0, $status, "Syntax check for $context failed:\n$output");
97 throw new \RuntimeException("Failed to start process with command '$syntaxCheckCommand'");