CachingLinterTest.php 3.1 KB

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