AbstractFunctionReferenceFixerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. static::assertTrue($fixer->isRisky());
  36. $tokens = Tokens::fromCode($source);
  37. static::assertSame(
  38. $expected,
  39. $fixer->findTest(
  40. $functionNameToSearch,
  41. $tokens,
  42. $start,
  43. $end
  44. )
  45. );
  46. static::assertFalse($tokens->isChanged());
  47. }
  48. public function provideAbstractFunctionReferenceFixerCases(): array
  49. {
  50. return [
  51. 'simple case I' => [
  52. [1, 2, 3],
  53. '<?php foo();',
  54. 'foo',
  55. ],
  56. 'simple case II' => [
  57. [2, 3, 4],
  58. '<?php \foo();',
  59. 'foo',
  60. ],
  61. 'test start offset' => [
  62. null,
  63. '<?php
  64. foo();
  65. bar();
  66. ',
  67. 'foo',
  68. 5,
  69. ],
  70. 'test returns only the first candidate' => [
  71. [2, 3, 4],
  72. '<?php
  73. foo();
  74. foo();
  75. foo();
  76. foo();
  77. foo();
  78. ',
  79. 'foo',
  80. ],
  81. 'not found I' => [
  82. null,
  83. '<?php foo();',
  84. 'bar',
  85. ],
  86. 'not found II' => [
  87. null,
  88. '<?php $foo();',
  89. 'foo',
  90. ],
  91. 'not found III' => [
  92. null,
  93. '<?php function foo(){}',
  94. 'foo',
  95. ],
  96. 'not found IIIb' => [
  97. null,
  98. '<?php function foo($a){}',
  99. 'foo',
  100. ],
  101. 'not found IV' => [
  102. null,
  103. '<?php \A\foo();',
  104. 'foo',
  105. ],
  106. ];
  107. }
  108. }