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