ProcessBuilderTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Process\ProcessBuilder;
  13. /**
  14. * @group legacy
  15. */
  16. class ProcessBuilderTest extends TestCase
  17. {
  18. public function testInheritEnvironmentVars()
  19. {
  20. $proc = ProcessBuilder::create()
  21. ->add('foo')
  22. ->getProcess();
  23. $this->assertTrue($proc->areEnvironmentVariablesInherited());
  24. $proc = ProcessBuilder::create()
  25. ->add('foo')
  26. ->inheritEnvironmentVariables(false)
  27. ->getProcess();
  28. $this->assertFalse($proc->areEnvironmentVariablesInherited());
  29. }
  30. public function testAddEnvironmentVariables()
  31. {
  32. $pb = new ProcessBuilder();
  33. $env = [
  34. 'foo' => 'bar',
  35. 'foo2' => 'bar2',
  36. ];
  37. $proc = $pb
  38. ->add('command')
  39. ->setEnv('foo', 'bar2')
  40. ->addEnvironmentVariables($env)
  41. ->getProcess()
  42. ;
  43. $this->assertSame($env, $proc->getEnv());
  44. }
  45. public function testNegativeTimeoutFromSetter()
  46. {
  47. $this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException');
  48. $pb = new ProcessBuilder();
  49. $pb->setTimeout(-1);
  50. }
  51. public function testNullTimeout()
  52. {
  53. $pb = new ProcessBuilder();
  54. $pb->setTimeout(10);
  55. $pb->setTimeout(null);
  56. $r = new \ReflectionObject($pb);
  57. $p = $r->getProperty('timeout');
  58. $p->setAccessible(true);
  59. $this->assertNull($p->getValue($pb));
  60. }
  61. public function testShouldSetArguments()
  62. {
  63. $pb = new ProcessBuilder(['initial']);
  64. $pb->setArguments(['second']);
  65. $proc = $pb->getProcess();
  66. $this->assertStringContainsString('second', $proc->getCommandLine());
  67. }
  68. public function testPrefixIsPrependedToAllGeneratedProcess()
  69. {
  70. $pb = new ProcessBuilder();
  71. $pb->setPrefix('/usr/bin/php');
  72. $proc = $pb->setArguments(['-v'])->getProcess();
  73. if ('\\' === \DIRECTORY_SEPARATOR) {
  74. $this->assertEquals('"/usr/bin/php" -v', $proc->getCommandLine());
  75. } else {
  76. $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
  77. }
  78. $proc = $pb->setArguments(['-i'])->getProcess();
  79. if ('\\' === \DIRECTORY_SEPARATOR) {
  80. $this->assertEquals('"/usr/bin/php" -i', $proc->getCommandLine());
  81. } else {
  82. $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
  83. }
  84. }
  85. public function testArrayPrefixesArePrependedToAllGeneratedProcess()
  86. {
  87. $pb = new ProcessBuilder();
  88. $pb->setPrefix(['/usr/bin/php', 'composer.phar']);
  89. $proc = $pb->setArguments(['-v'])->getProcess();
  90. if ('\\' === \DIRECTORY_SEPARATOR) {
  91. $this->assertEquals('"/usr/bin/php" composer.phar -v', $proc->getCommandLine());
  92. } else {
  93. $this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
  94. }
  95. $proc = $pb->setArguments(['-i'])->getProcess();
  96. if ('\\' === \DIRECTORY_SEPARATOR) {
  97. $this->assertEquals('"/usr/bin/php" composer.phar -i', $proc->getCommandLine());
  98. } else {
  99. $this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
  100. }
  101. }
  102. public function testShouldEscapeArguments()
  103. {
  104. $pb = new ProcessBuilder(['%path%', 'foo " bar', '%baz%baz']);
  105. $proc = $pb->getProcess();
  106. if ('\\' === \DIRECTORY_SEPARATOR) {
  107. $this->assertSame('""^%"path"^%"" "foo "" bar" ""^%"baz"^%"baz"', $proc->getCommandLine());
  108. } else {
  109. $this->assertSame("'%path%' 'foo \" bar' '%baz%baz'", $proc->getCommandLine());
  110. }
  111. }
  112. public function testShouldEscapeArgumentsAndPrefix()
  113. {
  114. $pb = new ProcessBuilder(['arg']);
  115. $pb->setPrefix('%prefix%');
  116. $proc = $pb->getProcess();
  117. if ('\\' === \DIRECTORY_SEPARATOR) {
  118. $this->assertSame('""^%"prefix"^%"" arg', $proc->getCommandLine());
  119. } else {
  120. $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
  121. }
  122. }
  123. public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
  124. {
  125. $this->expectException('Symfony\Component\Process\Exception\LogicException');
  126. ProcessBuilder::create()->getProcess();
  127. }
  128. public function testShouldNotThrowALogicExceptionIfNoArgument()
  129. {
  130. $process = ProcessBuilder::create()
  131. ->setPrefix('/usr/bin/php')
  132. ->getProcess();
  133. if ('\\' === \DIRECTORY_SEPARATOR) {
  134. $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
  135. } else {
  136. $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
  137. }
  138. }
  139. public function testShouldNotThrowALogicExceptionIfNoPrefix()
  140. {
  141. $process = ProcessBuilder::create(['/usr/bin/php'])
  142. ->getProcess();
  143. if ('\\' === \DIRECTORY_SEPARATOR) {
  144. $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
  145. } else {
  146. $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
  147. }
  148. }
  149. public function testShouldReturnProcessWithDisabledOutput()
  150. {
  151. $process = ProcessBuilder::create(['/usr/bin/php'])
  152. ->disableOutput()
  153. ->getProcess();
  154. $this->assertTrue($process->isOutputDisabled());
  155. }
  156. public function testShouldReturnProcessWithEnabledOutput()
  157. {
  158. $process = ProcessBuilder::create(['/usr/bin/php'])
  159. ->disableOutput()
  160. ->enableOutput()
  161. ->getProcess();
  162. $this->assertFalse($process->isOutputDisabled());
  163. }
  164. public function testInvalidInput()
  165. {
  166. $this->expectException('Symfony\Component\Process\Exception\InvalidArgumentException');
  167. $this->expectExceptionMessage('"Symfony\Component\Process\ProcessBuilder::setInput" only accepts strings, Traversable objects or stream resources.');
  168. $builder = ProcessBuilder::create();
  169. $builder->setInput([]);
  170. }
  171. public function testDoesNotPrefixExec()
  172. {
  173. if ('\\' === \DIRECTORY_SEPARATOR) {
  174. $this->markTestSkipped('This test cannot run on Windows.');
  175. }
  176. $builder = ProcessBuilder::create(['command', '-v', 'ls']);
  177. $process = $builder->getProcess();
  178. $process->run();
  179. $this->assertTrue($process->isSuccessful());
  180. }
  181. }