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