AbstractFunctionReferenceFixerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. private $fixer;
  23. protected function setUp(): void
  24. {
  25. $this->fixer = new FunctionReferenceTestFixer();
  26. parent::setUp();
  27. }
  28. protected function tearDown(): void
  29. {
  30. $this->fixer = null;
  31. parent::tearDown();
  32. }
  33. /**
  34. * @param null|int[] $expected
  35. *
  36. * @dataProvider provideAbstractFunctionReferenceFixerCases
  37. */
  38. public function testAbstractFunctionReferenceFixer(
  39. ?array $expected,
  40. string $source,
  41. string $functionNameToSearch,
  42. int $start = 0,
  43. ?int $end = null
  44. ): void {
  45. static::assertTrue($this->fixer->isRisky());
  46. $tokens = Tokens::fromCode($source);
  47. static::assertSame(
  48. $expected,
  49. $this->fixer->findTest(
  50. $functionNameToSearch,
  51. $tokens,
  52. $start,
  53. $end
  54. )
  55. );
  56. static::assertFalse($tokens->isChanged());
  57. }
  58. public function provideAbstractFunctionReferenceFixerCases(): array
  59. {
  60. return [
  61. 'simple case I' => [
  62. [1, 2, 3],
  63. '<?php foo();',
  64. 'foo',
  65. ],
  66. 'simple case II' => [
  67. [2, 3, 4],
  68. '<?php \foo();',
  69. 'foo',
  70. ],
  71. 'test start offset' => [
  72. null,
  73. '<?php
  74. foo();
  75. bar();
  76. ',
  77. 'foo',
  78. 5,
  79. ],
  80. 'test returns only the first candidate' => [
  81. [2, 3, 4],
  82. '<?php
  83. foo();
  84. foo();
  85. foo();
  86. foo();
  87. foo();
  88. ',
  89. 'foo',
  90. ],
  91. 'not found I' => [
  92. null,
  93. '<?php foo();',
  94. 'bar',
  95. ],
  96. 'not found II' => [
  97. null,
  98. '<?php $foo();',
  99. 'foo',
  100. ],
  101. 'not found III' => [
  102. null,
  103. '<?php function foo(){}',
  104. 'foo',
  105. ],
  106. 'not found IIIb' => [
  107. null,
  108. '<?php function foo($a){}',
  109. 'foo',
  110. ],
  111. 'not found IV' => [
  112. null,
  113. '<?php \A\foo();',
  114. 'foo',
  115. ],
  116. ];
  117. }
  118. }