ProcessBuilderTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 testInheritEnvironmentVarsByDefault()
  37. {
  38. $pb = new ProcessBuilder();
  39. $proc = $pb->add('foo')->getProcess();
  40. $this->assertNull($proc->getEnv());
  41. }
  42. public function testNotReplaceExplicitlySetVars()
  43. {
  44. $snapshot = $_ENV;
  45. $_ENV = array('foo' => 'bar');
  46. $expected = array('foo' => 'baz');
  47. $pb = new ProcessBuilder();
  48. $pb
  49. ->setEnv('foo', 'baz')
  50. ->inheritEnvironmentVariables()
  51. ->add('foo')
  52. ;
  53. $proc = $pb->getProcess();
  54. $this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
  55. $_ENV = $snapshot;
  56. }
  57. /**
  58. * @expectedException \InvalidArgumentException
  59. */
  60. public function testNegativeTimeoutFromSetter()
  61. {
  62. $pb = new ProcessBuilder();
  63. $pb->setTimeout(-1);
  64. }
  65. public function testNullTimeout()
  66. {
  67. $pb = new ProcessBuilder();
  68. $pb->setTimeout(10);
  69. $pb->setTimeout(null);
  70. $r = new \ReflectionObject($pb);
  71. $p = $r->getProperty('timeout');
  72. $p->setAccessible(true);
  73. $this->assertNull($p->getValue($pb));
  74. }
  75. }