Browse Source

DescribeCommand - Show which sample uses the default configuration

SpacePossum 7 years ago
parent
commit
db97ab6f25

+ 19 - 16
src/Console/Command/DescribeCommand.php

@@ -105,7 +105,7 @@ final class DescribeCommand extends Command
             $this->describeList($output, $e->getType());
 
             throw new \InvalidArgumentException(sprintf(
-                '%s %s not found.%s',
+                '%s "%s" not found.%s',
                 ucfirst($e->getType()), $name, null === $alternative ? '' : ' Did you mean "'.$alternative.'"?'
             ));
         }
@@ -149,11 +149,12 @@ final class DescribeCommand extends Command
         }
 
         if ($fixer instanceof ConfigurationDefinitionFixerInterface) {
-            $output->writeln('Fixer is configurable using following options:');
-
             $configurationDefinition = $fixer->getConfigurationDefinition();
+            $options = $configurationDefinition->getOptions();
+
+            $output->writeln(sprintf('Fixer is configurable using following option%s:', 1 === count($options) ? '' : 's'));
 
-            foreach ($configurationDefinition->getOptions() as $option) {
+            foreach ($options as $option) {
                 $line = '* <info>'.$option->getName().'</info>';
 
                 $allowed = HelpCommand::getDisplayableAllowedValues($option);
@@ -177,7 +178,7 @@ final class DescribeCommand extends Command
                         HelpCommand::toString($option->getDefault())
                     );
                 } else {
-                    $line .= 'required';
+                    $line .= '<comment>required</comment>';
                 }
 
                 $output->writeln($line);
@@ -224,28 +225,30 @@ final class DescribeCommand extends Command
             foreach ($codeSamples as $index => $codeSample) {
                 $old = $codeSample->getCode();
                 $tokens = Tokens::fromCode($old);
+
                 if ($fixer instanceof ConfigurableFixerInterface) {
                     $configuration = $codeSample->getConfiguration();
-
-                    if (null === $configuration) {
-                        $configuration = array();
-                    }
-
-                    $fixer->configure($configuration);
+                    $fixer->configure(null === $configuration ? array() : $configuration);
                 }
 
                 $file = $codeSample instanceof FileSpecificCodeSampleInterface
                     ? $codeSample->getSplFileInfo()
                     : new StdinFileInfo();
+
                 $fixer->fix($file, $tokens);
-                $new = $tokens->generateCode();
-                $diff = $differ->diff($old, $new);
 
-                if (null === $codeSample->getConfiguration()) {
-                    $output->writeln(sprintf(' * Example #%d.', $index + 1));
+                $diff = $differ->diff($old, $tokens->generateCode());
+
+                if ($fixer instanceof ConfigurableFixerInterface) {
+                    if (null === $configuration) {
+                        $output->writeln(sprintf(' * Example #%d. Fixing with the <comment>default</comment> configuration.', $index + 1));
+                    } else {
+                        $output->writeln(sprintf(' * Example #%d. Fixing with configuration: <comment>%s</comment>.', $index + 1, HelpCommand::toString($codeSample->getConfiguration())));
+                    }
                 } else {
-                    $output->writeln(sprintf(' * Example #%d. Fixing with configuration: <comment>%s</comment>.', $index + 1, HelpCommand::toString($codeSample->getConfiguration())));
+                    $output->writeln(sprintf(' * Example #%d.', $index + 1));
                 }
+
                 $output->writeln($diffFormatter->format($diff, '   %s'));
                 $output->writeln('');
             }

+ 58 - 22
tests/Console/Command/DescribeCommandTest.php

@@ -32,6 +32,16 @@ use Symfony\Component\Console\Tester\CommandTester;
  */
 final class DescribeCommandTest extends TestCase
 {
+    /**
+     * @var Application
+     */
+    private $application;
+
+    protected function setUp()
+    {
+        $this->application = new Application();
+    }
+
     public function testExecuteOutput()
     {
         $expected = <<<'EOT'
@@ -39,11 +49,14 @@ Description of Foo/bar rule.
 Fixes stuff.
 Replaces bad stuff with good stuff.
 
-Fixer is configurable using following options:
+Fixer applying this rule is risky.
+Can break stuff.
+
+Fixer is configurable using following option:
 * functions (array): list of `function` names to fix; defaults to ['foo', 'test']
 
 Fixing examples:
- * Example #1.
+ * Example #1. Fixing with the default configuration.
    ---------- begin diff ----------
    --- Original
    +++ New
@@ -64,7 +77,7 @@ Fixing examples:
 
 EOT;
 
-        $this->assertSame($expected, $this->execute(false)->getDisplay(true));
+        $this->assertSame($expected, $this->execute('Foo/bar', false)->getDisplay(true));
     }
 
     public function testExecuteOutputWithDecoration()
@@ -74,11 +87,14 @@ EOT;
 Fixes stuff.
 Replaces bad stuff with good stuff.
 
-Fixer is configurable using following options:
+\033[37;41mFixer applying this rule is risky.\033[39;49m
+Can break stuff.
+
+Fixer is configurable using following option:
 * \033[32mfunctions\033[39m (\033[33marray\033[39m): list of \033[32m`function`\033[39m names to fix; defaults to \033[33m['foo', 'test']\033[39m
 
 Fixing examples:
- * Example #1.
+ * Example #1. Fixing with the \033[33mdefault\033[39m configuration.
 \033[33m   ---------- begin diff ----------\033[39m
    \033[31m--- Original\033[39m
    \033[32m+++ New\033[39m
@@ -99,9 +115,10 @@ Fixing examples:
 
 EOT;
 
-        $actual = $this->execute(true)->getDisplay(true);
+        $actual = $this->execute('Foo/bar', true)->getDisplay(true);
 
         if (false !== strpos($actual, "\033[0m")) {
+            $expected = str_replace("\033[39;49m", "\033[0m", $expected);
             $expected = str_replace("\033[39m", "\033[0m", $expected);
         }
 
@@ -110,31 +127,44 @@ EOT;
 
     public function testExecuteStatusCode()
     {
-        $this->assertSame(0, $this->execute(false)->getStatusCode());
+        $this->assertSame(0, $this->execute('Foo/bar', false)->getStatusCode());
     }
 
-    public function testExecuteWithUnknownName()
+    public function testExecuteWithUnknownRuleName()
     {
-        $application = new Application();
-        $application->add(new DescribeCommand(new FixerFactory()));
+        $this->application->add(new DescribeCommand(new FixerFactory()));
 
-        $command = $application->find('describe');
+        $command = $this->application->find('describe');
 
         $commandTester = new CommandTester($command);
 
-        $this->setExpectedException('InvalidArgumentException', 'Rule Foo/bar not found.');
+        $this->setExpectedExceptionRegExp('InvalidArgumentException', '#^Rule "Foo/bar" not found\.$#');
         $commandTester->execute(array(
             'command' => $command->getName(),
             'name' => 'Foo/bar',
         ));
     }
 
+    public function testExecuteWithUnknownSetName()
+    {
+        $this->application->add(new DescribeCommand(new FixerFactory()));
+
+        $command = $this->application->find('describe');
+
+        $commandTester = new CommandTester($command);
+
+        $this->setExpectedExceptionRegExp('InvalidArgumentException', '#^Set "@NoSuchSet" not found\.$#');
+        $commandTester->execute(array(
+            'command' => $command->getName(),
+            'name' => '@NoSuchSet',
+        ));
+    }
+
     public function testExecuteWithoutName()
     {
-        $application = new Application();
-        $application->add(new DescribeCommand(new FixerFactory()));
+        $this->application->add(new DescribeCommand(new FixerFactory()));
 
-        $command = $application->find('describe');
+        $command = $this->application->find('describe');
 
         $commandTester = new CommandTester($command);
 
@@ -144,12 +174,19 @@ EOT;
         ));
     }
 
+    public function testGetAlternativeSuggestion()
+    {
+        $this->setExpectedExceptionRegExp('InvalidArgumentException', '#^Rule "Foo2/bar" not found\. Did you mean "Foo/bar"\?$#');
+        $this->execute('Foo2/bar', false);
+    }
+
     /**
-     * @param bool $decorated
+     * @param string $name
+     * @param bool   $decorated
      *
      * @return CommandTester
      */
-    private function execute($decorated)
+    private function execute($name, $decorated)
     {
         $fixer = $this->prophesize();
         $fixer->willImplement('PhpCsFixer\Fixer\DefinedFixerInterface');
@@ -157,7 +194,7 @@ EOT;
 
         $fixer->getName()->willReturn('Foo/bar');
         $fixer->getPriority()->willReturn(0);
-        $fixer->isRisky()->willReturn(false);
+        $fixer->isRisky()->willReturn(true);
 
         $generator = new FixerOptionValidatorGenerator();
         $functionNames = array('foo', 'test');
@@ -209,16 +246,15 @@ EOT;
         $fixerFactory = new FixerFactory();
         $fixerFactory->registerFixer($fixer->reveal(), true);
 
-        $application = new Application();
-        $application->add(new DescribeCommand($fixerFactory));
+        $this->application->add(new DescribeCommand($fixerFactory));
 
-        $command = $application->find('describe');
+        $command = $this->application->find('describe');
 
         $commandTester = new CommandTester($command);
         $commandTester->execute(
             array(
                 'command' => $command->getName(),
-                'name' => 'Foo/bar',
+                'name' => $name,
             ),
             array(
                 'decorated' => $decorated,

+ 16 - 0
tests/RuleSetTest.php

@@ -67,6 +67,22 @@ final class RuleSetTest extends TestCase
         );
     }
 
+    public function testBuildInSetDefinitionNames()
+    {
+        $setNames = RuleSet::create()->getSetDefinitionNames();
+
+        $this->assertInternalType('array', $setNames);
+        $this->assertNotEmpty($setNames);
+
+        $i = 0;
+        foreach ($setNames as $index => $setName) {
+            $this->assertSame($i, $index);
+            $this->assertInternalType('string', $setName);
+            $this->assertSame('@', substr($setName, 0, 1));
+            ++$i;
+        }
+    }
+
     public function testResolveRulesWithInvalidSet()
     {
         $this->setExpectedException(