ProcessBuilderTest.php 6.7 KB

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