4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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\ExecutionLoop\Loop;
17 use Psy\Output\PassthruPager;
18 use Psy\VersionUpdater\GitHubChecker;
19 use Symfony\Component\Console\Output\ConsoleOutput;
21 class ConfigurationTest extends \PHPUnit_Framework_TestCase
23 private function getConfig($configFile = null)
25 return new Configuration(array(
26 'configFile' => $configFile ?: __DIR__ . '/../../fixtures/empty.php',
30 public function testDefaults()
32 $config = $this->getConfig();
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());
43 public function testGettersAndSetters()
45 $config = $this->getConfig();
47 $this->assertNull($config->getDataDir());
48 $config->setDataDir('wheee');
49 $this->assertEquals('wheee', $config->getDataDir());
51 $this->assertNull($config->getConfigDir());
52 $config->setConfigDir('wheee');
53 $this->assertEquals('wheee', $config->getConfigDir());
57 * @dataProvider directories
59 public function testFilesAndDirectories($home, $configFile, $historyFile, $manualDbFile)
61 $oldHome = getenv('HOME');
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()));
69 putenv("HOME=$oldHome");
72 public function directories()
74 $base = realpath(__DIR__ . '/../../fixtures');
79 $base . '/default/.config/psysh/config.php',
80 $base . '/default/.config/psysh/psysh_history',
81 $base . '/default/.local/share/psysh/php_manual.sqlite',
85 $base . '/legacy/.psysh/rc.php',
86 $base . '/legacy/.psysh/history',
87 $base . '/legacy/.psysh/php_manual.sqlite',
91 $base . '/mixed/.psysh/config.php',
92 $base . '/mixed/.psysh/psysh_history',
98 public function testLoadConfig()
100 $config = $this->getConfig();
101 $cleaner = new CodeCleaner();
102 $pager = new PassthruPager(new ConsoleOutput());
103 $loop = new Loop($config);
105 $config->loadConfig(array(
106 'useReadline' => false,
108 'codeCleaner' => $cleaner,
111 'requireSemicolons' => true,
112 'errorLoggingLevel' => E_ERROR | E_WARNING,
113 'colorMode' => Configuration::COLOR_MODE_FORCED,
114 'startupMessage' => 'Psysh is awesome!',
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());
128 public function testLoadConfigFile()
130 $config = $this->getConfig(__DIR__ . '/../../fixtures/config.php');
132 $runtimeDir = $this->joinPath(realpath(sys_get_temp_dir()), 'psysh_test', 'withconfig', 'temp');
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()));
138 $this->assertEquals(function_exists('readline'), $config->useReadline());
139 $this->assertFalse($config->usePcntl());
140 $this->assertEquals(E_ALL & ~E_NOTICE, $config->errorLoggingLevel());
143 public function testLoadLocalConfigFile()
145 $oldPwd = getenv('PWD');
146 putenv('PWD=' . realpath(__DIR__ . '/../../fixtures/project/'));
148 $config = new Configuration();
150 // When no configuration file is specified local project config is merged
151 $this->assertFalse($config->useReadline());
152 $this->assertTrue($config->usePcntl());
154 $config = new Configuration(array('configFile' => __DIR__ . '/../../fixtures/config.php'));
156 // Defining a configuration file skips loading local project config
157 $this->assertTrue($config->useReadline());
158 $this->assertFalse($config->usePcntl());
160 putenv("PWD=$oldPwd");
164 * @expectedException \Psy\Exception\DeprecatedException
166 public function testBaseDirConfigIsDeprecated()
168 $config = new Configuration(array('baseDir' => 'fake'));
171 private function joinPath()
173 return implode(DIRECTORY_SEPARATOR, func_get_args());
176 public function testConfigIncludes()
178 $config = new Configuration(array(
179 'defaultIncludes' => array('/file.php'),
180 'configFile' => __DIR__ . '/../../fixtures/empty.php',
183 $includes = $config->getDefaultIncludes();
184 $this->assertCount(1, $includes);
185 $this->assertEquals('/file.php', $includes[0]);
188 public function testGetOutput()
190 $config = $this->getConfig();
191 $output = $config->getOutput();
193 $this->assertInstanceOf('\Psy\Output\ShellOutput', $output);
196 public function getOutputDecoratedProvider()
201 Configuration::COLOR_MODE_AUTO,
205 Configuration::COLOR_MODE_FORCED,
209 Configuration::COLOR_MODE_DISABLED,
214 /** @dataProvider getOutputDecoratedProvider */
215 public function testGetOutputDecorated($expectation, $colorMode)
217 $config = $this->getConfig();
218 $config->setColorMode($colorMode);
220 $this->assertSame($expectation, $config->getOutputDecorated());
223 public function setColorModeValidProvider()
226 'auto' => array(Configuration::COLOR_MODE_AUTO),
227 'forced' => array(Configuration::COLOR_MODE_FORCED),
228 'disabled' => array(Configuration::COLOR_MODE_DISABLED),
232 /** @dataProvider setColorModeValidProvider */
233 public function testSetColorModeValid($colorMode)
235 $config = $this->getConfig();
236 $config->setColorMode($colorMode);
238 $this->assertEquals($colorMode, $config->colorMode());
241 public function testSetColorModeInvalid()
243 $config = $this->getConfig();
244 $colorMode = 'some invalid mode';
246 $this->setExpectedException(
247 '\InvalidArgumentException',
248 'invalid color mode: some invalid mode'
250 $config->setColorMode($colorMode);
253 public function testSetCheckerValid()
255 $config = $this->getConfig();
256 $checker = new GitHubChecker();
258 $config->setChecker($checker);
260 $this->assertSame($checker, $config->getChecker());