AbstractFunctionReferenceFixerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Tests\Fixtures\FunctionReferenceTestFixer;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  19. */
  20. final class AbstractFunctionReferenceFixerTest extends TestCase
  21. {
  22. /**
  23. * @param null|int[] $expected
  24. *
  25. * @dataProvider provideAbstractFunctionReferenceFixerCases
  26. */
  27. public function testAbstractFunctionReferenceFixer(
  28. ?array $expected,
  29. string $source,
  30. string $functionNameToSearch,
  31. int $start = 0,
  32. ?int $end = null
  33. ): void {
  34. $fixer = new FunctionReferenceTestFixer();
  35. self::assertTrue($fixer->isRisky());
  36. $tokens = Tokens::fromCode($source);
  37. self::assertSame(
  38. $expected,
  39. $fixer->findTest(
  40. $functionNameToSearch,
  41. $tokens,
  42. $start,
  43. $end
  44. )
  45. );
  46. self::assertFalse($tokens->isChanged());
  47. }
  48. public static function provideAbstractFunctionReferenceFixerCases(): iterable
  49. {
  50. yield 'simple case I' => [
  51. [1, 2, 3],
  52. '<?php foo();',
  53. 'foo',
  54. ];
  55. yield 'simple case II' => [
  56. [2, 3, 4],
  57. '<?php \foo();',
  58. 'foo',
  59. ];
  60. yield 'test start offset' => [
  61. null,
  62. '<?php
  63. foo();
  64. bar();
  65. ',
  66. 'foo',
  67. 5,
  68. ];
  69. yield 'test returns only the first candidate' => [
  70. [2, 3, 4],
  71. '<?php
  72. foo();
  73. foo();
  74. foo();
  75. foo();
  76. foo();
  77. ',
  78. 'foo',
  79. ];
  80. yield 'not found I' => [
  81. null,
  82. '<?php foo();',
  83. 'bar',
  84. ];
  85. yield 'not found II' => [
  86. null,
  87. '<?php $foo();',
  88. 'foo',
  89. ];
  90. yield 'not found III' => [
  91. null,
  92. '<?php function foo(){}',
  93. 'foo',
  94. ];
  95. yield 'not found IIIb' => [
  96. null,
  97. '<?php function foo($a){}',
  98. 'foo',
  99. ];
  100. yield 'not found IV' => [
  101. null,
  102. '<?php \A\foo();',
  103. 'foo',
  104. ];
  105. }
  106. }