/**
  * @param \DOMDocument  $dom
  * @param \DOMElement   $testsuite
  * @param ReportSummary $reportSummary
  */
 private function createFailedTestCases(\DOMDocument $dom, \DOMElement $testsuite, ReportSummary $reportSummary)
 {
     $assertionsCount = 0;
     foreach ($reportSummary->getChanged() as $file => $fixResult) {
         $testcase = $this->createFailedTestCase($dom, $file, $fixResult, $reportSummary->shouldAddAppliedFixers());
         $testsuite->appendChild($testcase);
         $assertionsCount += (int) $testcase->getAttribute('assertions');
     }
     $testsuite->setAttribute('tests', count($reportSummary->getChanged()));
     $testsuite->setAttribute('assertions', $assertionsCount);
     $testsuite->setAttribute('failures', $assertionsCount);
     $testsuite->setAttribute('errors', 0);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function generate(ReportSummary $reportSummary)
 {
     $dom = new \DOMDocument('1.0', 'UTF-8');
     // new nodes should be added to this or existing children
     $root = $dom->createElement('report');
     $dom->appendChild($root);
     $filesXML = $dom->createElement('files');
     $root->appendChild($filesXML);
     $i = 1;
     foreach ($reportSummary->getChanged() as $file => $fixResult) {
         $fileXML = $dom->createElement('file');
         $fileXML->setAttribute('id', $i++);
         $fileXML->setAttribute('name', $file);
         $filesXML->appendChild($fileXML);
         if ($reportSummary->shouldAddAppliedFixers()) {
             $fileXML->appendChild($this->createAppliedFixersElement($dom, $fixResult));
         }
         if (!empty($fixResult['diff'])) {
             $fileXML->appendChild($this->createDiffElement($dom, $fixResult));
         }
     }
     if (null !== $reportSummary->getTime()) {
         $root->appendChild($this->createTimeElement($reportSummary->getTime(), $dom));
     }
     if (null !== $reportSummary->getTime()) {
         $root->appendChild($this->createMemoryElement($reportSummary->getMemory(), $dom));
     }
     $dom->formatOutput = true;
     return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($dom->saveXML()) : $dom->saveXML();
 }
 /**
  * {@inheritdoc}
  */
 public function generate(ReportSummary $reportSummary)
 {
     $output = '';
     $i = 0;
     foreach ($reportSummary->getChanged() as $file => $fixResult) {
         ++$i;
         $output .= sprintf('%4d) %s', $i, $file);
         if ($reportSummary->shouldAddAppliedFixers()) {
             $output .= $this->getAppliedFixers($reportSummary->isDecoratedOutput(), $fixResult);
         }
         $output .= $this->getDiff($reportSummary->isDecoratedOutput(), $fixResult);
         $output .= PHP_EOL;
     }
     return $output . $this->getFooter($reportSummary->getTime(), $reportSummary->getMemory(), $reportSummary->isDryRun());
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function generate(ReportSummary $reportSummary)
 {
     $jFiles = array();
     foreach ($reportSummary->getChanged() as $file => $fixResult) {
         $jfile = array('name' => $file);
         if ($reportSummary->shouldAddAppliedFixers()) {
             $jfile['appliedFixers'] = $fixResult['appliedFixers'];
         }
         if (!empty($fixResult['diff'])) {
             $jfile['diff'] = $fixResult['diff'];
         }
         $jFiles[] = $jfile;
     }
     $json = array('files' => $jFiles);
     if (null !== $reportSummary->getTime()) {
         $json['time'] = array('total' => round($reportSummary->getTime() / 1000, 3));
     }
     if (null !== $reportSummary->getMemory()) {
         $json['memory'] = round($reportSummary->getMemory() / 1024 / 1024, 3);
     }
     $json = json_encode($json);
     return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($json) : $json;
 }
    public function testGenerateComplex()
    {
        $expectedXml = <<<'XML'
<?xml version="1.0"?>
<testsuites>
  <testsuite assertions="3" errors="0" failures="3" name="PHP CS Fixer" tests="2" time="1.234">
    <testcase assertions="2" file="someFile.php" name="someFile">
      <failure type="code_style">applied fixers:
---------------
* some_fixer_name_here_1
* some_fixer_name_here_2

Diff:
---------------

this text is a diff ;)</failure>
    </testcase>
    <testcase assertions="1" file="anotherFile.php" name="anotherFile">
      <failure type="code_style">applied fixers:
---------------
* another_fixer_name_here

Diff:
---------------

another diff here ;)</failure>
    </testcase>
  </testsuite>
</testsuites>
XML;
        $actualXml = $this->reporter->generate(ReportSummary::create()->setAddAppliedFixers(true)->setChanged(array('someFile.php' => array('appliedFixers' => array('some_fixer_name_here_1', 'some_fixer_name_here_2'), 'diff' => 'this text is a diff ;)'), 'anotherFile.php' => array('appliedFixers' => array('another_fixer_name_here'), 'diff' => 'another diff here ;)')))->setTime(1234));
        $this->assertJunitXmlSchema($actualXml);
        $this->assertXmlStringEqualsXmlString($expectedXml, $actualXml);
    }
Example #6
0
    public function testGenerateComplexWithDecoratedOutput()
    {
        $expectedText = str_replace("\n", PHP_EOL, <<<'TEXT'
   1) someFile.php (<comment>some_fixer_name_here</comment>)
<comment>      ---------- begin diff ----------</comment>
this text is a diff ;)
<comment>      ----------- end diff -----------</comment>

   2) anotherFile.php (<comment>another_fixer_name_here</comment>)
<comment>      ---------- begin diff ----------</comment>
another diff here ;)
<comment>      ----------- end diff -----------</comment>


Checked all files in 1.234 seconds, 2.500 MB memory used

TEXT
);
        $this->assertSame($expectedText, $this->reporter->generate(ReportSummary::create()->setAddAppliedFixers(true)->setChanged(array('someFile.php' => array('appliedFixers' => array('some_fixer_name_here'), 'diff' => 'this text is a diff ;)'), 'anotherFile.php' => array('appliedFixers' => array('another_fixer_name_here'), 'diff' => 'another diff here ;)')))->setIsDecoratedOutput(true)->setIsDryRun(true)->setMemory(2.5 * 1024 * 1024)->setTime(1234)));
    }
Example #7
0
 /**
  * @see Command
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $verbosity = $output->getVerbosity();
     $resolver = new ConfigurationResolver();
     $resolver->setCwd(getcwd())->setDefaultConfig($this->defaultConfig)->setOptions(array('allow-risky' => $input->getOption('allow-risky'), 'config' => $input->getOption('config'), 'dry-run' => $input->getOption('dry-run'), 'rules' => $input->getOption('rules'), 'path' => $input->getArgument('path'), 'path-mode' => $input->getOption('path-mode'), 'progress' => OutputInterface::VERBOSITY_VERBOSE <= $verbosity && 'txt' === $input->getOption('format'), 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file'), 'format' => $input->getOption('format')))->resolve();
     $reporter = ReporterFactory::create()->registerBuiltInReporters()->getReporter($resolver->getFormat());
     $stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : ('txt' === $reporter->getFormat() ? $output : null);
     if (null !== $stdErr && extension_loaded('xdebug')) {
         $stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', 'You are running php-cs-fixer with xdebug enabled. This has a major impact on runtime performance.'));
     }
     $config = $resolver->getConfig();
     $configFile = $resolver->getConfigFile();
     if (null !== $stdErr && $configFile) {
         $stdErr->writeln(sprintf('Loaded config from "%s".', $configFile));
     }
     $linter = new NullLinter();
     if ($config->usingLinter()) {
         try {
             $linter = new Linter($config->getPhpExecutable());
         } catch (UnavailableLinterException $e) {
             if (null !== $stdErr && $configFile) {
                 $stdErr->writeln('Unable to use linter, can not find PHP executable.');
             }
         }
     }
     if (null !== $stdErr && $config->usingCache()) {
         $cacheFile = $config->getCacheFile();
         if (is_file($cacheFile)) {
             $stdErr->writeln(sprintf('Using cache file "%s".', $cacheFile));
         }
     }
     $showProgress = $resolver->getProgress();
     $runner = new Runner($config, $input->getOption('diff') ? new SebastianBergmannDiffer() : new NullDiffer(), $showProgress ? $this->eventDispatcher : null, $this->errorsManager, $linter, $resolver->isDryRun());
     $progressOutput = $showProgress && $stdErr ? new ProcessOutput($stdErr, $this->eventDispatcher) : new NullOutput();
     $this->stopwatch->start('fixFiles');
     $changed = $runner->fix();
     $this->stopwatch->stop('fixFiles');
     $progressOutput->printLegend();
     $fixEvent = $this->stopwatch->getEvent('fixFiles');
     $reportSummary = ReportSummary::create()->setChanged($changed)->setAddAppliedFixers(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity())->setIsDecoratedOutput($output->isDecorated())->setIsDryRun($resolver->isDryRun())->setMemory($fixEvent->getMemory())->setTime($fixEvent->getDuration());
     $output->write($reporter->generate($reportSummary));
     $invalidErrors = $this->errorsManager->getInvalidErrors();
     $exceptionErrors = $this->errorsManager->getExceptionErrors();
     $lintErrors = $this->errorsManager->getLintErrors();
     if (null !== $stdErr) {
         if (count($invalidErrors) > 0) {
             $this->listErrors($stdErr, 'linting before fixing', $invalidErrors);
         }
         if (count($exceptionErrors) > 0) {
             $this->listErrors($stdErr, 'fixing', $exceptionErrors);
         }
         if (count($lintErrors) > 0) {
             $this->listErrors($stdErr, 'linting after fixing', $lintErrors);
         }
     }
     return $this->calculateExitStatus($resolver->isDryRun(), count($changed) > 0, count($invalidErrors) > 0, count($exceptionErrors) > 0);
 }
Example #8
0
    public function testGenerateComplex()
    {
        $expectedXml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<report>
  <files>
    <file id="1" name="someFile.php">
      <applied_fixers>
        <applied_fixer name="some_fixer_name_here"/>
      </applied_fixers>
      <diff>this text is a diff ;)</diff>
    </file>
    <file id="2" name="anotherFile.php">
      <applied_fixers>
        <applied_fixer name="another_fixer_name_here"/>
      </applied_fixers>
      <diff>another diff here ;)</diff>
    </file>
  </files>
  <time unit="s">
    <total value="1.234"/>
  </time>
  <memory value="2.5" unit="MB"/>
</report>
XML;
        $this->assertXmlStringEqualsXmlString($expectedXml, $this->reporter->generate(ReportSummary::create()->setAddAppliedFixers(true)->setChanged(array('someFile.php' => array('appliedFixers' => array('some_fixer_name_here'), 'diff' => 'this text is a diff ;)'), 'anotherFile.php' => array('appliedFixers' => array('another_fixer_name_here'), 'diff' => 'another diff here ;)')))->setMemory(2.5 * 1024 * 1024)->setTime(1234)));
    }
Example #9
0
    public function testGenerateComplex()
    {
        $expectedJson = <<<'JSON'
{
    "files":[
        {
            "name": "someFile.php",
            "appliedFixers":["some_fixer_name_here"],
            "diff": "this text is a diff ;)"
        },
        {
            "name": "anotherFile.php",
            "appliedFixers":["another_fixer_name_here"],
            "diff": "another diff here ;)"
        }
    ],
    "memory": 2.5,
    "time": {
        "total": 1.234
    }
}
JSON;
        $this->assertJsonStringEqualsJsonString($expectedJson, $this->reporter->generate(ReportSummary::create()->setAddAppliedFixers(true)->setChanged(array('someFile.php' => array('appliedFixers' => array('some_fixer_name_here'), 'diff' => 'this text is a diff ;)'), 'anotherFile.php' => array('appliedFixers' => array('another_fixer_name_here'), 'diff' => 'another diff here ;)')))->setMemory(2.5 * 1024 * 1024)->setTime(1234)));
    }