ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ProcessBuilderTest.php
Go to the documentation of this file.
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 
13 
15 
17 {
18  public function testInheritEnvironmentVars()
19  {
20  $_ENV['MY_VAR_1'] = 'foo';
21 
22  $proc = ProcessBuilder::create()
23  ->add('foo')
24  ->getProcess();
25 
26  unset($_ENV['MY_VAR_1']);
27 
28  $env = $proc->getEnv();
29  $this->assertArrayHasKey('MY_VAR_1', $env);
30  $this->assertEquals('foo', $env['MY_VAR_1']);
31  }
32 
33  public function testAddEnvironmentVariables()
34  {
35  $pb = new ProcessBuilder();
36  $env = array(
37  'foo' => 'bar',
38  'foo2' => 'bar2',
39  );
40  $proc = $pb
41  ->add('command')
42  ->setEnv('foo', 'bar2')
43  ->addEnvironmentVariables($env)
44  ->inheritEnvironmentVariables(false)
45  ->getProcess()
46  ;
47 
48  $this->assertSame($env, $proc->getEnv());
49  }
50 
52  {
53  $_ENV['MY_VAR_1'] = 'foo';
54 
55  $proc = ProcessBuilder::create()
56  ->setEnv('MY_VAR_1', 'bar')
57  ->add('foo')
58  ->getProcess();
59 
60  unset($_ENV['MY_VAR_1']);
61 
62  $env = $proc->getEnv();
63  $this->assertArrayHasKey('MY_VAR_1', $env);
64  $this->assertEquals('bar', $env['MY_VAR_1']);
65  }
66 
71  {
72  $pb = new ProcessBuilder();
73  $pb->setTimeout(-1);
74  }
75 
76  public function testNullTimeout()
77  {
78  $pb = new ProcessBuilder();
79  $pb->setTimeout(10);
80  $pb->setTimeout(null);
81 
82  $r = new \ReflectionObject($pb);
83  $p = $r->getProperty('timeout');
84  $p->setAccessible(true);
85 
86  $this->assertNull($p->getValue($pb));
87  }
88 
89  public function testShouldSetArguments()
90  {
91  $pb = new ProcessBuilder(array('initial'));
92  $pb->setArguments(array('second'));
93 
94  $proc = $pb->getProcess();
95 
96  $this->assertContains('second', $proc->getCommandLine());
97  }
98 
100  {
101  $pb = new ProcessBuilder();
102  $pb->setPrefix('/usr/bin/php');
103 
104  $proc = $pb->setArguments(array('-v'))->getProcess();
105  if ('\\' === DIRECTORY_SEPARATOR) {
106  $this->assertEquals('"/usr/bin/php" "-v"', $proc->getCommandLine());
107  } else {
108  $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
109  }
110 
111  $proc = $pb->setArguments(array('-i'))->getProcess();
112  if ('\\' === DIRECTORY_SEPARATOR) {
113  $this->assertEquals('"/usr/bin/php" "-i"', $proc->getCommandLine());
114  } else {
115  $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
116  }
117  }
118 
120  {
121  $pb = new ProcessBuilder();
122  $pb->setPrefix(array('/usr/bin/php', 'composer.phar'));
123 
124  $proc = $pb->setArguments(array('-v'))->getProcess();
125  if ('\\' === DIRECTORY_SEPARATOR) {
126  $this->assertEquals('"/usr/bin/php" "composer.phar" "-v"', $proc->getCommandLine());
127  } else {
128  $this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
129  }
130 
131  $proc = $pb->setArguments(array('-i'))->getProcess();
132  if ('\\' === DIRECTORY_SEPARATOR) {
133  $this->assertEquals('"/usr/bin/php" "composer.phar" "-i"', $proc->getCommandLine());
134  } else {
135  $this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
136  }
137  }
138 
139  public function testShouldEscapeArguments()
140  {
141  $pb = new ProcessBuilder(array('%path%', 'foo " bar', '%baz%baz'));
142  $proc = $pb->getProcess();
143 
144  if ('\\' === DIRECTORY_SEPARATOR) {
145  $this->assertSame('^%"path"^% "foo \\" bar" "%baz%baz"', $proc->getCommandLine());
146  } else {
147  $this->assertSame("'%path%' 'foo \" bar' '%baz%baz'", $proc->getCommandLine());
148  }
149  }
150 
152  {
153  $pb = new ProcessBuilder(array('arg'));
154  $pb->setPrefix('%prefix%');
155  $proc = $pb->getProcess();
156 
157  if ('\\' === DIRECTORY_SEPARATOR) {
158  $this->assertSame('^%"prefix"^% "arg"', $proc->getCommandLine());
159  } else {
160  $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
161  }
162  }
163 
168  {
169  ProcessBuilder::create()->getProcess();
170  }
171 
173  {
174  $process = ProcessBuilder::create()
175  ->setPrefix('/usr/bin/php')
176  ->getProcess();
177 
178  if ('\\' === DIRECTORY_SEPARATOR) {
179  $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
180  } else {
181  $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
182  }
183  }
184 
186  {
187  $process = ProcessBuilder::create(array('/usr/bin/php'))
188  ->getProcess();
189 
190  if ('\\' === DIRECTORY_SEPARATOR) {
191  $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
192  } else {
193  $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
194  }
195  }
196 
198  {
199  $process = ProcessBuilder::create(array('/usr/bin/php'))
200  ->disableOutput()
201  ->getProcess();
202 
203  $this->assertTrue($process->isOutputDisabled());
204  }
205 
207  {
208  $process = ProcessBuilder::create(array('/usr/bin/php'))
209  ->disableOutput()
210  ->enableOutput()
211  ->getProcess();
212 
213  $this->assertFalse($process->isOutputDisabled());
214  }
215 
220  public function testInvalidInput()
221  {
223  $builder->setInput(array());
224  }
225 }
static create(array $arguments=array())
Creates a process builder instance.
$r
Definition: example_031.php:79
Create styles array
The data for the language used.
testInvalidInput()
Symfony::setInput only accepts strings, Traversable objects or stream resources.