ExecutableFinderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 testFindWithExtraDirs()
  53. {
  54. if (ini_get('open_basedir')) {
  55. $this->markTestSkipped('Cannot test when open_basedir is set');
  56. }
  57. $this->setPath('');
  58. $extraDirs = array(\dirname(PHP_BINARY));
  59. $finder = new ExecutableFinder();
  60. $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
  61. $this->assertSamePath(PHP_BINARY, $result);
  62. }
  63. public function testFindWithOpenBaseDir()
  64. {
  65. if ('\\' === \DIRECTORY_SEPARATOR) {
  66. $this->markTestSkipped('Cannot run test on windows');
  67. }
  68. if (ini_get('open_basedir')) {
  69. $this->markTestSkipped('Cannot test when open_basedir is set');
  70. }
  71. $this->iniSet('open_basedir', \dirname(PHP_BINARY).PATH_SEPARATOR.'/');
  72. $finder = new ExecutableFinder();
  73. $result = $finder->find($this->getPhpBinaryName());
  74. $this->assertSamePath(PHP_BINARY, $result);
  75. }
  76. public function testFindProcessInOpenBasedir()
  77. {
  78. if (ini_get('open_basedir')) {
  79. $this->markTestSkipped('Cannot test when open_basedir is set');
  80. }
  81. if ('\\' === \DIRECTORY_SEPARATOR) {
  82. $this->markTestSkipped('Cannot run test on windows');
  83. }
  84. $this->setPath('');
  85. $this->iniSet('open_basedir', PHP_BINARY.PATH_SEPARATOR.'/');
  86. $finder = new ExecutableFinder();
  87. $result = $finder->find($this->getPhpBinaryName(), false);
  88. $this->assertSamePath(PHP_BINARY, $result);
  89. }
  90. /**
  91. * @requires PHP 5.4
  92. */
  93. public function testFindBatchExecutableOnWindows()
  94. {
  95. if (ini_get('open_basedir')) {
  96. $this->markTestSkipped('Cannot test when open_basedir is set');
  97. }
  98. if ('\\' !== \DIRECTORY_SEPARATOR) {
  99. $this->markTestSkipped('Can be only tested on windows');
  100. }
  101. $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
  102. touch($target);
  103. touch($target.'.BAT');
  104. $this->assertFalse(is_executable($target));
  105. $this->setPath(sys_get_temp_dir());
  106. $finder = new ExecutableFinder();
  107. $result = $finder->find(basename($target), false);
  108. unlink($target);
  109. unlink($target.'.BAT');
  110. $this->assertSamePath($target.'.BAT', $result);
  111. }
  112. private function assertSamePath($expected, $tested)
  113. {
  114. if ('\\' === \DIRECTORY_SEPARATOR) {
  115. $this->assertEquals(strtolower($expected), strtolower($tested));
  116. } else {
  117. $this->assertEquals($expected, $tested);
  118. }
  119. }
  120. private function getPhpBinaryName()
  121. {
  122. return basename(PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
  123. }
  124. }