4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Process\ExecutableFinder;
18 * @author Chris Smith <chris@cs278.org>
20 class ExecutableFinderTest extends TestCase
24 protected function tearDown()
27 // Restore path if it was changed.
28 putenv('PATH='.$this->path);
32 private function setPath($path)
34 $this->path = getenv('PATH');
35 putenv('PATH='.$path);
38 public function testFind()
40 if (ini_get('open_basedir')) {
41 $this->markTestSkipped('Cannot test when open_basedir is set');
44 $this->setPath(\dirname(PHP_BINARY));
46 $finder = new ExecutableFinder();
47 $result = $finder->find($this->getPhpBinaryName());
49 $this->assertSamePath(PHP_BINARY, $result);
52 public function testFindWithDefault()
54 if (ini_get('open_basedir')) {
55 $this->markTestSkipped('Cannot test when open_basedir is set');
58 $expected = 'defaultValue';
62 $finder = new ExecutableFinder();
63 $result = $finder->find('foo', $expected);
65 $this->assertEquals($expected, $result);
68 public function testFindWithExtraDirs()
70 if (ini_get('open_basedir')) {
71 $this->markTestSkipped('Cannot test when open_basedir is set');
76 $extraDirs = array(\dirname(PHP_BINARY));
78 $finder = new ExecutableFinder();
79 $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
81 $this->assertSamePath(PHP_BINARY, $result);
84 public function testFindWithOpenBaseDir()
86 if ('\\' === \DIRECTORY_SEPARATOR) {
87 $this->markTestSkipped('Cannot run test on windows');
90 if (ini_get('open_basedir')) {
91 $this->markTestSkipped('Cannot test when open_basedir is set');
94 $this->iniSet('open_basedir', \dirname(PHP_BINARY).(!\defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
96 $finder = new ExecutableFinder();
97 $result = $finder->find($this->getPhpBinaryName());
99 $this->assertSamePath(PHP_BINARY, $result);
102 public function testFindProcessInOpenBasedir()
104 if (ini_get('open_basedir')) {
105 $this->markTestSkipped('Cannot test when open_basedir is set');
107 if ('\\' === \DIRECTORY_SEPARATOR) {
108 $this->markTestSkipped('Cannot run test on windows');
112 $this->iniSet('open_basedir', PHP_BINARY.(!\defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
114 $finder = new ExecutableFinder();
115 $result = $finder->find($this->getPhpBinaryName(), false);
117 $this->assertSamePath(PHP_BINARY, $result);
123 public function testFindBatchExecutableOnWindows()
125 if (ini_get('open_basedir')) {
126 $this->markTestSkipped('Cannot test when open_basedir is set');
128 if ('\\' !== \DIRECTORY_SEPARATOR) {
129 $this->markTestSkipped('Can be only tested on windows');
132 $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
135 touch($target.'.BAT');
137 $this->assertFalse(is_executable($target));
139 $this->setPath(sys_get_temp_dir());
141 $finder = new ExecutableFinder();
142 $result = $finder->find(basename($target), false);
145 unlink($target.'.BAT');
147 $this->assertSamePath($target.'.BAT', $result);
150 private function assertSamePath($expected, $tested)
152 if ('\\' === \DIRECTORY_SEPARATOR) {
153 $this->assertEquals(strtolower($expected), strtolower($tested));
155 $this->assertEquals($expected, $tested);
159 private function getPhpBinaryName()
161 return basename(PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');