ProcessBuilderTest.php 6.7 KB

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