123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- <?php
- /*
- * This file is part of Psy Shell.
- *
- * (c) 2012-2023 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Psy\Test;
- use Psy\CodeCleaner;
- use Psy\Configuration;
- use Psy\ExecutionLoop\ProcessForker;
- use Psy\Output\PassthruPager;
- use Psy\Output\ShellOutput;
- use Psy\VersionUpdater\GitHubChecker;
- use Symfony\Component\Console\Input\InputDefinition;
- use Symfony\Component\Console\Input\StringInput;
- use Symfony\Component\Console\Output\ConsoleOutput;
- use Symfony\Component\Console\Output\OutputInterface;
- class ConfigurationTest extends TestCase
- {
- private function getConfig($configFile = null)
- {
- return new Configuration([
- 'configFile' => $configFile ?: __DIR__.'/fixtures/empty.php',
- ]);
- }
- public function testDefaults()
- {
- $config = $this->getConfig();
- $this->assertSame(\function_exists('readline'), $config->hasReadline());
- $this->assertSame(\function_exists('readline'), $config->useReadline());
- $this->assertSame(ProcessForker::isSupported(), $config->hasPcntl());
- $this->assertSame($config->hasPcntl(), $config->usePcntl());
- $this->assertFalse($config->requireSemicolons());
- $this->assertSame(Configuration::COLOR_MODE_AUTO, $config->colorMode());
- $this->assertNull($config->getStartupMessage());
- }
- public function testGettersAndSetters()
- {
- $config = $this->getConfig();
- $this->assertNull($config->getDataDir());
- $config->setDataDir('wheee');
- $this->assertSame('wheee', $config->getDataDir());
- $this->assertNull($config->getConfigDir());
- $config->setConfigDir('wheee');
- $this->assertSame('wheee', $config->getConfigDir());
- }
- /**
- * @group isolation-fail
- */
- public function testLoadConfig()
- {
- $config = $this->getConfig();
- $cleaner = new CodeCleaner();
- $pager = new PassthruPager(new ConsoleOutput());
- $config->loadConfig([
- 'useReadline' => false,
- 'usePcntl' => false,
- 'codeCleaner' => $cleaner,
- 'pager' => $pager,
- 'requireSemicolons' => true,
- 'errorLoggingLevel' => \E_ERROR | \E_WARNING,
- 'colorMode' => Configuration::COLOR_MODE_FORCED,
- 'startupMessage' => 'Psysh is awesome!',
- ]);
- $this->assertFalse($config->useReadline());
- $this->assertFalse($config->usePcntl());
- $this->assertSame($cleaner, $config->getCodeCleaner());
- $this->assertSame($pager, $config->getPager());
- $this->assertTrue($config->requireSemicolons());
- $this->assertSame(\E_ERROR | \E_WARNING, $config->errorLoggingLevel());
- $this->assertSame(Configuration::COLOR_MODE_FORCED, $config->colorMode());
- $this->assertSame('Psysh is awesome!', $config->getStartupMessage());
- }
- public function testLoadConfigFile()
- {
- $config = $this->getConfig(__DIR__.'/fixtures/config.php');
- $runtimeDir = $this->joinPath(\realpath(\sys_get_temp_dir()), 'psysh_test', 'withconfig', 'temp');
- $this->assertStringStartsWith($runtimeDir, \realpath($config->getTempFile('foo', 123)));
- $this->assertStringStartsWith($runtimeDir, \realpath(\dirname($config->getPipe('pipe', 123))));
- $this->assertStringStartsWith($runtimeDir, \realpath($config->getRuntimeDir()));
- $this->assertSame(\function_exists('readline'), $config->useReadline());
- $this->assertFalse($config->usePcntl());
- $this->assertSame(\E_ALL & ~\E_NOTICE, $config->errorLoggingLevel());
- }
- public function testLoadLocalConfigFile()
- {
- $oldPwd = \getcwd();
- \chdir(\realpath(__DIR__.'/fixtures/project/'));
- $config = new Configuration();
- // When no configuration file is specified local project config is merged
- $this->assertTrue($config->requireSemicolons());
- $this->assertFalse($config->useUnicode());
- $config = new Configuration(['configFile' => __DIR__.'/fixtures/config.php']);
- // Defining a configuration file skips loading local project config
- $this->assertFalse($config->requireSemicolons());
- $this->assertTrue($config->useUnicode());
- \chdir($oldPwd);
- }
- public function testUnknownConfigFileThrowsException()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid configuration file specified');
- $config = new Configuration(['configFile' => __DIR__.'/not/a/real/config.php']);
- $this->fail();
- }
- public function testBaseDirConfigIsDeprecated()
- {
- $this->expectException(\Psy\Exception\DeprecatedException::class);
- $config = new Configuration(['baseDir' => 'fake']);
- $this->fail();
- }
- private function joinPath(...$parts)
- {
- return \implode(\DIRECTORY_SEPARATOR, $parts);
- }
- public function testConfigIncludes()
- {
- $config = new Configuration([
- 'defaultIncludes' => ['/file.php'],
- 'configFile' => __DIR__.'/fixtures/empty.php',
- ]);
- $includes = $config->getDefaultIncludes();
- $this->assertCount(1, $includes);
- $this->assertSame('/file.php', $includes[0]);
- }
- public function testGetOutput()
- {
- $config = $this->getConfig();
- $output = $config->getOutput();
- $this->assertInstanceOf(ShellOutput::class, $output);
- }
- public function getOutputDecoratedProvider()
- {
- return [
- 'auto' => [
- null,
- Configuration::COLOR_MODE_AUTO,
- ],
- 'forced' => [
- true,
- Configuration::COLOR_MODE_FORCED,
- ],
- 'disabled' => [
- false,
- Configuration::COLOR_MODE_DISABLED,
- ],
- ];
- }
- /** @dataProvider getOutputDecoratedProvider */
- public function testGetOutputDecorated($expectation, $colorMode)
- {
- if ($colorMode === Configuration::COLOR_MODE_AUTO) {
- $this->markTestSkipped('This test won\'t work on CI without overriding pipe detection');
- }
- $config = $this->getConfig();
- $config->setColorMode($colorMode);
- $this->assertSame($expectation, $config->getOutputDecorated());
- }
- public function setColorModeValidProvider()
- {
- return [
- 'auto' => [Configuration::COLOR_MODE_AUTO],
- 'forced' => [Configuration::COLOR_MODE_FORCED],
- 'disabled' => [Configuration::COLOR_MODE_DISABLED],
- ];
- }
- /** @dataProvider setColorModeValidProvider */
- public function testSetColorModeValid($colorMode)
- {
- $config = $this->getConfig();
- $config->setColorMode($colorMode);
- $this->assertSame($colorMode, $config->colorMode());
- }
- public function testSetColorModeInvalid()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid color mode: some invalid mode');
- $config = $this->getConfig();
- $config->setColorMode('some invalid mode');
- $this->fail();
- }
- public function getOutputVerbosityProvider()
- {
- return [
- 'quiet' => [OutputInterface::VERBOSITY_QUIET, Configuration::VERBOSITY_QUIET],
- 'normal' => [OutputInterface::VERBOSITY_NORMAL, Configuration::VERBOSITY_NORMAL],
- 'verbose' => [OutputInterface::VERBOSITY_VERBOSE, Configuration::VERBOSITY_VERBOSE],
- 'very_verbose' => [OutputInterface::VERBOSITY_VERY_VERBOSE, Configuration::VERBOSITY_VERY_VERBOSE],
- 'debug' => [OutputInterface::VERBOSITY_DEBUG, Configuration::VERBOSITY_DEBUG],
- ];
- }
- /**
- * @dataProvider getOutputVerbosityProvider
- *
- * @group isolation-fail
- */
- public function testGetOutputVerbosity($expectation, $verbosity)
- {
- $config = $this->getConfig();
- $config->setVerbosity($verbosity);
- $this->assertSame($expectation, $config->getOutputVerbosity());
- }
- public function setVerbosityValidProvider()
- {
- return [
- 'quiet' => [Configuration::VERBOSITY_QUIET],
- 'normal' => [Configuration::VERBOSITY_NORMAL],
- 'verbose' => [Configuration::VERBOSITY_VERBOSE],
- 'very_verbose' => [Configuration::VERBOSITY_VERY_VERBOSE],
- 'debug' => [Configuration::VERBOSITY_DEBUG],
- ];
- }
- /** @dataProvider setVerbosityValidProvider */
- public function testSetVerbosityValid($verbosity)
- {
- $config = $this->getConfig();
- $config->setVerbosity($verbosity);
- $this->assertSame($verbosity, $config->verbosity());
- }
- public function testSetVerbosityInvalid()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid verbosity level: some invalid verbosity');
- $config = $this->getConfig();
- $config->setVerbosity('some invalid verbosity');
- $this->fail();
- }
- public function getInputInteractiveProvider()
- {
- return [
- 'auto' => [
- null,
- Configuration::INTERACTIVE_MODE_AUTO,
- ],
- 'forced' => [
- true,
- Configuration::INTERACTIVE_MODE_FORCED,
- ],
- 'disabled' => [
- false,
- Configuration::INTERACTIVE_MODE_DISABLED,
- ],
- ];
- }
- /** @dataProvider getInputInteractiveProvider */
- public function testGetInputInteractive($expectation, $interactive)
- {
- if ($interactive === Configuration::INTERACTIVE_MODE_AUTO) {
- $this->markTestSkipped('This test won\'t work on CI without overriding pipe detection');
- }
- $config = $this->getConfig();
- $config->setInteractiveMode($interactive);
- $this->assertSame($expectation, $config->getInputInteractive());
- }
- public function setInteractiveModeValidProvider()
- {
- return [
- 'auto' => [Configuration::INTERACTIVE_MODE_AUTO],
- 'forced' => [Configuration::INTERACTIVE_MODE_FORCED],
- 'disabled' => [Configuration::INTERACTIVE_MODE_DISABLED],
- ];
- }
- /** @dataProvider setInteractiveModeValidProvider */
- public function testsetInteractiveModeValid($interactive)
- {
- $config = $this->getConfig();
- $config->setInteractiveMode($interactive);
- $this->assertSame($interactive, $config->interactiveMode());
- }
- public function testsetInteractiveModeInvalid()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid interactive mode: nope');
- $config = $this->getConfig();
- $config->setInteractiveMode('nope');
- $this->fail();
- }
- public function testSetCheckerValid()
- {
- $config = $this->getConfig();
- $checker = new GitHubChecker();
- $config->setChecker($checker);
- $this->assertSame($checker, $config->getChecker());
- }
- public function testSetFormatterStyles()
- {
- $config = $this->getConfig();
- $config->setFormatterStyles([
- 'mario' => ['white', 'red'],
- 'luigi' => ['white', 'green'],
- ]);
- $formatter = $config->getOutput()->getFormatter();
- $this->assertTrue($formatter->hasStyle('mario'));
- $this->assertTrue($formatter->hasStyle('luigi'));
- $mario = $formatter->getStyle('mario');
- $this->assertSame("\e[37;41mwheee\e[39;49m", $mario->apply('wheee'));
- $luigi = $formatter->getStyle('luigi');
- $this->assertSame("\e[37;42mwheee\e[39;49m", $luigi->apply('wheee'));
- }
- public function testSetFormatterStylesRuntimeUpdates()
- {
- $config = $this->getConfig();
- $formatter = $config->getOutput()->getFormatter();
- $this->assertFalse($formatter->hasStyle('mario'));
- $this->assertFalse($formatter->hasStyle('luigi'));
- $config->setFormatterStyles([
- 'mario' => ['white', 'red'],
- 'luigi' => ['white', 'green'],
- ]);
- $this->assertTrue($formatter->hasStyle('mario'));
- $this->assertTrue($formatter->hasStyle('luigi'));
- $mario = $formatter->getStyle('mario');
- $this->assertSame("\e[37;41mwheee\e[39;49m", $mario->apply('wheee'));
- $luigi = $formatter->getStyle('luigi');
- $this->assertSame("\e[37;42mwheee\e[39;49m", $luigi->apply('wheee'));
- $config->setFormatterStyles([
- 'mario' => ['red', 'white'],
- 'luigi' => ['green', 'white'],
- ]);
- $mario = $formatter->getStyle('mario');
- $this->assertSame("\e[31;47mwheee\e[39;49m", $mario->apply('wheee'));
- $luigi = $formatter->getStyle('luigi');
- $this->assertSame("\e[32;47mwheee\e[39;49m", $luigi->apply('wheee'));
- }
- /**
- * @dataProvider invalidStyles
- */
- public function testSetFormatterStylesInvalid($styles, $option)
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid');
- $this->expectExceptionMessage($option);
- $config = $this->getConfig();
- $config->setFormatterStyles($styles);
- $this->fail();
- }
- public function invalidStyles()
- {
- return [
- [
- ['error' => ['burgundy', null, ['bold']]],
- '"burgundy"',
- ],
- [
- ['error' => ['red', 'ink', ['bold']]],
- '"ink"',
- ],
- [
- ['error' => ['black', 'red', ['marquee']]],
- '"marquee"',
- ],
- ];
- }
- /**
- * @dataProvider inputStrings
- *
- * @group isolation-fail
- */
- public function testConfigurationFromInput($inputString, $verbosity, $colorMode, $interactiveMode, $rawOutput, $yolo)
- {
- $input = $this->getBoundStringInput($inputString);
- $config = Configuration::fromInput($input);
- $this->assertSame($verbosity, $config->verbosity());
- $this->assertSame($colorMode, $config->colorMode());
- $this->assertSame($interactiveMode, $config->interactiveMode());
- $this->assertSame($rawOutput, $config->rawOutput());
- $this->assertSame($yolo, $config->yolo());
- $input = $this->getUnboundStringInput($inputString);
- $config = Configuration::fromInput($input);
- $this->assertSame($verbosity, $config->verbosity());
- $this->assertSame($colorMode, $config->colorMode());
- $this->assertSame($interactiveMode, $config->interactiveMode());
- $this->assertSame($rawOutput, $config->rawOutput());
- $this->assertSame($yolo, $config->yolo());
- }
- public function inputStrings()
- {
- return [
- ['', Configuration::VERBOSITY_NORMAL, Configuration::COLOR_MODE_AUTO, Configuration::INTERACTIVE_MODE_AUTO, false, false],
- ['--raw-output --color --interactive --verbose', Configuration::VERBOSITY_VERBOSE, Configuration::COLOR_MODE_FORCED, Configuration::INTERACTIVE_MODE_FORCED, false, false],
- ['--raw-output --no-color --no-interactive --quiet', Configuration::VERBOSITY_QUIET, Configuration::COLOR_MODE_DISABLED, Configuration::INTERACTIVE_MODE_DISABLED, true, false],
- ['--quiet --color --interactive', Configuration::VERBOSITY_QUIET, Configuration::COLOR_MODE_FORCED, Configuration::INTERACTIVE_MODE_FORCED, false, false],
- ['--quiet --yolo', Configuration::VERBOSITY_QUIET, Configuration::COLOR_MODE_AUTO, Configuration::INTERACTIVE_MODE_AUTO, false, true],
- ];
- }
- /**
- * @group isolation-fail
- */
- public function testConfigurationFromInputSpecificity()
- {
- $input = $this->getBoundStringInput('--raw-output --color --interactive --verbose');
- $config = Configuration::fromInput($input);
- $this->assertSame(Configuration::VERBOSITY_VERBOSE, $config->verbosity());
- $this->assertSame(Configuration::COLOR_MODE_FORCED, $config->colorMode());
- $this->assertSame(Configuration::INTERACTIVE_MODE_FORCED, $config->interactiveMode());
- $this->assertFalse($config->rawOutput(), '--raw-output is ignored with interactive input');
- $input = $this->getBoundStringInput('--verbose --quiet --color --no-color --interactive --no-interactive');
- $config = Configuration::fromInput($input);
- $this->assertSame(Configuration::VERBOSITY_QUIET, $config->verbosity(), '--quiet trumps --verbose');
- $this->assertSame(Configuration::COLOR_MODE_FORCED, $config->colorMode(), '--color trumps --no-color');
- $this->assertSame(Configuration::INTERACTIVE_MODE_FORCED, $config->interactiveMode(), '--interactive trumps --no-interactive');
- }
- /**
- * @dataProvider verbosityInputStrings
- *
- * @group isolation-fail
- */
- public function testConfigurationFromInputVerbosityLevels($inputString, $verbosity)
- {
- $input = $this->getBoundStringInput($inputString);
- $config = Configuration::fromInput($input);
- $this->assertSame($verbosity, $config->verbosity());
- $input = $this->getUnboundStringInput($inputString);
- $config = Configuration::fromInput($input);
- $this->assertSame($verbosity, $config->verbosity());
- }
- public function verbosityInputStrings()
- {
- return [
- ['--verbose 0', Configuration::VERBOSITY_NORMAL],
- ['--verbose=0', Configuration::VERBOSITY_NORMAL],
- ['--verbose 1', Configuration::VERBOSITY_VERBOSE],
- ['--verbose=1', Configuration::VERBOSITY_VERBOSE],
- ['-v', Configuration::VERBOSITY_VERBOSE],
- ['--verbose 2', Configuration::VERBOSITY_VERY_VERBOSE],
- ['--verbose=2', Configuration::VERBOSITY_VERY_VERBOSE],
- ['-vv', Configuration::VERBOSITY_VERY_VERBOSE],
- ['--verbose 3', Configuration::VERBOSITY_DEBUG],
- ['--verbose=3', Configuration::VERBOSITY_DEBUG],
- ['-vvv', Configuration::VERBOSITY_DEBUG],
- // no `--verbose -1` because that's not a valid option value :P
- ['--verbose=-1', Configuration::VERBOSITY_QUIET],
- ['--quiet', Configuration::VERBOSITY_QUIET],
- ];
- }
- /**
- * @dataProvider shortInputStrings
- *
- * @group isolation-fail
- */
- public function testConfigurationFromInputShortOptions($inputString, $verbosity, $interactiveMode, $rawOutput, $skipUnbound = false)
- {
- $input = $this->getBoundStringInput($inputString);
- $config = Configuration::fromInput($input);
- $this->assertSame($verbosity, $config->verbosity());
- $this->assertSame($interactiveMode, $config->interactiveMode());
- $this->assertSame($rawOutput, $config->rawOutput());
- if ($skipUnbound) {
- $this->markTestSkipped($inputString.' fails with unbound input');
- }
- $input = $this->getUnboundStringInput($inputString);
- $config = Configuration::fromInput($input);
- $this->assertSame($verbosity, $config->verbosity());
- $this->assertSame($interactiveMode, $config->interactiveMode());
- $this->assertSame($rawOutput, $config->rawOutput());
- }
- public function shortInputStrings()
- {
- return [
- // Can't do `-nrq`-style compact short options with unbound input.
- ['-nrq', Configuration::VERBOSITY_QUIET, Configuration::INTERACTIVE_MODE_DISABLED, true, true],
- ['-n -r -q', Configuration::VERBOSITY_QUIET, Configuration::INTERACTIVE_MODE_DISABLED, true],
- ['-v', Configuration::VERBOSITY_VERBOSE, Configuration::INTERACTIVE_MODE_AUTO, false],
- ['-vv', Configuration::VERBOSITY_VERY_VERBOSE, Configuration::INTERACTIVE_MODE_AUTO, false],
- ['-vvv', Configuration::VERBOSITY_DEBUG, Configuration::INTERACTIVE_MODE_AUTO, false],
- ];
- }
- /**
- * @group isolation-fail
- */
- public function testConfigurationFromInputAliases()
- {
- $input = $this->getBoundStringInput('--ansi --interaction');
- $config = Configuration::fromInput($input);
- $this->assertSame(Configuration::COLOR_MODE_FORCED, $config->colorMode());
- $this->assertSame(Configuration::INTERACTIVE_MODE_FORCED, $config->interactiveMode());
- $input = $this->getBoundStringInput('--no-ansi --no-interaction');
- $config = Configuration::fromInput($input);
- $this->assertSame(Configuration::COLOR_MODE_DISABLED, $config->colorMode());
- $this->assertSame(Configuration::INTERACTIVE_MODE_DISABLED, $config->interactiveMode());
- }
- private function getBoundStringInput($string, $configFile = null)
- {
- $input = $this->getUnboundStringInput($string, $configFile);
- $input->bind(new InputDefinition(Configuration::getInputOptions()));
- return $input;
- }
- private function getUnboundStringInput($string, $configFile = null)
- {
- if ($configFile === null) {
- $configFile = __DIR__.'/fixtures/empty.php';
- }
- return new StringInput($string.' --config '.\escapeshellarg($configFile));
- }
- public function testYoloMode()
- {
- $config = $this->getConfig();
- $this->assertFalse($config->yolo());
- $config->setYolo(true);
- $this->assertTrue($config->yolo());
- // The CodeCleaner will not be updated after the first time we access it:
- $this->assertTrue($config->getCodeCleaner()->yolo());
- }
- }
|