/** * @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($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)); }