ExecutableFinderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  32. * @requires PHP 5.4
  33. */
  34. public function testFind()
  35. {
  36. if (ini_get('open_basedir')) {
  37. $this->markTestSkipped('Cannot test when open_basedir is set');
  38. }
  39. $this->setPath(\dirname(PHP_BINARY));
  40. $finder = new ExecutableFinder();
  41. $result = $finder->find($this->getPhpBinaryName());
  42. $this->assertSamePath(PHP_BINARY, $result);
  43. }
  44. public function testFindWithDefault()
  45. {
  46. if (ini_get('open_basedir')) {
  47. $this->markTestSkipped('Cannot test when open_basedir is set');
  48. }
  49. $expected = 'defaultValue';
  50. $this->setPath('');
  51. $finder = new ExecutableFinder();
  52. $result = $finder->find('foo', $expected);
  53. $this->assertEquals($expected, $result);
  54. }
  55. /**
  56. * @requires PHP 5.4
  57. */
  58. public function testFindWithExtraDirs()
  59. {
  60. if (ini_get('open_basedir')) {
  61. $this->markTestSkipped('Cannot test when open_basedir is set');
  62. }
  63. $this->setPath('');
  64. $extraDirs = array(\dirname(PHP_BINARY));
  65. $finder = new ExecutableFinder();
  66. $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
  67. $this->assertSamePath(PHP_BINARY, $result);
  68. }
  69. /**
  70. * @requires PHP 5.4
  71. */
  72. public function testFindWithOpenBaseDir()
  73. {
  74. if ('\\' === \DIRECTORY_SEPARATOR) {
  75. $this->markTestSkipped('Cannot run test on windows');
  76. }
  77. if (ini_get('open_basedir')) {
  78. $this->markTestSkipped('Cannot test when open_basedir is set');
  79. }
  80. $this->iniSet('open_basedir', \dirname(PHP_BINARY).(!\defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
  81. $finder = new ExecutableFinder();
  82. $result = $finder->find($this->getPhpBinaryName());
  83. $this->assertSamePath(PHP_BINARY, $result);
  84. }
  85. /**
  86. * @requires PHP 5.4
  87. */
  88. public function testFindProcessInOpenBasedir()
  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('Cannot run test on windows');
  95. }
  96. $this->setPath('');
  97. $this->iniSet('open_basedir', PHP_BINARY.(!\defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
  98. $finder = new ExecutableFinder();
  99. $result = $finder->find($this->getPhpBinaryName(), false);
  100. $this->assertSamePath(PHP_BINARY, $result);
  101. }
  102. /**
  103. * @requires PHP 5.4
  104. */
  105. public function testFindBatchExecutableOnWindows()
  106. {
  107. if (ini_get('open_basedir')) {
  108. $this->markTestSkipped('Cannot test when open_basedir is set');
  109. }
  110. if ('\\' !== \DIRECTORY_SEPARATOR) {
  111. $this->markTestSkipped('Can be only tested on windows');
  112. }
  113. $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
  114. touch($target);
  115. touch($target.'.BAT');
  116. $this->assertFalse(is_executable($target));
  117. $this->setPath(sys_get_temp_dir());
  118. $finder = new ExecutableFinder();
  119. $result = $finder->find(basename($target), false);
  120. unlink($target);
  121. unlink($target.'.BAT');
  122. $this->assertSamePath($target.'.BAT', $result);
  123. }
  124. private function assertSamePath($expected, $tested)
  125. {
  126. if ('\\' === \DIRECTORY_SEPARATOR) {
  127. $this->assertEquals(strtolower($expected), strtolower($tested));
  128. } else {
  129. $this->assertEquals($expected, $tested);
  130. }
  131. }
  132. private function getPhpBinaryName()
  133. {
  134. return basename(PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
  135. }
  136. }