ProcessBuilderTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /**
  16. * @group legacy
  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 = array(
  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. /**
  46. * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
  47. */
  48. public function testNegativeTimeoutFromSetter()
  49. {
  50. $pb = new ProcessBuilder();
  51. $pb->setTimeout(-1);
  52. }
  53. public function testNullTimeout()
  54. {
  55. $pb = new ProcessBuilder();
  56. $pb->setTimeout(10);
  57. $pb->setTimeout(null);
  58. $r = new \ReflectionObject($pb);
  59. $p = $r->getProperty('timeout');
  60. $p->setAccessible(true);
  61. $this->assertNull($p->getValue($pb));
  62. }
  63. public function testShouldSetArguments()
  64. {
  65. $pb = new ProcessBuilder(array('initial'));
  66. $pb->setArguments(array('second'));
  67. $proc = $pb->getProcess();
  68. $this->assertContains('second', $proc->getCommandLine());
  69. }
  70. public function testPrefixIsPrependedToAllGeneratedProcess()
  71. {
  72. $pb = new ProcessBuilder();
  73. $pb->setPrefix('/usr/bin/php');
  74. $proc = $pb->setArguments(array('-v'))->getProcess();
  75. if ('\\' === DIRECTORY_SEPARATOR) {
  76. $this->assertEquals('"/usr/bin/php" -v', $proc->getCommandLine());
  77. } else {
  78. $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
  79. }
  80. $proc = $pb->setArguments(array('-i'))->getProcess();
  81. if ('\\' === DIRECTORY_SEPARATOR) {
  82. $this->assertEquals('"/usr/bin/php" -i', $proc->getCommandLine());
  83. } else {
  84. $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
  85. }
  86. }
  87. public function testArrayPrefixesArePrependedToAllGeneratedProcess()
  88. {
  89. $pb = new ProcessBuilder();
  90. $pb->setPrefix(array('/usr/bin/php', 'composer.phar'));
  91. $proc = $pb->setArguments(array('-v'))->getProcess();
  92. if ('\\' === DIRECTORY_SEPARATOR) {
  93. $this->assertEquals('"/usr/bin/php" composer.phar -v', $proc->getCommandLine());
  94. } else {
  95. $this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
  96. }
  97. $proc = $pb->setArguments(array('-i'))->getProcess();
  98. if ('\\' === DIRECTORY_SEPARATOR) {
  99. $this->assertEquals('"/usr/bin/php" composer.phar -i', $proc->getCommandLine());
  100. } else {
  101. $this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
  102. }
  103. }
  104. public function testShouldEscapeArguments()
  105. {
  106. $pb = new ProcessBuilder(array('%path%', 'foo " bar', '%baz%baz'));
  107. $proc = $pb->getProcess();
  108. if ('\\' === DIRECTORY_SEPARATOR) {
  109. $this->assertSame('""^%"path"^%"" "foo "" bar" ""^%"baz"^%"baz"', $proc->getCommandLine());
  110. } else {
  111. $this->assertSame("'%path%' 'foo \" bar' '%baz%baz'", $proc->getCommandLine());
  112. }
  113. }
  114. public function testShouldEscapeArgumentsAndPrefix()
  115. {
  116. $pb = new ProcessBuilder(array('arg'));
  117. $pb->setPrefix('%prefix%');
  118. $proc = $pb->getProcess();
  119. if ('\\' === DIRECTORY_SEPARATOR) {
  120. $this->assertSame('""^%"prefix"^%"" arg', $proc->getCommandLine());
  121. } else {
  122. $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
  123. }
  124. }
  125. /**
  126. * @expectedException \Symfony\Component\Process\Exception\LogicException
  127. */
  128. public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
  129. {
  130. ProcessBuilder::create()->getProcess();
  131. }
  132. public function testShouldNotThrowALogicExceptionIfNoArgument()
  133. {
  134. $process = ProcessBuilder::create()
  135. ->setPrefix('/usr/bin/php')
  136. ->getProcess();
  137. if ('\\' === DIRECTORY_SEPARATOR) {
  138. $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
  139. } else {
  140. $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
  141. }
  142. }
  143. public function testShouldNotThrowALogicExceptionIfNoPrefix()
  144. {
  145. $process = ProcessBuilder::create(array('/usr/bin/php'))
  146. ->getProcess();
  147. if ('\\' === DIRECTORY_SEPARATOR) {
  148. $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
  149. } else {
  150. $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
  151. }
  152. }
  153. public function testShouldReturnProcessWithDisabledOutput()
  154. {
  155. $process = ProcessBuilder::create(array('/usr/bin/php'))
  156. ->disableOutput()
  157. ->getProcess();
  158. $this->assertTrue($process->isOutputDisabled());
  159. }
  160. public function testShouldReturnProcessWithEnabledOutput()
  161. {
  162. $process = ProcessBuilder::create(array('/usr/bin/php'))
  163. ->disableOutput()
  164. ->enableOutput()
  165. ->getProcess();
  166. $this->assertFalse($process->isOutputDisabled());
  167. }
  168. /**
  169. * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
  170. * @expectedExceptionMessage Symfony\Component\Process\ProcessBuilder::setInput only accepts strings, Traversable objects or stream resources.
  171. */
  172. public function testInvalidInput()
  173. {
  174. $builder = ProcessBuilder::create();
  175. $builder->setInput(array());
  176. }
  177. }