FileCachingLintingFileIteratorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Runner;
  13. use PhpCsFixer\Linter\LinterInterface;
  14. use PhpCsFixer\Linter\LintingResultInterface;
  15. use PhpCsFixer\Runner\FileCachingLintingFileIterator;
  16. use PhpCsFixer\Tests\TestCase;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Runner\FileCachingLintingFileIterator
  21. */
  22. final class FileCachingLintingFileIteratorTest extends TestCase
  23. {
  24. public function testLintingEmpty(): void
  25. {
  26. $iterator = new \ArrayIterator([]);
  27. $fileCachingLintingIterator = new FileCachingLintingFileIterator(
  28. $iterator,
  29. $this->createLinterDouble()
  30. );
  31. self::assertNull($fileCachingLintingIterator->current());
  32. self::assertSame($iterator, $fileCachingLintingIterator->getInnerIterator());
  33. self::assertFalse($fileCachingLintingIterator->valid());
  34. }
  35. public function testLintingNonEmpty(): void
  36. {
  37. $files = [
  38. new \SplFileInfo(__FILE__),
  39. new \SplFileInfo(__FILE__),
  40. new \SplFileInfo(__FILE__),
  41. ];
  42. $lintingResult = new class implements LintingResultInterface {
  43. public function check(): void
  44. {
  45. throw new \LogicException('Not implemented.');
  46. }
  47. };
  48. $iterator = new \ArrayIterator($files);
  49. $fileCachingLintingIterator = new FileCachingLintingFileIterator(
  50. $iterator,
  51. $this->createLinterDouble($lintingResult)
  52. );
  53. self::assertLintingIteratorIteration($fileCachingLintingIterator, $lintingResult, ...$files);
  54. $fileCachingLintingIterator->rewind();
  55. self::assertLintingIteratorIteration($fileCachingLintingIterator, $lintingResult, ...$files);
  56. }
  57. private static function assertLintingIteratorIteration(
  58. FileCachingLintingFileIterator $fileCachingLintingIterator,
  59. LintingResultInterface $lintingResultInterface,
  60. \SplFileInfo ...$files
  61. ): void {
  62. $iterations = 0;
  63. foreach ($fileCachingLintingIterator as $index => $lintedFile) {
  64. self::assertSame($lintingResultInterface, $fileCachingLintingIterator->currentLintingResult());
  65. self::assertSame($files[$index], $lintedFile);
  66. ++$iterations;
  67. }
  68. self::assertSame(\count($files), $iterations);
  69. }
  70. private function createLinterDouble(?LintingResultInterface $lintingResult = null): LinterInterface
  71. {
  72. return new class($lintingResult) implements LinterInterface {
  73. private ?LintingResultInterface $lintingResult;
  74. public function __construct(?LintingResultInterface $lintingResult)
  75. {
  76. $this->lintingResult = $lintingResult;
  77. }
  78. public function isAsync(): bool
  79. {
  80. throw new \LogicException('Not implemented.');
  81. }
  82. public function lintFile(string $path): LintingResultInterface
  83. {
  84. return $this->lintingResult;
  85. }
  86. public function lintSource(string $source): LintingResultInterface
  87. {
  88. throw new \LogicException('Not implemented.');
  89. }
  90. };
  91. }
  92. }