ExecutableFinderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. protected function tearDown(): void
  19. {
  20. putenv('PATH='.($_SERVER['PATH'] ?? $_SERVER['Path']));
  21. }
  22. public function testFind()
  23. {
  24. if (\ini_get('open_basedir')) {
  25. $this->markTestSkipped('Cannot test when open_basedir is set');
  26. }
  27. putenv('PATH='.\dirname(\PHP_BINARY));
  28. $finder = new ExecutableFinder();
  29. $result = $finder->find($this->getPhpBinaryName());
  30. $this->assertSamePath(\PHP_BINARY, $result);
  31. }
  32. public function testFindWithDefault()
  33. {
  34. if (\ini_get('open_basedir')) {
  35. $this->markTestSkipped('Cannot test when open_basedir is set');
  36. }
  37. $expected = 'defaultValue';
  38. putenv('PATH=');
  39. $finder = new ExecutableFinder();
  40. $result = $finder->find('foo', $expected);
  41. $this->assertEquals($expected, $result);
  42. }
  43. public function testFindWithNullAsDefault()
  44. {
  45. if (\ini_get('open_basedir')) {
  46. $this->markTestSkipped('Cannot test when open_basedir is set');
  47. }
  48. putenv('PATH=');
  49. $finder = new ExecutableFinder();
  50. $result = $finder->find('foo');
  51. $this->assertNull($result);
  52. }
  53. public function testFindWithExtraDirs()
  54. {
  55. if (\ini_get('open_basedir')) {
  56. $this->markTestSkipped('Cannot test when open_basedir is set');
  57. }
  58. putenv('PATH=');
  59. $extraDirs = [\dirname(\PHP_BINARY)];
  60. $finder = new ExecutableFinder();
  61. $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
  62. $this->assertSamePath(\PHP_BINARY, $result);
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testFindWithOpenBaseDir()
  68. {
  69. if ('\\' === \DIRECTORY_SEPARATOR) {
  70. $this->markTestSkipped('Cannot run test on windows');
  71. }
  72. if (\ini_get('open_basedir')) {
  73. $this->markTestSkipped('Cannot test when open_basedir is set');
  74. }
  75. putenv('PATH='.\dirname(\PHP_BINARY));
  76. $initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/');
  77. try {
  78. $finder = new ExecutableFinder();
  79. $result = $finder->find($this->getPhpBinaryName());
  80. $this->assertSamePath(\PHP_BINARY, $result);
  81. } finally {
  82. ini_set('open_basedir', $initialOpenBaseDir);
  83. }
  84. }
  85. public function testFindBatchExecutableOnWindows()
  86. {
  87. if (\ini_get('open_basedir')) {
  88. $this->markTestSkipped('Cannot test when open_basedir is set');
  89. }
  90. if ('\\' !== \DIRECTORY_SEPARATOR) {
  91. $this->markTestSkipped('Can be only tested on windows');
  92. }
  93. $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
  94. touch($target);
  95. touch($target.'.BAT');
  96. $this->assertFalse(is_executable($target));
  97. putenv('PATH='.sys_get_temp_dir());
  98. $finder = new ExecutableFinder();
  99. $result = $finder->find(basename($target), false);
  100. unlink($target);
  101. unlink($target.'.BAT');
  102. $this->assertSamePath($target.'.BAT', $result);
  103. }
  104. private function assertSamePath($expected, $tested)
  105. {
  106. if ('\\' === \DIRECTORY_SEPARATOR) {
  107. $this->assertEquals(strtolower($expected), strtolower($tested));
  108. } else {
  109. $this->assertEquals($expected, $tested);
  110. }
  111. }
  112. private function getPhpBinaryName()
  113. {
  114. return basename(\PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
  115. }
  116. }