AbstractFunctionReferenceFixerTest.php 3.0 KB

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