ProcessBuilderTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }