菜谱项目

ConfigurationTest.php 8.3KB

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