예제 #1
0
 /**
  * @param Fixer|null           $fixer
  * @param ConfigInterface|null $config
  */
 public function __construct(Fixer $fixer = null, ConfigInterface $config = null)
 {
     $this->defaultConfig = $config ?: new Config();
     $this->eventDispatcher = new EventDispatcher();
     $this->fixer = $fixer ?: new Fixer();
     $this->errorsManager = $this->fixer->getErrorsManager();
     $this->stopwatch = $this->fixer->getStopwatch();
     parent::__construct();
 }
예제 #2
0
 /**
  * @covers Symfony\CS\Fixer::fix
  * @covers Symfony\CS\Fixer::fixFile
  */
 public function testThatFixInvalidFileReportsToErrorManager()
 {
     $fixer = new Fixer();
     $fixer->setLinter(new Linter());
     $config = Config::create()->finder(new \DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'FixerTest' . DIRECTORY_SEPARATOR . 'invalid'))->fixers(array(new \Symfony\CS\Fixer\PSR2\VisibilityFixer(), new \Symfony\CS\Fixer\Symfony\UnusedUseFixer()))->setUsingCache(false);
     $changed = $fixer->fix($config, true, true);
     $pathToInvalidFile = __DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'FixerTest' . DIRECTORY_SEPARATOR . 'invalid' . DIRECTORY_SEPARATOR . 'somefile.php';
     $this->assertCount(0, $changed);
     $errors = $fixer->getErrorsManager()->getInvalidErrors();
     $this->assertCount(1, $errors);
     $error = $errors[0];
     $this->assertInstanceOf('Symfony\\CS\\Error\\Error', $error);
     $this->assertSame(Error::TYPE_INVALID, $error->getType());
     $this->assertSame($pathToInvalidFile, $error->getFilePath());
 }
 /**
  * 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);
 }
 /**
  * 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 array            $requirements Env requirements (PHP, HHVM)
  * @param string           $input        Code to fix
  * @param string|null      $expected     Expected result or null if the input is expected not to change
  */
 protected function doTest($testFileName, $testTitle, $fixers, array $requirements, $input, $expected = null)
 {
     if (defined('HHVM_VERSION') && false === $requirements['hhvm']) {
         $this->markTestSkipped('HHVM is not supported.');
     }
     if (isset($requirements['php']) && version_compare(PHP_VERSION, $requirements['php']) < 0) {
         $this->markTestSkipped(sprintf('PHP %s (or later) is required.', $requirements['php']));
     }
     $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));
     $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, $requirements, $expected);
 }
 /**
  * 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($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;
     if (getenv('LINT_TEST_CASES')) {
         $linter = new Linter();
         $linter->lintSource($input);
     }
     $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), $case->getFixers(), false, true, new FileCacheManager(false, null, $case->getFixers()));
     $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 (!$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), 'All used fixers must not have the same priority, integration tests should cover fixers with different priorities.');
         $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), array_reverse($case->getFixers()), false, true, new FileCacheManager(false, null, $case->getFixers()));
         $fixedInputCodeWithReversedFixers = file_get_contents($tmpFile);
         $this->assertNotSame($fixedInputCode, $fixedInputCodeWithReversedFixers, '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.');
     }
     // 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));
 }