ExecutableFinderTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /**
  86. * @runInSeparateProcess
  87. */
  88. public function testFindBatchExecutableOnWindows()
  89. {
  90. if (\ini_get('open_basedir')) {
  91. $this->markTestSkipped('Cannot test when open_basedir is set');
  92. }
  93. if ('\\' !== \DIRECTORY_SEPARATOR) {
  94. $this->markTestSkipped('Can be only tested on windows');
  95. }
  96. $tempDir = realpath(sys_get_temp_dir());
  97. $target = str_replace('.tmp', '_tmp', tempnam($tempDir, 'example-windows-executable'));
  98. try {
  99. touch($target);
  100. touch($target.'.BAT');
  101. $this->assertFalse(is_executable($target));
  102. putenv('PATH='.$tempDir);
  103. $finder = new ExecutableFinder();
  104. $result = $finder->find(basename($target), false);
  105. } finally {
  106. unlink($target);
  107. unlink($target.'.BAT');
  108. }
  109. $this->assertSamePath($target.'.BAT', $result);
  110. }
  111. /**
  112. * @runInSeparateProcess
  113. */
  114. public function testEmptyDirInPath()
  115. {
  116. putenv(sprintf('PATH=%s%s', \dirname(\PHP_BINARY), \PATH_SEPARATOR));
  117. try {
  118. touch('executable');
  119. chmod('executable', 0700);
  120. $finder = new ExecutableFinder();
  121. $result = $finder->find('executable');
  122. $this->assertSame(sprintf('.%sexecutable', \DIRECTORY_SEPARATOR), $result);
  123. } finally {
  124. unlink('executable');
  125. }
  126. }
  127. public function testFindBuiltInCommandOnWindows()
  128. {
  129. if ('\\' !== \DIRECTORY_SEPARATOR) {
  130. $this->markTestSkipped('Can be only tested on windows');
  131. }
  132. $finder = new ExecutableFinder();
  133. $this->assertSame('rmdir', strtolower($finder->find('RMDIR')));
  134. $this->assertSame('cd', strtolower($finder->find('cd')));
  135. $this->assertSame('move', strtolower($finder->find('MoVe')));
  136. }
  137. private function assertSamePath($expected, $tested)
  138. {
  139. if ('\\' === \DIRECTORY_SEPARATOR) {
  140. $this->assertEquals(strtolower($expected), strtolower($tested));
  141. } else {
  142. $this->assertEquals($expected, $tested);
  143. }
  144. }
  145. private function getPhpBinaryName()
  146. {
  147. return basename(\PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
  148. }
  149. }