CachingLinterTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Linter;
  13. use org\bovigo\vfs\vfsStream;
  14. use PhpCsFixer\Linter\CachingLinter;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author ntzm
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Linter\CachingLinter
  22. */
  23. final class CachingLinterTest extends TestCase
  24. {
  25. /**
  26. * @dataProvider provideIsAsyncCases
  27. */
  28. public function testIsAsync(bool $isAsync): void
  29. {
  30. $sublinter = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
  31. $sublinter->isAsync()->willReturn($isAsync);
  32. $linter = new CachingLinter($sublinter->reveal());
  33. static::assertSame($isAsync, $linter->isAsync());
  34. }
  35. public function provideIsAsyncCases()
  36. {
  37. return [
  38. [true],
  39. [false],
  40. ];
  41. }
  42. public function testLintFileIsCalledOnceOnSameContent(): void
  43. {
  44. $fs = vfsStream::setup('root', null, [
  45. 'foo.php' => '<?php echo "baz";',
  46. 'bar.php' => '<?php echo "baz";',
  47. 'baz.php' => '<?php echo "foobarbaz";',
  48. ]);
  49. $result1 = $this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class);
  50. $result2 = $this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class);
  51. $sublinter = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
  52. $sublinter->lintFile($fs->url().'/foo.php')->shouldBeCalledTimes(1)->willReturn($result1->reveal());
  53. $sublinter->lintFile($fs->url().'/bar.php')->shouldNotBeCalled();
  54. $sublinter->lintFile($fs->url().'/baz.php')->shouldBeCalledTimes(1)->willReturn($result2->reveal());
  55. $linter = new CachingLinter($sublinter->reveal());
  56. static::assertSame($result1->reveal(), $linter->lintFile($fs->url().'/foo.php'));
  57. static::assertSame($result1->reveal(), $linter->lintFile($fs->url().'/foo.php'));
  58. static::assertSame($result1->reveal(), $linter->lintFile($fs->url().'/bar.php'));
  59. static::assertSame($result2->reveal(), $linter->lintFile($fs->url().'/baz.php'));
  60. }
  61. public function testLintSourceIsCalledOnceOnSameContent(): void
  62. {
  63. $result1 = $this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class);
  64. $result2 = $this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class);
  65. $sublinter = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
  66. $sublinter->lintSource('<?php echo "baz";')->shouldBeCalledTimes(1)->willReturn($result1->reveal());
  67. $sublinter->lintSource('<?php echo "foobarbaz";')->shouldBeCalledTimes(1)->willReturn($result2->reveal());
  68. $linter = new CachingLinter($sublinter->reveal());
  69. static::assertSame($result1->reveal(), $linter->lintSource('<?php echo "baz";'));
  70. static::assertSame($result1->reveal(), $linter->lintSource('<?php echo "baz";'));
  71. static::assertSame($result2->reveal(), $linter->lintSource('<?php echo "foobarbaz";'));
  72. }
  73. }