ProcessBuilderTest.php 6.2 KB

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