123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167 |
- <?php
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Console;
- use PhpCsFixer\Config;
- use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
- use PhpCsFixer\Console\Command\FixCommand;
- use PhpCsFixer\Console\ConfigurationResolver;
- use PhpCsFixer\Finder;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Console\Output\OutputInterface;
- /**
- * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Console\ConfigurationResolver
- */
- final class ConfigurationResolverTest extends TestCase
- {
- /**
- * @var Config
- */
- private $config;
- protected function setUp()
- {
- parent::setUp();
- $this->config = new Config();
- }
- protected function tearDown()
- {
- parent::tearDown();
- unset($this->config);
- }
- public function testSetOptionWithUndefinedOption()
- {
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessageRegExp('/^Unknown option name: "foo"\.$/');
- new ConfigurationResolver(
- $this->config,
- ['foo' => 'bar'],
- ''
- );
- }
- public function testResolveProgressWithPositiveConfigAndPositiveOption()
- {
- $this->config->setHideProgress(true);
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
- ],
- ''
- );
- $this->assertSame('none', $resolver->getProgress());
- }
- public function testResolveProgressWithPositiveConfigAndNegativeOption()
- {
- $this->config->setHideProgress(true);
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_NORMAL,
- ],
- ''
- );
- $this->assertSame('none', $resolver->getProgress());
- }
- public function testResolveProgressWithNegativeConfigAndPositiveOption()
- {
- $this->config->setHideProgress(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
- ],
- ''
- );
- $this->assertSame('run-in', $resolver->getProgress());
- }
- public function testResolveProgressWithNegativeConfigAndNegativeOption()
- {
- $this->config->setHideProgress(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_NORMAL,
- ],
- ''
- );
- $this->assertSame('none', $resolver->getProgress());
- }
- /**
- * @param string $progressType
- *
- * @dataProvider provideProgressTypeCases
- */
- public function testResolveProgressWithPositiveConfigAndExplicitProgress($progressType)
- {
- $this->config->setHideProgress(true);
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
- 'show-progress' => $progressType,
- ],
- ''
- );
- $this->assertSame($progressType, $resolver->getProgress());
- }
- /**
- * @param string $progressType
- *
- * @dataProvider provideProgressTypeCases
- */
- public function testResolveProgressWithNegativeConfigAndExplicitProgress($progressType)
- {
- $this->config->setHideProgress(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
- 'show-progress' => $progressType,
- ],
- ''
- );
- $this->assertSame($progressType, $resolver->getProgress());
- }
- public function provideProgressTypeCases()
- {
- return [
- ['none'],
- ['run-in'],
- ['estimating'],
- ['estimating-max'],
- ];
- }
- public function testResolveProgressWithInvalidExplicitProgress()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'format' => 'txt',
- 'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
- 'show-progress' => 'foo',
- ],
- ''
- );
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessage('The progress type "foo" is not defined, supported are "none", "run-in", "estimating", "estimating-max".');
- $resolver->getProgress();
- }
- public function testResolveConfigFileDefault()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertNull($resolver->getConfigFile());
- $this->assertInstanceOf(\PhpCsFixer\ConfigInterface::class, $resolver->getConfig());
- }
- public function testResolveConfigFileByPathOfFile()
- {
- $dir = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_1';
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [$dir.DIRECTORY_SEPARATOR.'foo.php']],
- ''
- );
- $this->assertSame($dir.DIRECTORY_SEPARATOR.'.php_cs.dist', $resolver->getConfigFile());
- $this->assertInstanceOf('Test1Config', $resolver->getConfig());
- }
- public function testResolveConfigFileSpecified()
- {
- $file = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_4/my.php_cs';
- $resolver = new ConfigurationResolver(
- $this->config,
- ['config' => $file],
- ''
- );
- $this->assertSame($file, $resolver->getConfigFile());
- $this->assertInstanceOf('Test4Config', $resolver->getConfig());
- }
- /**
- * @param string $expectedFile
- * @param string $expectedClass
- * @param string $path
- * @param null|string $cwdPath
- *
- * @dataProvider provideResolveConfigFileDefaultCases
- */
- public function testResolveConfigFileChooseFile($expectedFile, $expectedClass, $path, $cwdPath = null)
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [$path]],
- $cwdPath
- );
- $this->assertSame($expectedFile, $resolver->getConfigFile());
- $this->assertInstanceOf($expectedClass, $resolver->getConfig());
- }
- public function provideResolveConfigFileDefaultCases()
- {
- $dirBase = $this->getFixtureDir();
- return [
- [
- $dirBase.'case_1'.DIRECTORY_SEPARATOR.'.php_cs.dist',
- 'Test1Config',
- $dirBase.'case_1',
- ],
- [
- $dirBase.'case_2'.DIRECTORY_SEPARATOR.'.php_cs',
- 'Test2Config',
- $dirBase.'case_2',
- ],
- [
- $dirBase.'case_3'.DIRECTORY_SEPARATOR.'.php_cs',
- 'Test3Config',
- $dirBase.'case_3',
- ],
- [
- $dirBase.'case_6'.DIRECTORY_SEPARATOR.'.php_cs.dist',
- 'Test6Config',
- $dirBase.'case_6'.DIRECTORY_SEPARATOR.'subdir',
- $dirBase.'case_6',
- ],
- [
- $dirBase.'case_6'.DIRECTORY_SEPARATOR.'.php_cs.dist',
- 'Test6Config',
- $dirBase.'case_6'.DIRECTORY_SEPARATOR.'subdir/empty_file.php',
- $dirBase.'case_6',
- ],
- ];
- }
- public function testResolveConfigFileChooseFileWithInvalidFile()
- {
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessageRegExp(
- '#^The config file: ".+[\/\\\]Fixtures[\/\\\]ConfigurationResolverConfigFile[\/\\\]case_5[\/\\\]\.php_cs\.dist" does not return a "PhpCsFixer\\\ConfigInterface" instance\. Got: "string"\.$#'
- );
- $dirBase = $this->getFixtureDir();
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [$dirBase.'case_5']],
- ''
- );
- $resolver->getConfig();
- }
- public function testResolveConfigFileChooseFileWithInvalidFormat()
- {
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessageRegExp('/^The format "xls" is not defined, supported are "json", "junit", "txt", "xml"\.$/');
- $dirBase = $this->getFixtureDir();
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [$dirBase.'case_7']],
- ''
- );
- $resolver->getReporter();
- }
- public function testResolveConfigFileChooseFileWithPathArrayWithoutConfig()
- {
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessageRegExp('/^For multiple paths config parameter is required\.$/');
- $dirBase = $this->getFixtureDir();
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [$dirBase.'case_1/.php_cs.dist', $dirBase.'case_1/foo.php']],
- ''
- );
- $resolver->getConfig();
- }
- public function testResolveConfigFileChooseFileWithPathArrayAndConfig()
- {
- $dirBase = $this->getFixtureDir();
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'config' => $dirBase.'case_1/.php_cs.dist',
- 'path' => [$dirBase.'case_1/.php_cs.dist', $dirBase.'case_1/foo.php'],
- ],
- ''
- );
- $this->assertInstanceOf(\PhpCsFixer\Console\ConfigurationResolver::class, $resolver);
- }
- public function testResolvePathRelativeA()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => ['Command']],
- __DIR__
- );
- $this->assertSame([__DIR__.DIRECTORY_SEPARATOR.'Command'], $resolver->getPath());
- }
- public function testResolvePathRelativeB()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [basename(__DIR__)]],
- dirname(__DIR__)
- );
- $this->assertSame([__DIR__], $resolver->getPath());
- }
- public function testResolvePathWithFileThatIsExcludedDirectlyOverridePathMode()
- {
- $this->config->getFinder()
- ->in(__DIR__)
- ->notPath(basename(__FILE__));
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [__FILE__]],
- ''
- );
- $this->assertCount(1, $resolver->getFinder());
- }
- public function testResolvePathWithFileThatIsExcludedDirectlyIntersectionPathMode()
- {
- $this->config->getFinder()
- ->in(__DIR__)
- ->notPath(basename(__FILE__));
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'path' => [__FILE__],
- 'path-mode' => 'intersection',
- ],
- ''
- );
- $this->assertCount(0, $resolver->getFinder());
- }
- public function testResolvePathWithFileThatIsExcludedByDirOverridePathMode()
- {
- $dir = dirname(__DIR__);
- $this->config->getFinder()
- ->in($dir)
- ->exclude(basename(__DIR__));
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [__FILE__]],
- ''
- );
- $this->assertCount(1, $resolver->getFinder());
- }
- public function testResolvePathWithFileThatIsExcludedByDirIntersectionPathMode()
- {
- $dir = dirname(__DIR__);
- $this->config->getFinder()
- ->in($dir)
- ->exclude(basename(__DIR__));
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'path-mode' => 'intersection',
- 'path' => [__FILE__],
- ],
- ''
- );
- $this->assertCount(0, $resolver->getFinder());
- }
- public function testResolvePathWithFileThatIsNotExcluded()
- {
- $dir = __DIR__;
- $this->config->getFinder()
- ->in($dir)
- ->notPath('foo-'.basename(__FILE__));
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [__FILE__]],
- ''
- );
- $this->assertCount(1, $resolver->getFinder());
- }
- /**
- * @param array|\Exception $expected
- * @param null|Finder $configFinder
- * @param string $pathMode
- * @param null|string $config
- *
- * @dataProvider provideResolveIntersectionOfPathsCases
- */
- public function testResolveIntersectionOfPaths($expected, $configFinder, array $path, $pathMode, $config = null)
- {
- if ($expected instanceof \Exception) {
- $this->expectException(get_class($expected));
- }
- if (null !== $configFinder) {
- $this->config->setFinder($configFinder);
- }
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'config' => $config,
- 'path' => $path,
- 'path-mode' => $pathMode,
- ],
- ''
- );
- $intersectionItems = array_map(
- function (\SplFileInfo $file) {
- return $file->getRealPath();
- },
- iterator_to_array($resolver->getFinder(), false)
- );
- sort($expected);
- sort($intersectionItems);
- $this->assertSame($expected, $intersectionItems);
- }
- public function provideResolveIntersectionOfPathsCases()
- {
- $dir = __DIR__.'/../Fixtures/ConfigurationResolverPathsIntersection';
- $cb = function (array $items) use ($dir) {
- return array_map(
- function ($item) use ($dir) {
- return realpath($dir.'/'.$item);
- },
- $items
- );
- };
- return [
- 'no path at all' => [
- new \LogicException(),
- Finder::create(),
- [],
- 'override',
- ],
- 'configured only by finder' => [
- // don't override if the argument is empty
- $cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
- Finder::create()
- ->in($dir),
- [],
- 'override',
- ],
- 'configured only by argument' => [
- $cb(['a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php']),
- Finder::create(),
- [$dir],
- 'override',
- ],
- 'configured by finder, intersected with empty argument' => [
- [],
- Finder::create()
- ->in($dir),
- [],
- 'intersection',
- ],
- 'configured by finder, intersected with dir' => [
- $cb(['c/c1.php', 'c/d/cd1.php']),
- Finder::create()
- ->in($dir),
- [$dir.'/c'],
- 'intersection',
- ],
- 'configured by finder, intersected with file' => [
- $cb(['c/c1.php']),
- Finder::create()
- ->in($dir),
- [$dir.'/c/c1.php'],
- 'intersection',
- ],
- 'finder points to one dir while argument to another, not connected' => [
- [],
- Finder::create()
- ->in($dir.'/b'),
- [$dir.'/c'],
- 'intersection',
- ],
- 'finder with excluded dir, intersected with excluded file' => [
- [],
- Finder::create()
- ->in($dir)
- ->exclude('c'),
- [$dir.'/c/d/cd1.php'],
- 'intersection',
- ],
- 'finder with excluded dir, intersected with dir containing excluded one' => [
- $cb(['c/c1.php']),
- Finder::create()
- ->in($dir)
- ->exclude('c/d'),
- [$dir.'/c'],
- 'intersection',
- ],
- 'finder with excluded file, intersected with dir containing excluded one' => [
- $cb(['c/d/cd1.php']),
- Finder::create()
- ->in($dir)
- ->notPath('c/c1.php'),
- [$dir.'/c'],
- 'intersection',
- ],
- 'configured by finder, intersected with non-existing path' => [
- new \LogicException(),
- Finder::create()
- ->in($dir),
- ['non_existing_dir'],
- 'intersection',
- ],
- 'configured by config file, overriden by multiple files' => [
- $cb(['d/d1.php', 'd/d2.php']),
- null,
- [$dir.'/d/d1.php', $dir.'/d/d2.php'],
- 'override',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, intersected with multiple files' => [
- $cb(['d/d1.php', 'd/d2.php']),
- null,
- [$dir.'/d/d1.php', $dir.'/d/d2.php'],
- 'intersection',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, overriden by non-existing dir' => [
- new \LogicException(),
- null,
- [$dir.'/d/fff'],
- 'override',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, intersected with non-existing dir' => [
- new \LogicException(),
- null,
- [$dir.'/d/fff'],
- 'intersection',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, overriden by non-existing file' => [
- new \LogicException(),
- null,
- [$dir.'/d/fff.php'],
- 'override',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, intersected with non-existing file' => [
- new \LogicException(),
- null,
- [$dir.'/d/fff.php'],
- 'intersection',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, overriden by multiple files and dirs' => [
- $cb(['d/d1.php', 'd/e/de1.php', 'd/f/df1.php']),
- null,
- [$dir.'/d/d1.php', $dir.'/d/e', $dir.'/d/f/'],
- 'override',
- $dir.'/d/.php_cs',
- ],
- 'configured by config file, intersected with multiple files and dirs' => [
- $cb(['d/d1.php', 'd/e/de1.php', 'd/f/df1.php']),
- null,
- [$dir.'/d/d1.php', $dir.'/d/e', $dir.'/d/f/'],
- 'intersection',
- $dir.'/d/.php_cs',
- ],
- ];
- }
- /**
- * @param array $options
- * @param bool $expectedResult
- *
- * @dataProvider provideConfigFinderIsOverriddenCases
- */
- public function testConfigFinderIsOverridden(array $options, $expectedResult)
- {
- $resolver = new ConfigurationResolver($this->config, $options, '');
- $this->assertSame($expectedResult, $resolver->configFinderIsOverridden());
- $resolver = new ConfigurationResolver($this->config, $options, '');
- $resolver->getFinder();
- $this->assertSame($expectedResult, $resolver->configFinderIsOverridden());
- }
- public function provideConfigFinderIsOverriddenCases()
- {
- $root = __DIR__.'/../..';
- return [
- [
- [
- 'config' => $root.'/.php_cs.dist',
- ],
- false,
- ],
- [
- [
- 'config' => $root.'/.php_cs.dist',
- 'path' => [$root.'/src'],
- ],
- true,
- ],
- [
- [],
- false,
- ],
- [
- [
- 'path' => [$root.'/src'],
- ],
- false,
- ],
- [
- [
- 'config' => $root.'/.php_cs.dist',
- 'path' => [$root.'/src'],
- 'path-mode' => ConfigurationResolver::PATH_MODE_INTERSECTION,
- ],
- false,
- ],
- // scenario when loaded config is not setting custom finder
- [
- [
- 'config' => $root.'/tests/Fixtures/ConfigurationResolverConfigFile/case_3/.php_cs.dist',
- 'path' => [$root.'/src'],
- ],
- false,
- ],
- // scenario when loaded config contains not fully defined finder
- [
- [
- 'config' => $root.'/tests/Fixtures/ConfigurationResolverConfigFile/case_9/.php_cs',
- 'path' => [$root.'/src'],
- ],
- false,
- ],
- ];
- }
- public function testResolveIsDryRunViaStdIn()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'dry-run' => false,
- 'path' => ['-'],
- ],
- ''
- );
- $this->assertTrue($resolver->isDryRun());
- }
- public function testResolveIsDryRunViaNegativeOption()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['dry-run' => false],
- ''
- );
- $this->assertFalse($resolver->isDryRun());
- }
- public function testResolveIsDryRunViaPositiveOption()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['dry-run' => true],
- ''
- );
- $this->assertTrue($resolver->isDryRun());
- }
- /**
- * @param bool $expected
- * @param bool $configValue
- * @param null|bool|string $passed
- *
- * @dataProvider provideResolveBooleanOptionCases
- */
- public function testResolveUsingCacheWithConfigOption($expected, $configValue, $passed)
- {
- $this->config->setUsingCache($configValue);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['using-cache' => $passed],
- ''
- );
- $this->assertSame($expected, $resolver->getUsingCache());
- }
- public function testResolveUsingCacheWithPositiveConfigAndNoOption()
- {
- $this->config->setUsingCache(true);
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertTrue($resolver->getUsingCache());
- }
- public function testResolveUsingCacheWithNegativeConfigAndNoOption()
- {
- $this->config->setUsingCache(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertFalse($resolver->getUsingCache());
- }
- public function testResolveCacheFileWithoutConfigAndOption()
- {
- $default = $this->config->getCacheFile();
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertSame($default, $resolver->getCacheFile());
- }
- public function testResolveCacheFileWithConfig()
- {
- $cacheFile = 'foo/bar.baz';
- $this->config->setCacheFile($cacheFile);
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertSame($cacheFile, $resolver->getCacheFile());
- }
- public function testResolveCacheFileWithOption()
- {
- $cacheFile = 'bar.baz';
- $this->config->setCacheFile($cacheFile);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['cache-file' => $cacheFile],
- ''
- );
- $this->assertSame($cacheFile, $resolver->getCacheFile());
- }
- public function testResolveCacheFileWithConfigAndOption()
- {
- $configCacheFile = 'foo/bar.baz';
- $optionCacheFile = 'bar.baz';
- $this->config->setCacheFile($configCacheFile);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['cache-file' => $optionCacheFile],
- ''
- );
- $this->assertSame($optionCacheFile, $resolver->getCacheFile());
- }
- /**
- * @param bool $expected
- * @param bool $configValue
- * @param null|bool|string $passed
- *
- * @dataProvider provideResolveBooleanOptionCases
- */
- public function testResolveAllowRiskyWithConfigOption($expected, $configValue, $passed)
- {
- $this->config->setRiskyAllowed($configValue);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['allow-risky' => $passed],
- ''
- );
- $this->assertSame($expected, $resolver->getRiskyAllowed());
- }
- public function testResolveAllowRiskyWithNegativeConfigAndPositiveOption()
- {
- $this->config->setRiskyAllowed(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['allow-risky' => 'yes'],
- ''
- );
- $this->assertTrue($resolver->getRiskyAllowed());
- }
- public function testResolveAllowRiskyWithNegativeConfigAndNegativeOption()
- {
- $this->config->setRiskyAllowed(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['allow-risky' => 'no'],
- ''
- );
- $this->assertFalse($resolver->getRiskyAllowed());
- }
- public function testResolveAllowRiskyWithPositiveConfigAndNoOption()
- {
- $this->config->setRiskyAllowed(true);
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertTrue($resolver->getRiskyAllowed());
- }
- public function testResolveAllowRiskyWithNegativeConfigAndNoOption()
- {
- $this->config->setRiskyAllowed(false);
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertFalse($resolver->getRiskyAllowed());
- }
- public function testResolveRulesWithConfig()
- {
- $this->config->setRules([
- 'braces' => true,
- 'strict_comparison' => false,
- ]);
- $resolver = new ConfigurationResolver(
- $this->config,
- [],
- ''
- );
- $this->assertSameRules(
- [
- 'braces' => true,
- ],
- $resolver->getRules()
- );
- }
- public function testResolveRulesWithOption()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['rules' => 'braces,-strict_comparison'],
- ''
- );
- $this->assertSameRules(
- [
- 'braces' => true,
- ],
- $resolver->getRules()
- );
- }
- public function testResolveRulesWithUnknownRules()
- {
- $this->expectException(
- \PhpCsFixer\ConfigurationException\InvalidConfigurationException::class
- );
- $this->expectExceptionMessage(
- 'The rules contain unknown fixers: "bar", "binary_operator_space" (did you mean "binary_operator_spaces"?).'
- );
- $resolver = new ConfigurationResolver(
- $this->config,
- ['rules' => 'braces,-bar,binary_operator_space'],
- ''
- );
- $resolver->getRules();
- }
- public function testResolveRulesWithConfigAndOption()
- {
- $this->config->setRules([
- 'braces' => true,
- 'strict_comparison' => false,
- ]);
- $resolver = new ConfigurationResolver(
- $this->config,
- ['rules' => 'blank_line_before_statement'],
- ''
- );
- $this->assertSameRules(
- [
- 'blank_line_before_statement' => true,
- ],
- $resolver->getRules()
- );
- }
- public function testResolveCommandLineInputOverridesDefault()
- {
- $command = new FixCommand();
- $definition = $command->getDefinition();
- $arguments = $definition->getArguments();
- $this->assertCount(1, $arguments, 'Expected one argument, possibly test needs updating.');
- $this->assertArrayHasKey('path', $arguments);
- $options = $definition->getOptions();
- $this->assertSame(
- ['path-mode', 'allow-risky', 'config', 'dry-run', 'rules', 'using-cache', 'cache-file', 'diff', 'format', 'stop-on-violation', 'show-progress'],
- array_keys($options),
- 'Expected options mismatch, possibly test needs updating.'
- );
- $resolver = new ConfigurationResolver(
- $this->config,
- [
- 'path-mode' => 'intersection',
- 'allow-risky' => 'yes',
- 'config' => null,
- 'dry-run' => true,
- 'rules' => 'php_unit_construct',
- 'using-cache' => false,
- 'diff' => true,
- 'format' => 'json',
- 'stop-on-violation' => true,
- ],
- ''
- );
- $this->assertTrue($resolver->shouldStopOnViolation());
- $this->assertTrue($resolver->getRiskyAllowed());
- $this->assertTrue($resolver->isDryRun());
- $this->assertSame(['php_unit_construct' => true], $resolver->getRules());
- $this->assertFalse($resolver->getUsingCache());
- $this->assertNull($resolver->getCacheFile());
- $this->assertInstanceOf(\PhpCsFixer\Differ\SebastianBergmannDiffer::class, $resolver->getDiffer());
- $this->assertSame('json', $resolver->getReporter()->getFormat());
- }
- /**
- * @param string $expected
- * @param bool|string $differConfig
- *
- * @dataProvider provideDifferCases
- */
- public function testResolveDiffer($expected, $differConfig)
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['diff' => $differConfig],
- ''
- );
- $this->assertInstanceOf($expected, $resolver->getDiffer());
- }
- public function provideDifferCases()
- {
- return [
- [
- \PhpCsFixer\Differ\NullDiffer::class,
- false,
- ],
- [
- \PhpCsFixer\Differ\SebastianBergmannDiffer::class,
- true,
- ],
- ];
- }
- public function testResolveConfigFileOverridesDefault()
- {
- $dir = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_8';
- $resolver = new ConfigurationResolver(
- $this->config,
- ['path' => [$dir.DIRECTORY_SEPARATOR.'.php_cs']],
- ''
- );
- $this->assertTrue($resolver->getRiskyAllowed());
- $this->assertSame(['php_unit_construct' => true], $resolver->getRules());
- $this->assertFalse($resolver->getUsingCache());
- $this->assertNull($resolver->getCacheFile());
- $this->assertSame('xml', $resolver->getReporter()->getFormat());
- }
- /**
- * @group legacy
- * @expectedDeprecation Expected "yes" or "no" for option "allow-risky", other values are deprecated and support will be removed in 3.0. Got "yes please", this implicitly set the option to "false".
- */
- public function testDeprecationOfPassingOtherThanNoOrYes()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['allow-risky' => 'yes please'],
- ''
- );
- $this->assertFalse($resolver->getRiskyAllowed());
- }
- public function provideResolveBooleanOptionCases()
- {
- return [
- [true, true, 'yes'],
- [true, true, true],
- [true, false, 'yes'],
- [true, false, true],
- [false, true, 'no'],
- [false, true, false],
- [false, false, 'no'],
- [false, false, false],
- [true, true, null],
- [false, false, null],
- ];
- }
- public function testWithEmptyRules()
- {
- $resolver = new ConfigurationResolver(
- $this->config,
- ['rules' => ''],
- ''
- );
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessageRegExp('/^Empty rules value is not allowed\.$/');
- $resolver->getRules();
- }
- private function assertSameRules(array $expected, array $actual, $message = '')
- {
- ksort($expected);
- ksort($actual);
- $this->assertSame($expected, $actual, $message);
- }
- private function getFixtureDir()
- {
- return realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'ConfigurationResolverConfigFile'.DIRECTORY_SEPARATOR).'/';
- }
- }
|