99604a6ddfbe1e73270b29795cd5f69dd7c8ecd9
[yaffs-website] / vendor / psy / psysh / test / Psy / Test / ConfigurationTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
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 Psy\Test;
13
14 use Psy\CodeCleaner;
15 use Psy\Configuration;
16 use Psy\ExecutionLoop\Loop;
17 use Psy\Output\PassthruPager;
18 use Psy\VersionUpdater\GitHubChecker;
19 use Symfony\Component\Console\Output\ConsoleOutput;
20
21 class ConfigurationTest extends \PHPUnit_Framework_TestCase
22 {
23     private function getConfig($configFile = null)
24     {
25         return new Configuration(array(
26             'configFile' => $configFile ?: __DIR__ . '/../../fixtures/empty.php',
27         ));
28     }
29
30     public function testDefaults()
31     {
32         $config = $this->getConfig();
33
34         $this->assertEquals(function_exists('readline'), $config->hasReadline());
35         $this->assertEquals(function_exists('readline'), $config->useReadline());
36         $this->assertEquals(function_exists('pcntl_signal'), $config->hasPcntl());
37         $this->assertEquals(function_exists('pcntl_signal'), $config->usePcntl());
38         $this->assertFalse($config->requireSemicolons());
39         $this->assertSame(Configuration::COLOR_MODE_AUTO, $config->colorMode());
40         $this->assertNull($config->getStartupMessage());
41     }
42
43     public function testGettersAndSetters()
44     {
45         $config = $this->getConfig();
46
47         $this->assertNull($config->getDataDir());
48         $config->setDataDir('wheee');
49         $this->assertEquals('wheee', $config->getDataDir());
50
51         $this->assertNull($config->getConfigDir());
52         $config->setConfigDir('wheee');
53         $this->assertEquals('wheee', $config->getConfigDir());
54     }
55
56     /**
57      * @dataProvider directories
58      */
59     public function testFilesAndDirectories($home, $configFile, $historyFile, $manualDbFile)
60     {
61         $oldHome = getenv('HOME');
62         putenv("HOME=$home");
63
64         $config = new Configuration();
65         $this->assertEquals(realpath($configFile),   realpath($config->getConfigFile()));
66         $this->assertEquals(realpath($historyFile),  realpath($config->getHistoryFile()));
67         $this->assertEquals(realpath($manualDbFile), realpath($config->getManualDbFile()));
68
69         putenv("HOME=$oldHome");
70     }
71
72     public function directories()
73     {
74         $base = realpath(__DIR__ . '/../../fixtures');
75
76         return array(
77             array(
78                 $base . '/default',
79                 $base . '/default/.config/psysh/config.php',
80                 $base . '/default/.config/psysh/psysh_history',
81                 $base . '/default/.local/share/psysh/php_manual.sqlite',
82             ),
83             array(
84                 $base . '/legacy',
85                 $base . '/legacy/.psysh/rc.php',
86                 $base . '/legacy/.psysh/history',
87                 $base . '/legacy/.psysh/php_manual.sqlite',
88             ),
89             array(
90                 $base . '/mixed',
91                 $base . '/mixed/.psysh/config.php',
92                 $base . '/mixed/.psysh/psysh_history',
93                 null,
94             ),
95         );
96     }
97
98     public function testLoadConfig()
99     {
100         $config  = $this->getConfig();
101         $cleaner = new CodeCleaner();
102         $pager   = new PassthruPager(new ConsoleOutput());
103         $loop    = new Loop($config);
104
105         $config->loadConfig(array(
106             'useReadline'       => false,
107             'usePcntl'          => false,
108             'codeCleaner'       => $cleaner,
109             'pager'             => $pager,
110             'loop'              => $loop,
111             'requireSemicolons' => true,
112             'errorLoggingLevel' => E_ERROR | E_WARNING,
113             'colorMode'         => Configuration::COLOR_MODE_FORCED,
114             'startupMessage'    => 'Psysh is awesome!',
115         ));
116
117         $this->assertFalse($config->useReadline());
118         $this->assertFalse($config->usePcntl());
119         $this->assertSame($cleaner, $config->getCodeCleaner());
120         $this->assertSame($pager, $config->getPager());
121         $this->assertSame($loop, $config->getLoop());
122         $this->assertTrue($config->requireSemicolons());
123         $this->assertEquals(E_ERROR | E_WARNING, $config->errorLoggingLevel());
124         $this->assertSame(Configuration::COLOR_MODE_FORCED, $config->colorMode());
125         $this->assertSame('Psysh is awesome!', $config->getStartupMessage());
126     }
127
128     public function testLoadConfigFile()
129     {
130         $config = $this->getConfig(__DIR__ . '/../../fixtures/config.php');
131
132         $runtimeDir = $this->joinPath(realpath(sys_get_temp_dir()), 'psysh_test', 'withconfig', 'temp');
133
134         $this->assertStringStartsWith($runtimeDir, realpath($config->getTempFile('foo', 123)));
135         $this->assertStringStartsWith($runtimeDir, realpath(dirname($config->getPipe('pipe', 123))));
136         $this->assertStringStartsWith($runtimeDir, realpath($config->getRuntimeDir()));
137
138         $this->assertEquals(function_exists('readline'), $config->useReadline());
139         $this->assertFalse($config->usePcntl());
140         $this->assertEquals(E_ALL & ~E_NOTICE, $config->errorLoggingLevel());
141     }
142
143     public function testLoadLocalConfigFile()
144     {
145         $oldPwd = getenv('PWD');
146         putenv('PWD=' . realpath(__DIR__ . '/../../fixtures/project/'));
147
148         $config = new Configuration();
149
150         // When no configuration file is specified local project config is merged
151         $this->assertFalse($config->useReadline());
152         $this->assertTrue($config->usePcntl());
153
154         $config = new Configuration(array('configFile' => __DIR__ . '/../../fixtures/config.php'));
155
156         // Defining a configuration file skips loading local project config
157         $this->assertTrue($config->useReadline());
158         $this->assertFalse($config->usePcntl());
159
160         putenv("PWD=$oldPwd");
161     }
162
163     /**
164      * @expectedException \Psy\Exception\DeprecatedException
165      */
166     public function testBaseDirConfigIsDeprecated()
167     {
168         $config = new Configuration(array('baseDir' => 'fake'));
169     }
170
171     private function joinPath()
172     {
173         return implode(DIRECTORY_SEPARATOR, func_get_args());
174     }
175
176     public function testConfigIncludes()
177     {
178         $config = new Configuration(array(
179             'defaultIncludes' => array('/file.php'),
180             'configFile'      => __DIR__ . '/../../fixtures/empty.php',
181         ));
182
183         $includes = $config->getDefaultIncludes();
184         $this->assertCount(1, $includes);
185         $this->assertEquals('/file.php', $includes[0]);
186     }
187
188     public function testGetOutput()
189     {
190         $config = $this->getConfig();
191         $output = $config->getOutput();
192
193         $this->assertInstanceOf('\Psy\Output\ShellOutput', $output);
194     }
195
196     public function getOutputDecoratedProvider()
197     {
198         return array(
199             'auto' => array(
200                 null,
201                 Configuration::COLOR_MODE_AUTO,
202             ),
203             'forced' => array(
204                 true,
205                 Configuration::COLOR_MODE_FORCED,
206             ),
207             'disabled' => array(
208                 false,
209                 Configuration::COLOR_MODE_DISABLED,
210             ),
211         );
212     }
213
214     /** @dataProvider getOutputDecoratedProvider */
215     public function testGetOutputDecorated($expectation, $colorMode)
216     {
217         $config = $this->getConfig();
218         $config->setColorMode($colorMode);
219
220         $this->assertSame($expectation, $config->getOutputDecorated());
221     }
222
223     public function setColorModeValidProvider()
224     {
225         return array(
226             'auto'     => array(Configuration::COLOR_MODE_AUTO),
227             'forced'   => array(Configuration::COLOR_MODE_FORCED),
228             'disabled' => array(Configuration::COLOR_MODE_DISABLED),
229         );
230     }
231
232     /** @dataProvider setColorModeValidProvider */
233     public function testSetColorModeValid($colorMode)
234     {
235         $config = $this->getConfig();
236         $config->setColorMode($colorMode);
237
238         $this->assertEquals($colorMode, $config->colorMode());
239     }
240
241     public function testSetColorModeInvalid()
242     {
243         $config = $this->getConfig();
244         $colorMode = 'some invalid mode';
245
246         $this->setExpectedException(
247             '\InvalidArgumentException',
248             'invalid color mode: some invalid mode'
249         );
250         $config->setColorMode($colorMode);
251     }
252
253     public function testSetCheckerValid()
254     {
255         $config = $this->getConfig();
256         $checker = new GitHubChecker();
257
258         $config->setChecker($checker);
259
260         $this->assertSame($checker, $config->getChecker());
261     }
262 }