ExecutableFinderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\ExecutableFinder;
  12. /**
  13. * @author Chris Smith <chris@cs278.org>
  14. */
  15. class ExecutableFinderTest extends \PHPUnit_Framework_TestCase
  16. {
  17. private $path;
  18. protected function tearDown()
  19. {
  20. if ($this->path) {
  21. // Restore path if it was changed.
  22. putenv('PATH='.$this->path);
  23. }
  24. }
  25. private function setPath($path)
  26. {
  27. $this->path = getenv('PATH');
  28. putenv('PATH='.$path);
  29. }
  30. /**
  31. * @requires PHP 5.4
  32. */
  33. public function testFind()
  34. {
  35. if (ini_get('open_basedir')) {
  36. $this->markTestSkipped('Cannot test when open_basedir is set');
  37. }
  38. $this->setPath(dirname(PHP_BINARY));
  39. $finder = new ExecutableFinder();
  40. $result = $finder->find($this->getPhpBinaryName());
  41. $this->assertSamePath(PHP_BINARY, $result);
  42. }
  43. public function testFindWithDefault()
  44. {
  45. if (ini_get('open_basedir')) {
  46. $this->markTestSkipped('Cannot test when open_basedir is set');
  47. }
  48. $expected = 'defaultValue';
  49. $this->setPath('');
  50. $finder = new ExecutableFinder();
  51. $result = $finder->find('foo', $expected);
  52. $this->assertEquals($expected, $result);
  53. }
  54. /**
  55. * @requires PHP 5.4
  56. */
  57. public function testFindWithExtraDirs()
  58. {
  59. if (ini_get('open_basedir')) {
  60. $this->markTestSkipped('Cannot test when open_basedir is set');
  61. }
  62. $this->setPath('');
  63. $extraDirs = array(dirname(PHP_BINARY));
  64. $finder = new ExecutableFinder();
  65. $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
  66. $this->assertSamePath(PHP_BINARY, $result);
  67. }
  68. /**
  69. * @requires PHP 5.4
  70. */
  71. public function testFindWithOpenBaseDir()
  72. {
  73. if ('\\' === DIRECTORY_SEPARATOR) {
  74. $this->markTestSkipped('Cannot run test on windows');
  75. }
  76. if (ini_get('open_basedir')) {
  77. $this->markTestSkipped('Cannot test when open_basedir is set');
  78. }
  79. $this->iniSet('open_basedir', dirname(PHP_BINARY).(!defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
  80. $finder = new ExecutableFinder();
  81. $result = $finder->find($this->getPhpBinaryName());
  82. $this->assertSamePath(PHP_BINARY, $result);
  83. }
  84. /**
  85. * @requires PHP 5.4
  86. */
  87. public function testFindProcessInOpenBasedir()
  88. {
  89. if (ini_get('open_basedir')) {
  90. $this->markTestSkipped('Cannot test when open_basedir is set');
  91. }
  92. if ('\\' === DIRECTORY_SEPARATOR) {
  93. $this->markTestSkipped('Cannot run test on windows');
  94. }
  95. $this->setPath('');
  96. $this->iniSet('open_basedir', PHP_BINARY.(!defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
  97. $finder = new ExecutableFinder();
  98. $result = $finder->find($this->getPhpBinaryName(), false);
  99. $this->assertSamePath(PHP_BINARY, $result);
  100. }
  101. private function assertSamePath($expected, $tested)
  102. {
  103. if ('\\' === DIRECTORY_SEPARATOR) {
  104. $this->assertEquals(strtolower($expected), strtolower($tested));
  105. } else {
  106. $this->assertEquals($expected, $tested);
  107. }
  108. }
  109. private function getPhpBinaryName()
  110. {
  111. return basename(PHP_BINARY, '\\' === DIRECTORY_SEPARATOR ? '.exe' : '');
  112. }
  113. }