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