4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 use Psy\Configuration;
16 use Psy\Output\PassthruPager;
17 use Psy\VersionUpdater\GitHubChecker;
18 use Symfony\Component\Console\Output\ConsoleOutput;
20 class ConfigurationTest extends \PHPUnit\Framework\TestCase
22 private function getConfig($configFile = null)
24 return new Configuration([
25 'configFile' => $configFile ?: __DIR__ . '/fixtures/empty.php',
29 public function testDefaults()
31 $config = $this->getConfig();
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());
42 public function testGettersAndSetters()
44 $config = $this->getConfig();
46 $this->assertNull($config->getDataDir());
47 $config->setDataDir('wheee');
48 $this->assertSame('wheee', $config->getDataDir());
50 $this->assertNull($config->getConfigDir());
51 $config->setConfigDir('wheee');
52 $this->assertSame('wheee', $config->getConfigDir());
56 * @dataProvider directories
58 public function testFilesAndDirectories($home, $configFile, $historyFile, $manualDbFile)
60 $oldHome = \getenv('HOME');
61 \putenv("HOME=$home");
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()));
68 \putenv("HOME=$oldHome");
71 public function directories()
73 $base = \realpath(__DIR__ . '/fixtures');
78 $base . '/default/.config/psysh/config.php',
79 $base . '/default/.config/psysh/psysh_history',
80 $base . '/default/.local/share/psysh/php_manual.sqlite',
84 $base . '/legacy/.psysh/rc.php',
85 $base . '/legacy/.psysh/history',
86 $base . '/legacy/.psysh/php_manual.sqlite',
90 $base . '/mixed/.psysh/config.php',
91 $base . '/mixed/.psysh/psysh_history',
97 public function testLoadConfig()
99 $config = $this->getConfig();
100 $cleaner = new CodeCleaner();
101 $pager = new PassthruPager(new ConsoleOutput());
103 $config->loadConfig([
104 'useReadline' => false,
106 'codeCleaner' => $cleaner,
108 'requireSemicolons' => true,
109 'errorLoggingLevel' => E_ERROR | E_WARNING,
110 'colorMode' => Configuration::COLOR_MODE_FORCED,
111 'startupMessage' => 'Psysh is awesome!',
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());
124 public function testLoadConfigFile()
126 $config = $this->getConfig(__DIR__ . '/fixtures/config.php');
128 $runtimeDir = $this->joinPath(\realpath(\sys_get_temp_dir()), 'psysh_test', 'withconfig', 'temp');
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()));
134 $this->assertSame(\function_exists('readline'), $config->useReadline());
135 $this->assertFalse($config->usePcntl());
136 $this->assertSame(E_ALL & ~E_NOTICE, $config->errorLoggingLevel());
139 public function testLoadLocalConfigFile()
142 \chdir(\realpath(__DIR__ . '/fixtures/project/'));
144 $config = new Configuration();
146 // When no configuration file is specified local project config is merged
147 $this->assertTrue($config->requireSemicolons());
148 $this->assertFalse($config->useUnicode());
150 $config = new Configuration(['configFile' => __DIR__ . '/fixtures/config.php']);
152 // Defining a configuration file skips loading local project config
153 $this->assertFalse($config->requireSemicolons());
154 $this->assertTrue($config->useUnicode());
160 * @expectedException \Psy\Exception\DeprecatedException
162 public function testBaseDirConfigIsDeprecated()
164 $config = new Configuration(['baseDir' => 'fake']);
167 private function joinPath()
169 return \implode(DIRECTORY_SEPARATOR, \func_get_args());
172 public function testConfigIncludes()
174 $config = new Configuration([
175 'defaultIncludes' => ['/file.php'],
176 'configFile' => __DIR__ . '/fixtures/empty.php',
179 $includes = $config->getDefaultIncludes();
180 $this->assertCount(1, $includes);
181 $this->assertSame('/file.php', $includes[0]);
184 public function testGetOutput()
186 $config = $this->getConfig();
187 $output = $config->getOutput();
189 $this->assertInstanceOf('Psy\Output\ShellOutput', $output);
192 public function getOutputDecoratedProvider()
197 Configuration::COLOR_MODE_AUTO,
201 Configuration::COLOR_MODE_FORCED,
205 Configuration::COLOR_MODE_DISABLED,
210 /** @dataProvider getOutputDecoratedProvider */
211 public function testGetOutputDecorated($expectation, $colorMode)
213 $config = $this->getConfig();
214 $config->setColorMode($colorMode);
216 $this->assertSame($expectation, $config->getOutputDecorated());
219 public function setColorModeValidProvider()
222 'auto' => [Configuration::COLOR_MODE_AUTO],
223 'forced' => [Configuration::COLOR_MODE_FORCED],
224 'disabled' => [Configuration::COLOR_MODE_DISABLED],
228 /** @dataProvider setColorModeValidProvider */
229 public function testSetColorModeValid($colorMode)
231 $config = $this->getConfig();
232 $config->setColorMode($colorMode);
234 $this->assertSame($colorMode, $config->colorMode());
238 * @expectedException \InvalidArgumentException
239 * @expectedExceptionMessage invalid color mode: some invalid mode
241 public function testSetColorModeInvalid()
243 $config = $this->getConfig();
244 $config->setColorMode('some invalid mode');
247 public function testSetCheckerValid()
249 $config = $this->getConfig();
250 $checker = new GitHubChecker();
252 $config->setChecker($checker);
254 $this->assertSame($checker, $config->getChecker());