ExecutableFinderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. public function testFind()
  31. {
  32. if (ini_get('open_basedir')) {
  33. $this->markTestSkipped('Cannot test when open_basedir is set');
  34. }
  35. $this->setPath(dirname(PHP_BINARY));
  36. $finder = new ExecutableFinder();
  37. $result = $finder->find($this->getPhpBinaryName());
  38. $this->assertSamePath(PHP_BINARY, $result);
  39. }
  40. public function testFindWithDefault()
  41. {
  42. if (ini_get('open_basedir')) {
  43. $this->markTestSkipped('Cannot test when open_basedir is set');
  44. }
  45. $expected = 'defaultValue';
  46. $this->setPath('');
  47. $finder = new ExecutableFinder();
  48. $result = $finder->find('foo', $expected);
  49. $this->assertEquals($expected, $result);
  50. }
  51. public function testFindWithExtraDirs()
  52. {
  53. if (ini_get('open_basedir')) {
  54. $this->markTestSkipped('Cannot test when open_basedir is set');
  55. }
  56. $this->setPath('');
  57. $extraDirs = array(dirname(PHP_BINARY));
  58. $finder = new ExecutableFinder();
  59. $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
  60. $this->assertSamePath(PHP_BINARY, $result);
  61. }
  62. public function testFindWithOpenBaseDir()
  63. {
  64. if ('\\' === DIRECTORY_SEPARATOR) {
  65. $this->markTestSkipped('Cannot run test on windows');
  66. }
  67. if (ini_get('open_basedir')) {
  68. $this->markTestSkipped('Cannot test when open_basedir is set');
  69. }
  70. $this->iniSet('open_basedir', dirname(PHP_BINARY).(!defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
  71. $finder = new ExecutableFinder();
  72. $result = $finder->find($this->getPhpBinaryName());
  73. $this->assertSamePath(PHP_BINARY, $result);
  74. }
  75. public function testFindProcessInOpenBasedir()
  76. {
  77. if (ini_get('open_basedir')) {
  78. $this->markTestSkipped('Cannot test when open_basedir is set');
  79. }
  80. if ('\\' === DIRECTORY_SEPARATOR) {
  81. $this->markTestSkipped('Cannot run test on windows');
  82. }
  83. $this->setPath('');
  84. $this->iniSet('open_basedir', PHP_BINARY.(!defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
  85. $finder = new ExecutableFinder();
  86. $result = $finder->find($this->getPhpBinaryName(), false);
  87. $this->assertSamePath(PHP_BINARY, $result);
  88. }
  89. private function assertSamePath($expected, $tested)
  90. {
  91. if ('\\' === DIRECTORY_SEPARATOR) {
  92. $this->assertEquals(strtolower($expected), strtolower($tested));
  93. } else {
  94. $this->assertEquals($expected, $tested);
  95. }
  96. }
  97. private function getPhpBinaryName()
  98. {
  99. return basename(PHP_BINARY, '\\' === DIRECTORY_SEPARATOR ? '.exe' : '');
  100. }
  101. }