Geen omschrijving

ConfigurationTest.php 8.0KB

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