No Description

ConfigurationTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of Psy Shell
  4. *
  5. * (c) 2012-2014 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\Configuration;
  12. use Psy\CodeCleaner;
  13. use Psy\Output\PassthruPager;
  14. use Psy\ExecutionLoop\Loop;
  15. use Symfony\Component\Console\Output\ConsoleOutput;
  16. class ConfigurationTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testDefaults()
  19. {
  20. $config = new Configuration();
  21. $this->assertEquals(function_exists('readline'), $config->hasReadline());
  22. $this->assertEquals(function_exists('readline'), $config->useReadline());
  23. $this->assertEquals(function_exists('pcntl_signal'), $config->hasPcntl());
  24. $this->assertEquals(function_exists('pcntl_signal'), $config->usePcntl());
  25. $this->assertFalse($config->requireSemicolons());
  26. }
  27. public function testGettersAndSetters()
  28. {
  29. $config = new Configuration();
  30. $this->assertNull($config->getDataDir());
  31. $config->setDataDir('wheee');
  32. $this->assertEquals('wheee', $config->getDataDir());
  33. $this->assertNull($config->getConfigDir());
  34. $config->setConfigDir('wheee');
  35. $this->assertEquals('wheee', $config->getConfigDir());
  36. }
  37. /**
  38. * @dataProvider directories
  39. */
  40. public function testFilesAndDirectories($home, $configFile, $historyFile, $manualDbFile)
  41. {
  42. $oldHome = getenv('HOME');
  43. putenv("HOME=$home");
  44. $config = new Configuration();
  45. $this->assertEquals(realpath($configFile), realpath($config->getConfigFile()));
  46. $this->assertEquals(realpath($historyFile), realpath($config->getHistoryFile()));
  47. $this->assertEquals(realpath($manualDbFile), realpath($config->getManualDbFile()));
  48. putenv("HOME=$oldHome");
  49. }
  50. public function directories()
  51. {
  52. $base = realpath(__DIR__ . '/../../fixtures');
  53. return array(
  54. array(
  55. $base . '/default',
  56. $base . '/default/.config/psysh/config.php',
  57. $base . '/default/.config/psysh/psysh_history',
  58. $base . '/default/.local/share/psysh/php_manual.sqlite',
  59. ),
  60. array(
  61. $base . '/legacy',
  62. $base . '/legacy/.psysh/rc.php',
  63. $base . '/legacy/.psysh/history',
  64. $base . '/legacy/.psysh/php_manual.sqlite',
  65. ),
  66. array(
  67. $base . '/mixed',
  68. $base . '/mixed/.psysh/config.php',
  69. $base . '/mixed/.psysh/psysh_history',
  70. null,
  71. ),
  72. );
  73. }
  74. public function testLoadConfig()
  75. {
  76. $config = new Configuration();
  77. $cleaner = new CodeCleaner();
  78. $pager = new PassthruPager(new ConsoleOutput());
  79. $loop = new Loop($config);
  80. $config->loadConfig(array(
  81. 'useReadline' => false,
  82. 'usePcntl' => false,
  83. 'codeCleaner' => $cleaner,
  84. 'pager' => $pager,
  85. 'loop' => $loop,
  86. 'requireSemicolons' => true,
  87. ));
  88. $this->assertFalse($config->useReadline());
  89. $this->assertFalse($config->usePcntl());
  90. $this->assertSame($cleaner, $config->getCodeCleaner());
  91. $this->assertSame($pager, $config->getPager());
  92. $this->assertSame($loop, $config->getLoop());
  93. $this->assertTrue($config->requireSemicolons());
  94. }
  95. public function testLoadConfigFile()
  96. {
  97. $config = new Configuration(array('configFile' => __DIR__ . '/../../fixtures/config.php'));
  98. $runtimeDir = $this->joinPath(realpath(sys_get_temp_dir()), 'psysh_test', 'withconfig', 'temp');
  99. $this->assertStringStartsWith($runtimeDir, realpath($config->getTempFile('foo', 123)));
  100. $this->assertStringStartsWith($runtimeDir, realpath(dirname($config->getPipe('pipe', 123))));
  101. // This will be deprecated, but we want to actually test the value.
  102. $was = error_reporting(error_reporting() & ~E_USER_DEPRECATED);
  103. $this->assertStringStartsWith($runtimeDir, realpath($config->getTempDir()));
  104. error_reporting($was);
  105. $this->assertStringStartsWith($runtimeDir, realpath($config->getRuntimeDir()));
  106. $this->assertEquals(function_exists('readline'), $config->useReadline());
  107. $this->assertFalse($config->usePcntl());
  108. }
  109. /**
  110. * @expectedException PHPUnit_Framework_Error_Deprecated
  111. */
  112. public function testSetTempDirIsDeprecated()
  113. {
  114. $config = new Configuration();
  115. $config->setTempDir('fake');
  116. }
  117. /**
  118. * @expectedException PHPUnit_Framework_Error_Deprecated
  119. */
  120. public function testGetTempDirIsDeprecated()
  121. {
  122. $config = new Configuration();
  123. $config->getTempDir();
  124. }
  125. /**
  126. * @expectedException PHPUnit_Framework_Error_Deprecated
  127. */
  128. public function testBaseDirConfigIsDeprecated()
  129. {
  130. $config = new Configuration(array('baseDir' => 'fake'));
  131. }
  132. private function joinPath()
  133. {
  134. return implode(DIRECTORY_SEPARATOR, func_get_args());
  135. }
  136. public function testConfigIncludes()
  137. {
  138. $config = new Configuration(array(
  139. 'defaultIncludes' => array('/file.php'),
  140. 'configFile' => __DIR__ . '/../../fixtures/empty.php',
  141. ));
  142. $includes = $config->getDefaultIncludes();
  143. $this->assertCount(1, $includes);
  144. $this->assertEquals('/file.php', $includes[0]);
  145. }
  146. }