c253a62ee4cd6f0c2922e762125a303d4ef10fa1
[yaffs-website] / vendor / stecman / symfony-console-completion / tests / Stecman / Component / Symfony / Console / BashCompletion / HookFactoryTest.php
1 <?php
2
3 namespace Stecman\Component\Symfony\Console\BashCompletion\Tests;
4
5 use PHPUnit\Framework\TestCase;
6 use Stecman\Component\Symfony\Console\BashCompletion\HookFactory;
7
8 class HookFactoryTest extends TestCase
9 {
10     /**
11      * @var HookFactory
12      */
13     protected $factory;
14
15     protected function setUp()
16     {
17         $this->factory = new HookFactory();
18     }
19
20     /**
21      * @dataProvider generateHookDataProvider
22      */
23     public function testBashSyntax($programPath, $programName, $multiple)
24     {
25         if ($this->hasProgram('bash')) {
26             $script = $this->factory->generateHook('bash', $programPath, $programName, $multiple);
27             $this->assertSyntaxIsValid($script, 'bash -n', 'BASH hook');
28         } else {
29             $this->markTestSkipped("Couldn't detect BASH program to run hook syntax check");
30         }
31     }
32
33     /**
34      * @dataProvider generateHookDataProvider
35      */
36     public function testZshSyntax($programPath, $programName, $multiple)
37     {
38         if ($this->hasProgram('zsh')) {
39             $script = $this->factory->generateHook('zsh', $programPath, $programName, $multiple);
40             $this->assertSyntaxIsValid($script, 'zsh -n', 'ZSH hook');
41         } else {
42             $this->markTestSkipped("Couldn't detect ZSH program to run hook syntax check");
43         }
44     }
45
46     public function generateHookDataProvider()
47     {
48         return array(
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)
54         );
55     }
56
57     protected function hasProgram($programName)
58     {
59         exec(sprintf(
60             'command -v %s',
61             escapeshellarg($programName)
62         ), $output, $return);
63
64         return $return === 0;
65     }
66
67     /**
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
71      */
72     protected function assertSyntaxIsValid($code, $syntaxCheckCommand, $context)
73     {
74         $process = proc_open(
75             escapeshellcmd($syntaxCheckCommand),
76             array(
77                 0 => array('pipe', 'r'),
78                 1 => array('pipe', 'w'),
79                 2 => array('pipe', 'w')
80             ),
81             $pipes
82         );
83
84         if (is_resource($process)) {
85             // Push code into STDIN for the syntax checking process
86             fwrite($pipes[0], $code);
87             fclose($pipes[0]);
88
89             $output = stream_get_contents($pipes[1]) . stream_get_contents($pipes[2]);
90             fclose($pipes[1]);
91             fclose($pipes[2]);
92
93             $status = proc_close($process);
94
95             $this->assertSame(0, $status, "Syntax check for $context failed:\n$output");
96         } else {
97             throw new \RuntimeException("Failed to start process with command '$syntaxCheckCommand'");
98         }
99     }
100 }