protected function setUp() { parent::setUp(); self::$savedHeader = HeaderCommentFixer::getHeader(); HeaderCommentFixer::setHeader(self::$testHeader); }
/** * Fixes all files for the given finder. * * @param ConfigInterface $config A ConfigInterface instance * @param bool $dryRun Whether to simulate the changes or not * @param bool $diff Whether to provide diff * * @return array */ public function fix(ConfigInterface $config, $dryRun = false, $diff = false) { $fixers = $this->prepareFixers($config); $changed = array(); $this->stopwatch->openSection(); $fileCacheManager = new FileCacheManager($config->usingCache(), $config->getCacheFile(), $config->getFixers(), HeaderCommentFixer::getHeader()); $processed = array(); foreach ($config->getFinder() as $file) { $name = $this->getFileRelativePathname($file); if (in_array($name, $processed, true)) { continue; } $processed[] = $name; if ($file->isDir() || $file->isLink()) { continue; } $this->stopwatch->start($this->getFileRelativePathname($file)); if ($fixInfo = $this->fixFile($file, $fixers, $dryRun, $diff, $fileCacheManager)) { $changed[$name] = $fixInfo; } $this->stopwatch->stop($this->getFileRelativePathname($file)); } $this->stopwatch->stopSection('fixFile'); return $changed; }
/** * 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 string $testFileName Filename * @param string $testTitle Test title * @param FixerInterface[] $fixers Fixers to use * @param string $input Code to fix * @param string|null $expected Expected result or null if the input is expected not to change */ protected function doTestIntegration($testFileName, $testTitle, $fixers, $input, $expected = null) { $fixer = new Fixer(); $tmpFile = static::getTempFile(); if (false === @file_put_contents($tmpFile, $input)) { throw new IOException(sprintf('Failed to write to tmp. file "%s".', $tmpFile)); } $changed = $fixer->fixFile(new \SplFileInfo($tmpFile), $fixers, false, true, new FileCacheManager(false, null, $fixers, HeaderCommentFixer::getHeader())); $errorsManager = $fixer->getErrorsManager(); if (!$errorsManager->isEmpty()) { $errors = $errorsManager->getExceptionErrors(); $this->assertEmpty($errors, sprintf('Errors reported during fixing: %s', $this->implodeErrors($errors))); $errors = $errorsManager->getInvalidErrors(); $this->assertEmpty($errors, sprintf('Errors reported during linting before fixing: %s.', $this->implodeErrors($errors))); $errors = $errorsManager->getLintErrors(); $this->assertEmpty($errors, sprintf('Errors reported during linting after fixing: %s.', $this->implodeErrors($errors))); } if (null === $expected) { $this->assertEmpty($changed, sprintf("Expected no changes made to test \"%s\" in \"%s\".\nFixers applied:\n\"%s\".\nDiff.:\n\"%s\".", $testTitle, $testFileName, $changed === null ? '[None]' : implode(',', $changed['appliedFixers']), $changed === null ? '[None]' : $changed['diff'])); return; } $this->assertNotEmpty($changed, sprintf('Expected changes made to test "%s" in "%s".', $testTitle, $testFileName)); $this->assertSame($expected, file_get_contents($tmpFile), sprintf('Expected changes do not match result, for "%s" in "%s".', $testTitle, $testFileName)); // run the test again with the `expected` part, this should always stay the same $this->testIntegration($testFileName, $testTitle . ' "--EXPECT-- part run"', $fixers, $expected); }
/** * @return string[] */ public function getFixers() { $fixers = array_merge(isset($this->styleCIConfig['enabled']) ? $this->styleCIConfig['enabled'] : array(), isset($this->styleCIConfig['disabled']) ? array_map(function ($disabledFixer) { return '-' . $disabledFixer; }, $this->styleCIConfig['disabled']) : array()); if (HeaderCommentFixer::getHeader()) { array_push($fixers, 'header_comment'); } return $fixers; }