Example #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->errorsManager = new ErrorsManager();
     $this->stopwatch = new Stopwatch();
     $this->fixer = $fixer ?: new Fixer();
     $this->fixer->registerBuiltInFixers();
     $this->fixer->registerBuiltInConfigs();
     $this->fixer->setStopwatch($this->stopwatch);
     $this->fixer->setErrorsManager($this->errorsManager);
     parent::__construct();
 }
 /**
  * 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 doTestIntegration($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']));
     }
     $errorsManager = new ErrorsManager();
     $fixer = new Fixer();
     $fixer->setErrorsManager($errorsManager);
     $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));
     $this->assertTrue($errorsManager->isEmpty(), 'Errors reported during fixing.');
     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);
 }