ExecutableFinderTest.php 3.4KB

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