FileLintingIteratorTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\FileLintingIterator;
  16. use PhpCsFixer\Tests\TestCase;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Runner\FileLintingIterator
  21. */
  22. final class FileLintingIteratorTest extends TestCase
  23. {
  24. public function testFileLintingIteratorEmpty(): void
  25. {
  26. $fileLintingIteratorProphecy = $this->prophesize(LinterInterface::class);
  27. $iterator = new \ArrayIterator([]);
  28. $fileLintingIterator = new FileLintingIterator(
  29. $iterator,
  30. $fileLintingIteratorProphecy->reveal()
  31. );
  32. self::assertNull($fileLintingIterator->current());
  33. self::assertNull($fileLintingIterator->currentLintingResult());
  34. self::assertSame($iterator, $fileLintingIterator->getInnerIterator());
  35. self::assertFalse($fileLintingIterator->valid());
  36. }
  37. public function testFileLintingIterator(): void
  38. {
  39. $file = new \SplFileInfo(__FILE__);
  40. $fileLintingIteratorProphecy = $this->prophesize(LinterInterface::class);
  41. $lintingResultInterfaceProphecy = $this->prophesize(LintingResultInterface::class)->reveal();
  42. $fileLintingIteratorProphecy->lintFile($file)->willReturn($lintingResultInterfaceProphecy);
  43. $iterator = new \ArrayIterator([$file]);
  44. $fileLintingIterator = new FileLintingIterator(
  45. $iterator,
  46. $fileLintingIteratorProphecy->reveal()
  47. );
  48. // test when not touched current is null
  49. self::assertNull($fileLintingIterator->currentLintingResult());
  50. // test iterating
  51. $this->fileLintingIteratorIterationTest($fileLintingIterator, $file, $lintingResultInterfaceProphecy);
  52. // rewind and test again
  53. $fileLintingIterator->rewind();
  54. $this->fileLintingIteratorIterationTest($fileLintingIterator, $file, $lintingResultInterfaceProphecy);
  55. }
  56. private function fileLintingIteratorIterationTest(
  57. FileLintingIterator $fileLintingIterator,
  58. \SplFileInfo $file,
  59. LintingResultInterface $lintingResultInterface
  60. ): void {
  61. $iterations = 0;
  62. foreach ($fileLintingIterator as $lintedFile) {
  63. self::assertSame($file, $lintedFile);
  64. self::assertSame($lintingResultInterface, $fileLintingIterator->currentLintingResult());
  65. ++$iterations;
  66. }
  67. self::assertSame(1, $iterations);
  68. $fileLintingIterator->next();
  69. self::assertNull($fileLintingIterator->currentLintingResult());
  70. }
  71. }