3faa50c551c7044161d122433f95618d75726b38
[yaffs-website] / vendor / symfony / process / Tests / ProcessBuilderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\Process\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Process\ProcessBuilder;
16
17 class ProcessBuilderTest extends TestCase
18 {
19     public function testInheritEnvironmentVars()
20     {
21         $_ENV['MY_VAR_1'] = 'foo';
22
23         $proc = ProcessBuilder::create()
24             ->add('foo')
25             ->getProcess();
26
27         unset($_ENV['MY_VAR_1']);
28
29         $env = $proc->getEnv();
30         $this->assertArrayHasKey('MY_VAR_1', $env);
31         $this->assertEquals('foo', $env['MY_VAR_1']);
32     }
33
34     public function testAddEnvironmentVariables()
35     {
36         $pb = new ProcessBuilder();
37         $env = array(
38             'foo' => 'bar',
39             'foo2' => 'bar2',
40         );
41         $proc = $pb
42             ->add('command')
43             ->setEnv('foo', 'bar2')
44             ->addEnvironmentVariables($env)
45             ->inheritEnvironmentVariables(false)
46             ->getProcess()
47         ;
48
49         $this->assertSame($env, $proc->getEnv());
50     }
51
52     public function testProcessShouldInheritAndOverrideEnvironmentVars()
53     {
54         $_ENV['MY_VAR_1'] = 'foo';
55
56         $proc = ProcessBuilder::create()
57             ->setEnv('MY_VAR_1', 'bar')
58             ->add('foo')
59             ->getProcess();
60
61         unset($_ENV['MY_VAR_1']);
62
63         $env = $proc->getEnv();
64         $this->assertArrayHasKey('MY_VAR_1', $env);
65         $this->assertEquals('bar', $env['MY_VAR_1']);
66     }
67
68     /**
69      * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
70      */
71     public function testNegativeTimeoutFromSetter()
72     {
73         $pb = new ProcessBuilder();
74         $pb->setTimeout(-1);
75     }
76
77     public function testNullTimeout()
78     {
79         $pb = new ProcessBuilder();
80         $pb->setTimeout(10);
81         $pb->setTimeout(null);
82
83         $r = new \ReflectionObject($pb);
84         $p = $r->getProperty('timeout');
85         $p->setAccessible(true);
86
87         $this->assertNull($p->getValue($pb));
88     }
89
90     public function testShouldSetArguments()
91     {
92         $pb = new ProcessBuilder(array('initial'));
93         $pb->setArguments(array('second'));
94
95         $proc = $pb->getProcess();
96
97         $this->assertContains('second', $proc->getCommandLine());
98     }
99
100     public function testPrefixIsPrependedToAllGeneratedProcess()
101     {
102         $pb = new ProcessBuilder();
103         $pb->setPrefix('/usr/bin/php');
104
105         $proc = $pb->setArguments(array('-v'))->getProcess();
106         if ('\\' === DIRECTORY_SEPARATOR) {
107             $this->assertEquals('"/usr/bin/php" "-v"', $proc->getCommandLine());
108         } else {
109             $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
110         }
111
112         $proc = $pb->setArguments(array('-i'))->getProcess();
113         if ('\\' === DIRECTORY_SEPARATOR) {
114             $this->assertEquals('"/usr/bin/php" "-i"', $proc->getCommandLine());
115         } else {
116             $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
117         }
118     }
119
120     public function testArrayPrefixesArePrependedToAllGeneratedProcess()
121     {
122         $pb = new ProcessBuilder();
123         $pb->setPrefix(array('/usr/bin/php', 'composer.phar'));
124
125         $proc = $pb->setArguments(array('-v'))->getProcess();
126         if ('\\' === DIRECTORY_SEPARATOR) {
127             $this->assertEquals('"/usr/bin/php" "composer.phar" "-v"', $proc->getCommandLine());
128         } else {
129             $this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
130         }
131
132         $proc = $pb->setArguments(array('-i'))->getProcess();
133         if ('\\' === DIRECTORY_SEPARATOR) {
134             $this->assertEquals('"/usr/bin/php" "composer.phar" "-i"', $proc->getCommandLine());
135         } else {
136             $this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
137         }
138     }
139
140     public function testShouldEscapeArguments()
141     {
142         $pb = new ProcessBuilder(array('%path%', 'foo " bar', '%baz%baz'));
143         $proc = $pb->getProcess();
144
145         if ('\\' === DIRECTORY_SEPARATOR) {
146             $this->assertSame('^%"path"^% "foo \\" bar" "%baz%baz"', $proc->getCommandLine());
147         } else {
148             $this->assertSame("'%path%' 'foo \" bar' '%baz%baz'", $proc->getCommandLine());
149         }
150     }
151
152     public function testShouldEscapeArgumentsAndPrefix()
153     {
154         $pb = new ProcessBuilder(array('arg'));
155         $pb->setPrefix('%prefix%');
156         $proc = $pb->getProcess();
157
158         if ('\\' === DIRECTORY_SEPARATOR) {
159             $this->assertSame('^%"prefix"^% "arg"', $proc->getCommandLine());
160         } else {
161             $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
162         }
163     }
164
165     /**
166      * @expectedException \Symfony\Component\Process\Exception\LogicException
167      */
168     public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
169     {
170         ProcessBuilder::create()->getProcess();
171     }
172
173     public function testShouldNotThrowALogicExceptionIfNoArgument()
174     {
175         $process = ProcessBuilder::create()
176             ->setPrefix('/usr/bin/php')
177             ->getProcess();
178
179         if ('\\' === DIRECTORY_SEPARATOR) {
180             $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
181         } else {
182             $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
183         }
184     }
185
186     public function testShouldNotThrowALogicExceptionIfNoPrefix()
187     {
188         $process = ProcessBuilder::create(array('/usr/bin/php'))
189             ->getProcess();
190
191         if ('\\' === DIRECTORY_SEPARATOR) {
192             $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
193         } else {
194             $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
195         }
196     }
197
198     public function testShouldReturnProcessWithDisabledOutput()
199     {
200         $process = ProcessBuilder::create(array('/usr/bin/php'))
201             ->disableOutput()
202             ->getProcess();
203
204         $this->assertTrue($process->isOutputDisabled());
205     }
206
207     public function testShouldReturnProcessWithEnabledOutput()
208     {
209         $process = ProcessBuilder::create(array('/usr/bin/php'))
210             ->disableOutput()
211             ->enableOutput()
212             ->getProcess();
213
214         $this->assertFalse($process->isOutputDisabled());
215     }
216
217     /**
218      * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
219      * @expectedExceptionMessage Symfony\Component\Process\ProcessBuilder::setInput only accepts strings or stream resources.
220      */
221     public function testInvalidInput()
222     {
223         $builder = ProcessBuilder::create();
224         $builder->setInput(array());
225     }
226 }