ee25eda02b453d2ca6182d0cf9caf3cff295f659
[yaffs-website] / vendor / symfony / process / Tests / ExecutableFinderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Process\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Process\ExecutableFinder;
16
17 /**
18  * @author Chris Smith <chris@cs278.org>
19  */
20 class ExecutableFinderTest extends TestCase
21 {
22     private $path;
23
24     protected function tearDown()
25     {
26         if ($this->path) {
27             // Restore path if it was changed.
28             putenv('PATH='.$this->path);
29         }
30     }
31
32     private function setPath($path)
33     {
34         $this->path = getenv('PATH');
35         putenv('PATH='.$path);
36     }
37
38     public function testFind()
39     {
40         if (ini_get('open_basedir')) {
41             $this->markTestSkipped('Cannot test when open_basedir is set');
42         }
43
44         $this->setPath(\dirname(PHP_BINARY));
45
46         $finder = new ExecutableFinder();
47         $result = $finder->find($this->getPhpBinaryName());
48
49         $this->assertSamePath(PHP_BINARY, $result);
50     }
51
52     public function testFindWithDefault()
53     {
54         if (ini_get('open_basedir')) {
55             $this->markTestSkipped('Cannot test when open_basedir is set');
56         }
57
58         $expected = 'defaultValue';
59
60         $this->setPath('');
61
62         $finder = new ExecutableFinder();
63         $result = $finder->find('foo', $expected);
64
65         $this->assertEquals($expected, $result);
66     }
67
68     public function testFindWithExtraDirs()
69     {
70         if (ini_get('open_basedir')) {
71             $this->markTestSkipped('Cannot test when open_basedir is set');
72         }
73
74         $this->setPath('');
75
76         $extraDirs = array(\dirname(PHP_BINARY));
77
78         $finder = new ExecutableFinder();
79         $result = $finder->find($this->getPhpBinaryName(), null, $extraDirs);
80
81         $this->assertSamePath(PHP_BINARY, $result);
82     }
83
84     public function testFindWithOpenBaseDir()
85     {
86         if ('\\' === \DIRECTORY_SEPARATOR) {
87             $this->markTestSkipped('Cannot run test on windows');
88         }
89
90         if (ini_get('open_basedir')) {
91             $this->markTestSkipped('Cannot test when open_basedir is set');
92         }
93
94         $this->iniSet('open_basedir', \dirname(PHP_BINARY).(!\defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
95
96         $finder = new ExecutableFinder();
97         $result = $finder->find($this->getPhpBinaryName());
98
99         $this->assertSamePath(PHP_BINARY, $result);
100     }
101
102     public function testFindProcessInOpenBasedir()
103     {
104         if (ini_get('open_basedir')) {
105             $this->markTestSkipped('Cannot test when open_basedir is set');
106         }
107         if ('\\' === \DIRECTORY_SEPARATOR) {
108             $this->markTestSkipped('Cannot run test on windows');
109         }
110
111         $this->setPath('');
112         $this->iniSet('open_basedir', PHP_BINARY.(!\defined('HHVM_VERSION') || HHVM_VERSION_ID >= 30800 ? PATH_SEPARATOR.'/' : ''));
113
114         $finder = new ExecutableFinder();
115         $result = $finder->find($this->getPhpBinaryName(), false);
116
117         $this->assertSamePath(PHP_BINARY, $result);
118     }
119
120     /**
121      * @requires PHP 5.4
122      */
123     public function testFindBatchExecutableOnWindows()
124     {
125         if (ini_get('open_basedir')) {
126             $this->markTestSkipped('Cannot test when open_basedir is set');
127         }
128         if ('\\' !== \DIRECTORY_SEPARATOR) {
129             $this->markTestSkipped('Can be only tested on windows');
130         }
131
132         $target = tempnam(sys_get_temp_dir(), 'example-windows-executable');
133
134         touch($target);
135         touch($target.'.BAT');
136
137         $this->assertFalse(is_executable($target));
138
139         $this->setPath(sys_get_temp_dir());
140
141         $finder = new ExecutableFinder();
142         $result = $finder->find(basename($target), false);
143
144         unlink($target);
145         unlink($target.'.BAT');
146
147         $this->assertSamePath($target.'.BAT', $result);
148     }
149
150     private function assertSamePath($expected, $tested)
151     {
152         if ('\\' === \DIRECTORY_SEPARATOR) {
153             $this->assertEquals(strtolower($expected), strtolower($tested));
154         } else {
155             $this->assertEquals($expected, $tested);
156         }
157     }
158
159     private function getPhpBinaryName()
160     {
161         return basename(PHP_BINARY, '\\' === \DIRECTORY_SEPARATOR ? '.exe' : '');
162     }
163 }