AbstractFunctionReferenceFixerTest.php 3.1 KB

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