Aucune description

ProcessBuilderTest.php 6.2KB

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