ExecutableFinderTest.php 4.5 KB

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