AbstractFunctionReferenceFixerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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;
  13. use PhpCsFixer\AbstractFunctionReferenceFixer;
  14. use PhpCsFixer\AccessibleObject\AccessibleObject;
  15. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  21. */
  22. final class AbstractFunctionReferenceFixerTest extends TestCase
  23. {
  24. /**
  25. * @param null|list<int> $expected
  26. *
  27. * @dataProvider provideAbstractFunctionReferenceFixerCases
  28. */
  29. public function testAbstractFunctionReferenceFixer(
  30. ?array $expected,
  31. string $source,
  32. string $functionNameToSearch,
  33. int $start = 0,
  34. ?int $end = null
  35. ): void {
  36. $fixer = $this->createAbstractFunctionReferenceFixerDouble();
  37. self::assertTrue($fixer->isRisky());
  38. $tokens = Tokens::fromCode($source);
  39. self::assertSame(
  40. $expected,
  41. AccessibleObject::create($fixer)->find(
  42. $functionNameToSearch,
  43. $tokens,
  44. $start,
  45. $end
  46. )
  47. );
  48. self::assertFalse($tokens->isChanged());
  49. }
  50. public static function provideAbstractFunctionReferenceFixerCases(): iterable
  51. {
  52. yield 'simple case I' => [
  53. [1, 2, 3],
  54. '<?php foo();',
  55. 'foo',
  56. ];
  57. yield 'simple case II' => [
  58. [2, 3, 4],
  59. '<?php \foo();',
  60. 'foo',
  61. ];
  62. yield 'test start offset' => [
  63. null,
  64. '<?php
  65. foo();
  66. bar();
  67. ',
  68. 'foo',
  69. 5,
  70. ];
  71. yield 'test returns only the first candidate' => [
  72. [2, 3, 4],
  73. '<?php
  74. foo();
  75. foo();
  76. foo();
  77. foo();
  78. foo();
  79. ',
  80. 'foo',
  81. ];
  82. yield 'not found I' => [
  83. null,
  84. '<?php foo();',
  85. 'bar',
  86. ];
  87. yield 'not found II' => [
  88. null,
  89. '<?php $foo();',
  90. 'foo',
  91. ];
  92. yield 'not found III' => [
  93. null,
  94. '<?php function foo(){}',
  95. 'foo',
  96. ];
  97. yield 'not found IIIb' => [
  98. null,
  99. '<?php function foo($a){}',
  100. 'foo',
  101. ];
  102. yield 'not found IV' => [
  103. null,
  104. '<?php \A\foo();',
  105. 'foo',
  106. ];
  107. }
  108. private function createAbstractFunctionReferenceFixerDouble(): AbstractFunctionReferenceFixer
  109. {
  110. return new class extends AbstractFunctionReferenceFixer {
  111. public function getDefinition(): FixerDefinitionInterface
  112. {
  113. throw new \BadMethodCallException('Not implemented.');
  114. }
  115. public function isCandidate(Tokens $tokens): bool
  116. {
  117. throw new \BadMethodCallException('Not implemented.');
  118. }
  119. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  120. {
  121. throw new \BadMethodCallException('Not implemented.');
  122. }
  123. };
  124. }
  125. }