ExecutableFinderTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. $this->iniSet('open_basedir', \dirname(\PHP_BINARY).\PATH_SEPARATOR.'/');
  85. $finder = new ExecutableFinder();
  86. $result = $finder->find($this->getPhpBinaryName());
  87. $this->assertSamePath(\PHP_BINARY, $result);
  88. }
  89. /**
  90. * @runInSeparateProcess
  91. */
  92. public function testFindProcessInOpenBasedir()
  93. {
  94. if (ini_get('open_basedir')) {
  95. $this->markTestSkipped('Cannot test when open_basedir is set');
  96. }
  97. if ('\\' === \DIRECTORY_SEPARATOR) {
  98. $this->markTestSkipped('Cannot run test on windows');
  99. }
  100. $this->setPath('');
  101. $this->iniSet('open_basedir', \PHP_BINARY.\PATH_SEPARATOR.'/');
  102. $finder = new ExecutableFinder();
  103. $result = $finder->find($this->getPhpBinaryName(), false);
  104. $this->assertSamePath(\PHP_BINARY, $result);
  105. }
  106. public function testFindBatchExecutableOnWindows()
  107. {
  108. if (ini_get('open_basedir')) {
  109. $this->markTestSkipped('Cannot test when open_basedir is set');
  110. }
  111. if ('\\' !== \DIRECTORY_SEPARATOR) {
  112. $this->markTestSkipped('Can be only tested on windows');
  113. }
  114. $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
  115. touch($target);
  116. touch($target.'.BAT');
  117. $this->assertFalse(is_executable($target));
  118. $this->setPath(sys_get_temp_dir());
  119. $finder = new ExecutableFinder();
  120. $result = $finder->find(basename($target), false);
  121. unlink($target);
  122. unlink($target.'.BAT');
  123. $this->assertSamePath($target.'.BAT', $result);
  124. }
  125. private function assertSamePath($expected, $tested)
  126. {
  127. if ('\\' === \DIRECTORY_SEPARATOR) {
  128. $this->assertEquals(strtolower($expected), strtolower($tested));
  129. } else {
  130. $this->assertEquals($expected, $tested);
  131. }
  132. }
  133. private function getPhpBinaryName()
  134. {
  135. return basename(\PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
  136. }
  137. }