123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?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;
- use PhpCsFixer\Config;
- use PhpCsFixer\Console\Command\FixCommand;
- use PhpCsFixer\Console\ConfigurationResolver;
- use PhpCsFixer\Finder;
- use PhpCsFixer\Fixer\FixerInterface;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Tester\CommandTester;
- use Symfony\Component\Finder\Finder as SymfonyFinder;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Config
- */
- final class ConfigTest extends TestCase
- {
- public function testConfigRulesUsingSeparateMethod()
- {
- $config = new Config();
- $configResolver = new ConfigurationResolver(
- $config,
- array(
- 'rules' => 'cast_spaces,braces',
- ),
- getcwd()
- );
- $this->assertArraySubset(
- array(
- 'cast_spaces' => true,
- 'braces' => true,
- ),
- $configResolver->getRules()
- );
- }
- public function testConfigRulesUsingJsonMethod()
- {
- $config = new Config();
- $configResolver = new ConfigurationResolver(
- $config,
- array(
- 'rules' => '{"array_syntax": {"syntax": "short"}, "cast_spaces": true}',
- ),
- getcwd()
- );
- $this->assertArraySubset(
- array(
- 'array_syntax' => array(
- 'syntax' => 'short',
- ),
- 'cast_spaces' => true,
- ),
- $configResolver->getRules()
- );
- }
- public function testConfigRulesUsingInvalidJson()
- {
- $this->setExpectedException('PhpCsFixer\ConfigurationException\InvalidConfigurationException');
- $config = new Config();
- $configResolver = new ConfigurationResolver(
- $config,
- array(
- 'rules' => '{blah',
- ),
- getcwd()
- );
- $configResolver->getRules();
- }
- public function testCustomConfig()
- {
- $customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
- $command = new FixCommand();
- $commandTester = new CommandTester($command);
- $commandTester->execute(
- array(
- 'path' => array($customConfigFile),
- '--dry-run' => true,
- '--config' => $customConfigFile,
- ),
- array(
- 'decorated' => false,
- 'verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE,
- )
- );
- $this->assertStringMatchesFormat(
- sprintf('%%ALoaded config custom_config_test from "%s".%%A', $customConfigFile),
- $commandTester->getDisplay(true)
- );
- }
- public function testThatFinderWorksWithDirSetOnConfig()
- {
- $config = new Config();
- $items = iterator_to_array(
- $config->getFinder()->in(__DIR__.'/Fixtures/FinderDirectory'),
- false
- );
- $this->assertCount(1, $items);
- $this->assertSame('somefile.php', $items[0]->getFilename());
- }
- public function testThatCustomFinderWorks()
- {
- $finder = new Finder();
- $finder->in(__DIR__.'/Fixtures/FinderDirectory');
- $config = Config::create()->setFinder($finder);
- $items = iterator_to_array(
- $config->getFinder(),
- false
- );
- $this->assertCount(1, $items);
- $this->assertSame('somefile.php', $items[0]->getFilename());
- }
- public function testThatCustomSymfonyFinderWorks()
- {
- $finder = new SymfonyFinder();
- $finder->in(__DIR__.'/Fixtures/FinderDirectory');
- $config = Config::create()->setFinder($finder);
- $items = iterator_to_array(
- $config->getFinder(),
- false
- );
- $this->assertCount(1, $items);
- $this->assertSame('somefile.php', $items[0]->getFilename());
- }
- public function testThatCacheFileHasDefaultValue()
- {
- $config = new Config();
- $this->assertSame('.php_cs.cache', $config->getCacheFile());
- }
- public function testThatCacheFileCanBeMutated()
- {
- $cacheFile = 'some-directory/some.file';
- $config = new Config();
- $config->setCacheFile($cacheFile);
- $this->assertSame($cacheFile, $config->getCacheFile());
- }
- public function testThatMutatorHasFluentInterface()
- {
- $config = new Config();
- $this->assertSame($config, $config->setCacheFile('some-directory/some.file'));
- }
- public function testRegisterCustomFixersWithInvalidArgument()
- {
- $this->setExpectedExceptionRegExp(
- 'InvalidArgumentException',
- '/^Argument must be an array or a Traversable, got "\w+"\.$/'
- );
- $config = new Config();
- $config->registerCustomFixers('foo');
- }
- /**
- * @param FixerInterface[] $expected
- * @param iterable $suite
- *
- * @dataProvider provideRegisterCustomFixersCases
- */
- public function testRegisterCustomFixers(array $expected, $suite)
- {
- $config = new Config();
- $config->registerCustomFixers($suite);
- $this->assertSame($expected, $config->getCustomFixers());
- }
- /**
- * @return array
- */
- public function provideRegisterCustomFixersCases()
- {
- $fixers = array(
- new \PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer(),
- new \PhpCsFixer\Fixer\ControlStructure\IncludeFixer(),
- );
- $cases = array(
- array($fixers, $fixers),
- array($fixers, new \ArrayIterator($fixers)),
- );
- return $cases;
- }
- }
|