ExecutableFinderTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(): void
  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 testFindWithNullAsDefault()
  53. {
  54. if (\ini_get('open_basedir')) {
  55. $this->markTestSkipped('Cannot test when open_basedir is set');
  56. }
  57. $this->setPath('');
  58. $finder = new ExecutableFinder();
  59. $result = $finder->find('foo');
  60. $this->assertNull($result);
  61. }
  62. public function testFindWithExtraDirs()
  63. {
  64. if (\ini_get('open_basedir')) {
  65. $this->markTestSkipped('Cannot test when open_basedir is set');
  66. }
  67. $this->setPath('');
  68. $extraDirs = [\dirname(\PHP_BINARY)];
  69. $finder = new ExecutableFinder();
  70. $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
  71. $this->assertSamePath(\PHP_BINARY, $result);
  72. }
  73. /**
  74. * @runInSeparateProcess
  75. */
  76. public function testFindWithOpenBaseDir()
  77. {
  78. if ('\\' === \DIRECTORY_SEPARATOR) {
  79. $this->markTestSkipped('Cannot run test on windows');
  80. }
  81. if (\ini_get('open_basedir')) {
  82. $this->markTestSkipped('Cannot test when open_basedir is set');
  83. }
  84. $initialOpenBaseDir = ini_set('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/');
  85. try {
  86. $finder = new ExecutableFinder();
  87. $result = $finder->find($this->getPhpBinaryName());
  88. $this->assertSamePath(\PHP_BINARY, $result);
  89. } finally {
  90. ini_set('open_basedir', $initialOpenBaseDir);
  91. }
  92. }
  93. /**
  94. * @runInSeparateProcess
  95. */
  96. public function testFindProcessInOpenBasedir()
  97. {
  98. if (\ini_get('open_basedir')) {
  99. $this->markTestSkipped('Cannot test when open_basedir is set');
  100. }
  101. if ('\\' === \DIRECTORY_SEPARATOR) {
  102. $this->markTestSkipped('Cannot run test on windows');
  103. }
  104. $this->setPath('');
  105. $initialOpenBaseDir = ini_set('open_basedir', \PHP_BINARY.\PATH_SEPARATOR.'/');
  106. try {
  107. $finder = new ExecutableFinder();
  108. $result = $finder->find($this->getPhpBinaryName(), false);
  109. $this->assertSamePath(\PHP_BINARY, $result);
  110. } finally {
  111. ini_set('open_basedir', $initialOpenBaseDir);
  112. }
  113. }
  114. public function testFindBatchExecutableOnWindows()
  115. {
  116. if (\ini_get('open_basedir')) {
  117. $this->markTestSkipped('Cannot test when open_basedir is set');
  118. }
  119. if ('\\' !== \DIRECTORY_SEPARATOR) {
  120. $this->markTestSkipped('Can be only tested on windows');
  121. }
  122. $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
  123. touch($target);
  124. touch($target.'.BAT');
  125. $this->assertFalse(is_executable($target));
  126. $this->setPath(sys_get_temp_dir());
  127. $finder = new ExecutableFinder();
  128. $result = $finder->find(basename($target), false);
  129. unlink($target);
  130. unlink($target.'.BAT');
  131. $this->assertSamePath($target.'.BAT', $result);
  132. }
  133. private function assertSamePath($expected, $tested)
  134. {
  135. if ('\\' === \DIRECTORY_SEPARATOR) {
  136. $this->assertEquals(strtolower($expected), strtolower($tested));
  137. } else {
  138. $this->assertEquals($expected, $tested);
  139. }
  140. }
  141. private function getPhpBinaryName()
  142. {
  143. return basename(\PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
  144. }
  145. }