Author: Dariusz RumiƄski (dariusz.ruminski@gmail.com)
Example #1
0
 /**
  * @param string $fileName
  * @param string $content
  *
  * @return IntegrationCase
  */
 public function create($fileName, $content)
 {
     try {
         if (!preg_match('/--TEST--\\n(?<title>.*?)\\s--CONFIG--\\n(?<config>.*?)(\\s--SETTINGS--\\n(?<settings>.*?))?(\\s--REQUIREMENTS--\\n(?<requirements>.*?))?\\s--EXPECT--\\n(?<expect>.*?\\n*)(?:\\n--INPUT--\\s(?<input>.*)|$)/s', $content, $match)) {
             throw new \InvalidArgumentException('File format is invalid.');
         }
         return IntegrationCase::create()->setFileName($fileName)->setTitle($match['title'])->setFixers($this->determineFixers($match['config']))->setRequirements($this->determineRequirements($match['requirements']))->setSettings($this->determineSettings($match['settings']))->setExpectedCode($match['expect'])->setInputCode(isset($match['input']) ? $match['input'] : null);
     } catch (\InvalidArgumentException $e) {
         throw new \InvalidArgumentException(sprintf('%s Test file: "%s".', $e->getMessage(), $fileName), $e->getCode(), $e);
     }
 }
 /**
  * Applies the given fixers on the input and checks the result.
  *
  * It will write the input to a temp file. The file will be fixed by a Fixer instance
  * configured with the given fixers. The result is compared with the expected output.
  * It checks if no errors were reported during the fixing.
  *
  * @param IntegrationCase $case
  */
 protected function doTest(IntegrationCase $case)
 {
     if (defined('HHVM_VERSION') && false === $case->getRequirement('hhvm')) {
         $this->markTestSkipped('HHVM is not supported.');
     }
     if (version_compare(PHP_VERSION, $case->getRequirement('php')) < 0) {
         $this->markTestSkipped(sprintf('PHP %s (or later) is required.', $case->getRequirement('php')));
     }
     $input = $case->getInputCode();
     $expected = $case->getExpectedCode();
     $input = $case->hasInputCode() ? $input : $expected;
     $this->assertNull($this->lintSource($input));
     $tmpFile = static::getTempFile();
     if (false === @file_put_contents($tmpFile, $input)) {
         throw new IOException(sprintf('Failed to write to tmp. file "%s".', $tmpFile));
     }
     $errorsManager = new ErrorsManager();
     $configProphecy = $this->prophesize('PhpCsFixer\\ConfigInterface');
     $configProphecy->usingCache()->willReturn(false);
     $configProphecy->getCacheFile()->willReturn(null);
     $configProphecy->usingLinter()->willReturn(false);
     $configProphecy->getRules()->willReturn(array());
     $configProphecy->getFinder()->willReturn(new \ArrayIterator(array(new \SplFileInfo($tmpFile))));
     $configProphecy->getFixers()->willReturn($case->getFixers());
     $runner = new Runner($configProphecy->reveal(), new SebastianBergmannDiffer(), null, $errorsManager, new NullLinter(), false);
     $result = $runner->fix();
     $changed = array_pop($result);
     if (!$errorsManager->isEmpty()) {
         $errors = $errorsManager->getExceptionErrors();
         $this->assertEmpty($errors, sprintf('Errors reported during fixing of file "%s": %s', $case->getFileName(), $this->implodeErrors($errors)));
         $errors = $errorsManager->getInvalidErrors();
         $this->assertEmpty($errors, sprintf('Errors reported during linting before fixing file "%s": %s.', $case->getFileName(), $this->implodeErrors($errors)));
         $errors = $errorsManager->getLintErrors();
         $this->assertEmpty($errors, sprintf('Errors reported during linting after fixing file "%s": %s.', $case->getFileName(), $this->implodeErrors($errors)));
     }
     if (!$case->hasInputCode()) {
         $this->assertEmpty($changed, sprintf("Expected no changes made to test \"%s\" in \"%s\".\nFixers applied:\n\"%s\".\nDiff.:\n\"%s\".", $case->getTitle(), $case->getFileName(), $changed === null ? '[None]' : implode(',', $changed['appliedFixers']), $changed === null ? '[None]' : $changed['diff']));
         return;
     }
     $this->assertNotEmpty($changed, sprintf('Expected changes made to test "%s" in "%s".', $case->getTitle(), $case->getFileName()));
     $fixedInputCode = file_get_contents($tmpFile);
     $this->assertSame($expected, $fixedInputCode, sprintf('Expected changes do not match result for "%s" in "%s".', $case->getTitle(), $case->getFileName()));
     if ($case->shouldCheckPriority()) {
         $priorities = array_map(function (FixerInterface $fixer) {
             return $fixer->getPriority();
         }, $case->getFixers());
         $this->assertNotCount(1, array_unique($priorities), sprintf('All used fixers must not have the same priority, integration tests should cover fixers with different priorities. In "%s".', $case->getFileName()));
         $tmpFile = static::getTempFile();
         if (false === @file_put_contents($tmpFile, $input)) {
             throw new IOException(sprintf('Failed to write to tmp. file "%s".', $tmpFile));
         }
         $configProphecy->getFinder()->willReturn(new \ArrayIterator(array(new \SplFileInfo($tmpFile))));
         $configProphecy->getFixers()->willReturn(array_reverse($case->getFixers()));
         $runner->fix();
         $fixedInputCodeWithReversedFixers = file_get_contents($tmpFile);
         $this->assertNotSame($fixedInputCode, $fixedInputCodeWithReversedFixers, sprintf('Set priorities must be significant. If fixers used in reverse order return same output then the integration test is not sufficient or the priority relation between used fixers should not be set. In "%s".', $case->getFileName()));
     }
     // run the test again with the `expected` part, this should always stay the same
     $this->testIntegration($case->setTitle($case->getTitle() . ' "--EXPECT-- part run"')->setInputCode(null));
 }
 /**
  * @param IntegrationCase $case
  *
  * @return FixerInterface[]
  */
 private function createFixers(IntegrationCase $case)
 {
     $config = $case->getConfig();
     return FixerFactory::create()->registerBuiltInFixers()->useRuleSet($case->getRuleset())->setWhitespacesConfig(new WhitespacesFixerConfig($config['indent'], $config['lineEnding']))->getFixers();
 }