AbstractIntegrationTestCase.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Test;
  12. use PhpCsFixer\Cache\NullCacheManager;
  13. use PhpCsFixer\Differ\SebastianBergmannDiffer;
  14. use PhpCsFixer\Error\Error;
  15. use PhpCsFixer\Error\ErrorsManager;
  16. use PhpCsFixer\FileRemoval;
  17. use PhpCsFixer\Fixer\FixerInterface;
  18. use PhpCsFixer\FixerFactory;
  19. use PhpCsFixer\Linter\CachingLinter;
  20. use PhpCsFixer\Linter\Linter;
  21. use PhpCsFixer\Linter\LinterInterface;
  22. use PhpCsFixer\Linter\ProcessLinter;
  23. use PhpCsFixer\Runner\Runner;
  24. use PhpCsFixer\Tests\TestCase;
  25. use PhpCsFixer\Tokenizer\Tokens;
  26. use PhpCsFixer\WhitespacesFixerConfig;
  27. use Prophecy\Argument;
  28. use Symfony\Component\Filesystem\Exception\IOException;
  29. use Symfony\Component\Filesystem\Filesystem;
  30. use Symfony\Component\Finder\Finder;
  31. use Symfony\Component\Finder\SplFileInfo;
  32. /**
  33. * Integration test base class.
  34. *
  35. * This test searches for '.test' fixture files in the given directory.
  36. * Each fixture file will be parsed and tested against the expected result.
  37. *
  38. * Fixture files have the following format:
  39. *
  40. * --TEST--
  41. * Example test description.
  42. * --RULESET--
  43. * {"@PSR2": true, "strict": true}
  44. * --CONFIG--*
  45. * {"indent": " ", "lineEnding": "\n"}
  46. * --SETTINGS--*
  47. * {"key": "value"} # optional extension point for custom IntegrationTestCase class
  48. * --EXPECT--
  49. * Expected code after fixing
  50. * --INPUT--*
  51. * Code to fix
  52. *
  53. * * Section or any line in it may be omitted.
  54. * ** PHP minimum version. Default to current running php version (no effect).
  55. *
  56. * @author SpacePossum
  57. *
  58. * @internal
  59. */
  60. abstract class AbstractIntegrationTestCase extends TestCase
  61. {
  62. use IsIdenticalConstraint;
  63. /**
  64. * @var null|LinterInterface
  65. */
  66. protected $linter;
  67. /**
  68. * @var null|FileRemoval
  69. */
  70. private static $fileRemoval;
  71. public static function setUpBeforeClass()
  72. {
  73. parent::setUpBeforeClass();
  74. $tmpFile = static::getTempFile();
  75. self::$fileRemoval = new FileRemoval();
  76. self::$fileRemoval->observe($tmpFile);
  77. if (!is_file($tmpFile)) {
  78. $dir = \dirname($tmpFile);
  79. if (!is_dir($dir)) {
  80. $fs = new Filesystem();
  81. $fs->mkdir($dir, 0766);
  82. }
  83. }
  84. }
  85. public static function tearDownAfterClass()
  86. {
  87. parent::tearDownAfterClass();
  88. $tmpFile = static::getTempFile();
  89. self::$fileRemoval->delete($tmpFile);
  90. self::$fileRemoval = null;
  91. }
  92. protected function setUp()
  93. {
  94. parent::setUp();
  95. $this->linter = $this->getLinter();
  96. // @todo remove at 3.0 together with env var itself
  97. if (getenv('PHP_CS_FIXER_TEST_USE_LEGACY_TOKENIZER')) {
  98. Tokens::setLegacyMode(true);
  99. }
  100. }
  101. protected function tearDown()
  102. {
  103. parent::tearDown();
  104. $this->linter = null;
  105. // @todo remove at 3.0
  106. Tokens::setLegacyMode(false);
  107. }
  108. /**
  109. * @dataProvider provideIntegrationCases
  110. *
  111. * @see doTest()
  112. */
  113. public function testIntegration(IntegrationCase $case)
  114. {
  115. $this->doTest($case);
  116. }
  117. /**
  118. * Creates test data by parsing '.test' files.
  119. *
  120. * @return IntegrationCase[][]
  121. */
  122. public function provideIntegrationCases()
  123. {
  124. $fixturesDir = realpath(static::getFixturesDir());
  125. if (!is_dir($fixturesDir)) {
  126. throw new \UnexpectedValueException(sprintf('Given fixture dir "%s" is not a directory.', $fixturesDir));
  127. }
  128. $factory = static::createIntegrationCaseFactory();
  129. $tests = [];
  130. /** @var SplFileInfo $file */
  131. foreach (Finder::create()->files()->in($fixturesDir) as $file) {
  132. if ('test' !== $file->getExtension()) {
  133. continue;
  134. }
  135. $tests[$file->getPathname()] = [
  136. $factory->create($file),
  137. ];
  138. }
  139. return $tests;
  140. }
  141. /**
  142. * @return IntegrationCaseFactoryInterface
  143. */
  144. protected static function createIntegrationCaseFactory()
  145. {
  146. return new IntegrationCaseFactory();
  147. }
  148. /**
  149. * Returns the full path to directory which contains the tests.
  150. *
  151. * @return string
  152. */
  153. protected static function getFixturesDir()
  154. {
  155. throw new \BadMethodCallException('Method "getFixturesDir" must be overridden by the extending class.');
  156. }
  157. /**
  158. * Returns the full path to the temporary file where the test will write to.
  159. *
  160. * @return string
  161. */
  162. protected static function getTempFile()
  163. {
  164. throw new \BadMethodCallException('Method "getTempFile" must be overridden by the extending class.');
  165. }
  166. /**
  167. * Applies the given fixers on the input and checks the result.
  168. *
  169. * It will write the input to a temp file. The file will be fixed by a Fixer instance
  170. * configured with the given fixers. The result is compared with the expected output.
  171. * It checks if no errors were reported during the fixing.
  172. */
  173. protected function doTest(IntegrationCase $case)
  174. {
  175. if (\PHP_VERSION_ID < $case->getRequirement('php')) {
  176. static::markTestSkipped(sprintf('PHP %d (or later) is required for "%s", current "%d".', $case->getRequirement('php'), $case->getFileName(), \PHP_VERSION_ID));
  177. }
  178. $input = $case->getInputCode();
  179. $expected = $case->getExpectedCode();
  180. $input = $case->hasInputCode() ? $input : $expected;
  181. $tmpFile = static::getTempFile();
  182. if (false === @file_put_contents($tmpFile, $input)) {
  183. throw new IOException(sprintf('Failed to write to tmp. file "%s".', $tmpFile));
  184. }
  185. $errorsManager = new ErrorsManager();
  186. $fixers = static::createFixers($case);
  187. $runner = new Runner(
  188. new \ArrayIterator([new \SplFileInfo($tmpFile)]),
  189. $fixers,
  190. new SebastianBergmannDiffer(),
  191. null,
  192. $errorsManager,
  193. $this->linter,
  194. false,
  195. new NullCacheManager()
  196. );
  197. Tokens::clearCache();
  198. $result = $runner->fix();
  199. $changed = array_pop($result);
  200. if (!$errorsManager->isEmpty()) {
  201. $errors = $errorsManager->getExceptionErrors();
  202. static::assertEmpty($errors, sprintf('Errors reported during fixing of file "%s": %s', $case->getFileName(), $this->implodeErrors($errors)));
  203. $errors = $errorsManager->getInvalidErrors();
  204. static::assertEmpty($errors, sprintf('Errors reported during linting before fixing file "%s": %s.', $case->getFileName(), $this->implodeErrors($errors)));
  205. $errors = $errorsManager->getLintErrors();
  206. static::assertEmpty($errors, sprintf('Errors reported during linting after fixing file "%s": %s.', $case->getFileName(), $this->implodeErrors($errors)));
  207. }
  208. if (!$case->hasInputCode()) {
  209. static::assertEmpty(
  210. $changed,
  211. sprintf(
  212. "Expected no changes made to test \"%s\" in \"%s\".\nFixers applied:\n%s.\nDiff.:\n%s.",
  213. $case->getTitle(),
  214. $case->getFileName(),
  215. null === $changed ? '[None]' : implode(',', $changed['appliedFixers']),
  216. null === $changed ? '[None]' : $changed['diff']
  217. )
  218. );
  219. return;
  220. }
  221. static::assertNotEmpty($changed, sprintf('Expected changes made to test "%s" in "%s".', $case->getTitle(), $case->getFileName()));
  222. $fixedInputCode = file_get_contents($tmpFile);
  223. static::assertThat(
  224. $fixedInputCode,
  225. self::createIsIdenticalStringConstraint($expected),
  226. sprintf(
  227. "Expected changes do not match result for \"%s\" in \"%s\".\nFixers applied:\n%s.",
  228. $case->getTitle(),
  229. $case->getFileName(),
  230. null === $changed ? '[None]' : implode(',', $changed['appliedFixers'])
  231. )
  232. );
  233. if (1 < \count($fixers)) {
  234. $tmpFile = static::getTempFile();
  235. if (false === @file_put_contents($tmpFile, $input)) {
  236. throw new IOException(sprintf('Failed to write to tmp. file "%s".', $tmpFile));
  237. }
  238. $runner = new Runner(
  239. new \ArrayIterator([new \SplFileInfo($tmpFile)]),
  240. array_reverse($fixers),
  241. new SebastianBergmannDiffer(),
  242. null,
  243. $errorsManager,
  244. $this->linter,
  245. false,
  246. new NullCacheManager()
  247. );
  248. Tokens::clearCache();
  249. $runner->fix();
  250. $fixedInputCodeWithReversedFixers = file_get_contents($tmpFile);
  251. static::assertRevertedOrderFixing($case, $fixedInputCode, $fixedInputCodeWithReversedFixers);
  252. }
  253. // run the test again with the `expected` part, this should always stay the same
  254. $this->testIntegration(
  255. new IntegrationCase(
  256. $case->getFileName(),
  257. $case->getTitle().' "--EXPECT-- part run"',
  258. $case->getSettings(),
  259. $case->getRequirements(),
  260. $case->getConfig(),
  261. $case->getRuleset(),
  262. $case->getExpectedCode(),
  263. null
  264. )
  265. );
  266. }
  267. /**
  268. * @param string $fixedInputCode
  269. * @param string $fixedInputCodeWithReversedFixers
  270. */
  271. protected static function assertRevertedOrderFixing(IntegrationCase $case, $fixedInputCode, $fixedInputCodeWithReversedFixers)
  272. {
  273. // If output is different depends on rules order - we need to verify that the rules are ordered by priority.
  274. // If not, any order is valid.
  275. if ($fixedInputCode !== $fixedInputCodeWithReversedFixers) {
  276. static::assertGreaterThan(
  277. 1,
  278. \count(array_unique(array_map(
  279. static function (FixerInterface $fixer) {
  280. return $fixer->getPriority();
  281. },
  282. static::createFixers($case)
  283. ))),
  284. sprintf(
  285. 'Rules priorities are not differential enough. If rules would be used in reverse order then final output would be different than the expected one. For that, different priorities must be set up for used rules to ensure stable order of them. In "%s".',
  286. $case->getFileName()
  287. )
  288. );
  289. }
  290. }
  291. /**
  292. * @return FixerInterface[]
  293. */
  294. private static function createFixers(IntegrationCase $case)
  295. {
  296. $config = $case->getConfig();
  297. return FixerFactory::create()
  298. ->registerBuiltInFixers()
  299. ->useRuleSet($case->getRuleset())
  300. ->setWhitespacesConfig(
  301. new WhitespacesFixerConfig($config['indent'], $config['lineEnding'])
  302. )
  303. ->getFixers()
  304. ;
  305. }
  306. /**
  307. * @param Error[] $errors
  308. *
  309. * @return string
  310. */
  311. private function implodeErrors(array $errors)
  312. {
  313. $errorStr = '';
  314. foreach ($errors as $error) {
  315. $source = $error->getSource();
  316. $errorStr .= sprintf("%d: %s%s\n", $error->getType(), $error->getFilePath(), null === $source ? '' : ' '.$source->getMessage()."\n\n".$source->getTraceAsString());
  317. }
  318. return $errorStr;
  319. }
  320. /**
  321. * @return LinterInterface
  322. */
  323. private function getLinter()
  324. {
  325. static $linter = null;
  326. if (null === $linter) {
  327. if (getenv('SKIP_LINT_TEST_CASES')) {
  328. $linterProphecy = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
  329. $linterProphecy
  330. ->lintSource(Argument::type('string'))
  331. ->willReturn($this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class)->reveal())
  332. ;
  333. $linterProphecy
  334. ->lintFile(Argument::type('string'))
  335. ->willReturn($this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class)->reveal())
  336. ;
  337. $linterProphecy
  338. ->isAsync()
  339. ->willReturn(false)
  340. ;
  341. $linter = $linterProphecy->reveal();
  342. } else {
  343. $linter = new CachingLinter(
  344. getenv('FAST_LINT_TEST_CASES') ? new Linter() : new ProcessLinter()
  345. );
  346. }
  347. }
  348. return $linter;
  349. }
  350. }