コード例 #1
0
ファイル: LinterTest.php プロジェクト: stof/PHP-CS-Fixer-1
 /**
  * @covers Symfony\CS\Linter\Linter::lintSource
  *
  * @expectedException Symfony\CS\Linter\LintingException
  * @expectedExceptionMessageRegExp /syntax error, unexpected (?:'echo' \(T_ECHO\))|(?:T_ECHO)/
  */
 public function testLintSourceWithBadCode()
 {
     $linter = new Linter();
     $linter->lintSource('<?php echo echo;');
 }
コード例 #2
0
 /**
  * Tests if a fixer fixes a given string to match the expected result.
  *
  * It is used both if you want to test if something is fixed or if it is not touched by the fixer.
  * It also makes sure that the expected output does not change when run through the fixer. That means that you
  * do not need two test cases like [$expected] and [$expected, $input] (where $expected is the same in both cases)
  * as the latter covers both of them.
  * This method throws an exception if $expected and $input are equal to prevent test cases that accidentally do
  * not test anything.
  *
  * @param string              $expected The expected fixer output.
  * @param string|null         $input    The fixer input, or null if it should intentionally be equal to the output.
  * @param \SplFileInfo|null   $file     The file to fix, or null if unneeded.
  * @param FixerInterface|null $fixer    The fixer to be used, or null if it should be inferred from the test name.
  */
 protected function doTest($expected, $input = null, \SplFileInfo $file = null, FixerInterface $fixer = null)
 {
     if ($expected === $input) {
         throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.');
     }
     $linter = null;
     if (getenv('LINT_TEST_CASES')) {
         $linter = new Linter();
     }
     $fixer = $fixer ?: $this->getFixer();
     $file = $file ?: $this->getTestFile();
     $fileIsSupported = $fixer->supports($file);
     if (null !== $input) {
         if ($linter) {
             $linter->lintSource($input);
         }
         Tokens::clearCache();
         $tokens = Tokens::fromCode($input);
         if ($fileIsSupported) {
             $this->assertTrue($fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
             $fixResult = $fixer->fix($file, $tokens);
             $this->assertNull($fixResult, '->fix method must return null.');
         }
         $this->assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');
         $this->assertSame($expected, $tokens->generateCode(), 'Code build on input code must match expected code.');
         Tokens::clearCache();
         $expectedTokens = Tokens::fromCode($expected);
         $tokens->clearEmptyTokens();
         $this->assertTokens($expectedTokens, $tokens);
     }
     if ($linter) {
         $linter->lintSource($input);
     }
     Tokens::clearCache();
     $tokens = Tokens::fromCode($expected);
     if ($fileIsSupported) {
         $fixResult = $fixer->fix($file, $tokens);
         $this->assertNull($fixResult, '->fix method must return null.');
     }
     $this->assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
     $this->assertSame($expected, $tokens->generateCode(), 'Code build on expected code must not change.');
 }
コード例 #3
0
 /**
  * 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));
 }