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);
41 public function testFind()
43 if (ini_get('open_basedir')) {
44 $this->markTestSkipped('Cannot test when open_basedir is set');
47 $this->setPath(dirname(PHP_BINARY));
49 $finder = new ExecutableFinder();
50 $result = $finder->find($this->getPhpBinaryName());
52 $this->assertSamePath(PHP_BINARY, $result);
55 public function testFindWithDefault()
57 if (ini_get('open_basedir')) {
58 $this->markTestSkipped('Cannot test when open_basedir is set');
61 $expected = 'defaultValue';
65 $finder = new ExecutableFinder();
66 $result = $finder->find('foo', $expected);
68 $this->assertEquals($expected, $result);
74 public function testFindWithExtraDirs()
76 if (ini_get('open_basedir')) {
77 $this->markTestSkipped('Cannot test when open_basedir is set');
82 $extraDirs = array(dirname(PHP_BINARY));
84 $finder = new ExecutableFinder();
85 $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
87 $this->assertSamePath(PHP_BINARY, $result);
93 public function testFindWithOpenBaseDir()
95 if ('\\' === DIRECTORY_SEPARATOR) {
96 $this->markTestSkipped('Cannot run test on windows');
99 if (ini_get('open_basedir')) {
100 $this->markTestSkipped('Cannot test when open_basedir is set');
103 $this->iniSet('open_basedir', dirname(PHP_BINARY).(!defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
105 $finder = new ExecutableFinder();
106 $result = $finder->find($this->getPhpBinaryName());
108 $this->assertSamePath(PHP_BINARY, $result);
114 public function testFindProcessInOpenBasedir()
116 if (ini_get('open_basedir')) {
117 $this->markTestSkipped('Cannot test when open_basedir is set');
119 if ('\\' === DIRECTORY_SEPARATOR) {
120 $this->markTestSkipped('Cannot run test on windows');
124 $this->iniSet('open_basedir', PHP_BINARY.(!defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
126 $finder = new ExecutableFinder();
127 $result = $finder->find($this->getPhpBinaryName(), false);
129 $this->assertSamePath(PHP_BINARY, $result);
132 private function assertSamePath($expected, $tested)
134 if ('\\' === DIRECTORY_SEPARATOR) {
135 $this->assertEquals(strtolower($expected), strtolower($tested));
137 $this->assertEquals($expected, $tested);
141 private function getPhpBinaryName()
143 return basename(PHP_BINARY, '\\' === DIRECTORY_SEPARATOR ? '.exe' : '');