ProcessBuilderTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. $snapshot = $_ENV;
  17. $_ENV = $expected = array('foo' => 'bar');
  18. $pb = new ProcessBuilder();
  19. $pb->add('foo')->inheritEnvironmentVariables();
  20. $proc = $pb->getProcess();
  21. $this->assertNull($proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
  22. $_ENV = $snapshot;
  23. }
  24. public function testProcessShouldInheritAndOverrideEnvironmentVars()
  25. {
  26. $snapshot = $_ENV;
  27. $_ENV = array('foo' => 'bar', 'bar' => 'baz');
  28. $expected = array('foo' => 'foo', 'bar' => 'baz');
  29. $pb = new ProcessBuilder();
  30. $pb->add('foo')->inheritEnvironmentVariables()
  31. ->setEnv('foo', 'foo');
  32. $proc = $pb->getProcess();
  33. $this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
  34. $_ENV = $snapshot;
  35. }
  36. public function testProcessBuilderShouldNotPassEnvArrays()
  37. {
  38. $snapshot = $_ENV;
  39. $_ENV = array('a' => array('b', 'c'), 'd' => 'e', 'f' => 'g');
  40. $expected = array('d' => 'e', 'f' => 'g');
  41. $pb = new ProcessBuilder();
  42. $pb->add('a')->inheritEnvironmentVariables()
  43. ->setEnv('d', 'e');
  44. $proc = $pb->getProcess();
  45. $this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() removes array values from $_ENV');
  46. $_ENV = $snapshot;
  47. }
  48. public function testInheritEnvironmentVarsByDefault()
  49. {
  50. $pb = new ProcessBuilder();
  51. $proc = $pb->add('foo')->getProcess();
  52. $this->assertNull($proc->getEnv());
  53. }
  54. public function testNotReplaceExplicitlySetVars()
  55. {
  56. $snapshot = $_ENV;
  57. $_ENV = array('foo' => 'bar');
  58. $expected = array('foo' => 'baz');
  59. $pb = new ProcessBuilder();
  60. $pb
  61. ->setEnv('foo', 'baz')
  62. ->inheritEnvironmentVariables()
  63. ->add('foo')
  64. ;
  65. $proc = $pb->getProcess();
  66. $this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
  67. $_ENV = $snapshot;
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
  71. */
  72. public function testNegativeTimeoutFromSetter()
  73. {
  74. $pb = new ProcessBuilder();
  75. $pb->setTimeout(-1);
  76. }
  77. public function testNullTimeout()
  78. {
  79. $pb = new ProcessBuilder();
  80. $pb->setTimeout(10);
  81. $pb->setTimeout(null);
  82. $r = new \ReflectionObject($pb);
  83. $p = $r->getProperty('timeout');
  84. $p->setAccessible(true);
  85. $this->assertNull($p->getValue($pb));
  86. }
  87. public function testShouldSetArguments()
  88. {
  89. $pb = new ProcessBuilder(array('initial'));
  90. $pb->setArguments(array('second'));
  91. $proc = $pb->getProcess();
  92. $this->assertContains("second", $proc->getCommandLine());
  93. }
  94. public function testPrefixIsPrependedToAllGeneratedProcess()
  95. {
  96. $pb = new ProcessBuilder();
  97. $pb->setPrefix('/usr/bin/php');
  98. $proc = $pb->setArguments(array('-v'))->getProcess();
  99. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  100. $this->assertEquals('"/usr/bin/php" "-v"', $proc->getCommandLine());
  101. } else {
  102. $this->assertEquals("'/usr/bin/php' '-v'", $proc->getCommandLine());
  103. }
  104. $proc = $pb->setArguments(array('-i'))->getProcess();
  105. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  106. $this->assertEquals('"/usr/bin/php" "-i"', $proc->getCommandLine());
  107. } else {
  108. $this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
  109. }
  110. }
  111. public function testShouldEscapeArguments()
  112. {
  113. $pb = new ProcessBuilder(array('%path%', 'foo " bar'));
  114. $proc = $pb->getProcess();
  115. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  116. $this->assertSame('^%"path"^% "foo "\\"" bar"', $proc->getCommandLine());
  117. } else {
  118. $this->assertSame("'%path%' 'foo \" bar'", $proc->getCommandLine());
  119. }
  120. }
  121. public function testShouldEscapeArgumentsAndPrefix()
  122. {
  123. $pb = new ProcessBuilder(array('arg'));
  124. $pb->setPrefix('%prefix%');
  125. $proc = $pb->getProcess();
  126. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  127. $this->assertSame('^%"prefix"^% "arg"', $proc->getCommandLine());
  128. } else {
  129. $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine());
  130. }
  131. }
  132. /**
  133. * @expectedException \Symfony\Component\Process\Exception\LogicException
  134. */
  135. public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument()
  136. {
  137. ProcessBuilder::create()->getProcess();
  138. }
  139. public function testShouldNotThrowALogicExceptionIfNoArgument()
  140. {
  141. $process = ProcessBuilder::create()
  142. ->setPrefix('/usr/bin/php')
  143. ->getProcess();
  144. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  145. $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
  146. } else {
  147. $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
  148. }
  149. }
  150. public function testShouldNotThrowALogicExceptionIfNoPrefix()
  151. {
  152. $process = ProcessBuilder::create(array('/usr/bin/php'))
  153. ->getProcess();
  154. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  155. $this->assertEquals('"/usr/bin/php"', $process->getCommandLine());
  156. } else {
  157. $this->assertEquals("'/usr/bin/php'", $process->getCommandLine());
  158. }
  159. }
  160. }