LintingFileIteratorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\LintingFileIterator;
  16. use PhpCsFixer\Tests\TestCase;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Runner\LintingFileIterator
  21. */
  22. final class LintingFileIteratorTest extends TestCase
  23. {
  24. public function testFileLintingIteratorEmpty(): void
  25. {
  26. $iterator = new \ArrayIterator([]);
  27. $lintingFileIterator = new LintingFileIterator(
  28. $iterator,
  29. $this->createLinterDouble()
  30. );
  31. self::assertNull($lintingFileIterator->current());
  32. self::assertNull($lintingFileIterator->currentLintingResult());
  33. self::assertSame($iterator, $lintingFileIterator->getInnerIterator());
  34. self::assertFalse($lintingFileIterator->valid());
  35. }
  36. public function testFileLintingIterator(): void
  37. {
  38. $file = new \SplFileInfo(__FILE__);
  39. $lintingResult = new class implements LintingResultInterface {
  40. public function check(): void
  41. {
  42. throw new \LogicException('Not implemented.');
  43. }
  44. };
  45. $iterator = new \ArrayIterator([$file]);
  46. $lintingFileIterator = new LintingFileIterator(
  47. $iterator,
  48. $this->createLinterDouble($lintingResult)
  49. );
  50. // test when not touched current is null
  51. self::assertNull($lintingFileIterator->currentLintingResult());
  52. // test iterating
  53. $this->lintingFileIteratorIterationTest($lintingFileIterator, $file, $lintingResult);
  54. // rewind and test again
  55. $lintingFileIterator->rewind();
  56. $this->lintingFileIteratorIterationTest($lintingFileIterator, $file, $lintingResult);
  57. }
  58. private function lintingFileIteratorIterationTest(
  59. LintingFileIterator $lintingFileIterator,
  60. \SplFileInfo $file,
  61. LintingResultInterface $lintingResultInterface
  62. ): void {
  63. $iterations = 0;
  64. foreach ($lintingFileIterator as $lintedFile) {
  65. self::assertSame($file, $lintedFile);
  66. self::assertSame($lintingResultInterface, $lintingFileIterator->currentLintingResult());
  67. ++$iterations;
  68. }
  69. self::assertSame(1, $iterations);
  70. $lintingFileIterator->next();
  71. self::assertNull($lintingFileIterator->currentLintingResult());
  72. }
  73. private function createLinterDouble(?LintingResultInterface $lintingResult = null): LinterInterface
  74. {
  75. return new class($lintingResult) implements LinterInterface {
  76. private ?LintingResultInterface $lintingResult;
  77. public function __construct(?LintingResultInterface $lintingResult)
  78. {
  79. $this->lintingResult = $lintingResult;
  80. }
  81. public function isAsync(): bool
  82. {
  83. throw new \LogicException('Not implemented.');
  84. }
  85. public function lintFile(string $path): LintingResultInterface
  86. {
  87. return $this->lintingResult;
  88. }
  89. public function lintSource(string $source): LintingResultInterface
  90. {
  91. throw new \LogicException('Not implemented.');
  92. }
  93. };
  94. }
  95. }