DirConstantFixerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\Fixer\LanguageConstruct;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Vladimir Reznichenko <kalessil@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  19. * @covers \PhpCsFixer\Fixer\LanguageConstruct\DirConstantFixer
  20. */
  21. final class DirConstantFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix($expected, $input = null)
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. public function provideFixCases()
  34. {
  35. $multiLinePatternToFix = <<<'FIX'
  36. <?php $x =
  37. dirname
  38. (
  39. __FILE__
  40. )
  41. ;
  42. FIX;
  43. $multiLinePatternFixed = <<<'FIXED'
  44. <?php $x =
  45. __DIR__
  46. ;
  47. FIXED;
  48. return [
  49. ['<?php $x = "dirname";'],
  50. ['<?php $x = dirname(__FILE__.".dist");'],
  51. ['<?php $x = ClassA::dirname(__FILE__);'],
  52. ['<?php $x = ScopeA\\dirname(__FILE__);'],
  53. ['<?php $x = namespace\\dirname(__FILE__);'],
  54. ['<?php $x = $object->dirname(__FILE__);'],
  55. ['<?php $x = new \\dirname(__FILE__);'],
  56. ['<?php $x = new dirname(__FILE__);'],
  57. ['<?php $x = new ScopeB\\dirname(__FILE__);'],
  58. ['<?php dirnameSmth(__FILE__);'],
  59. ['<?php smth_dirname(__FILE__);'],
  60. ['<?php "SELECT ... dirname(__FILE__) ...";'],
  61. ['<?php "SELECT ... DIRNAME(__FILE__) ...";'],
  62. ['<?php "test" . "dirname" . "in concatenation";'],
  63. [
  64. '<?php $x = dirname(__DIR__);',
  65. '<?php $x = dirname(dirname(__FILE__));',
  66. ],
  67. [
  68. '<?php $x = __DIR__;',
  69. '<?php $x = dirname(__FILE__);',
  70. ],
  71. [
  72. '<?php $x = __DIR__;',
  73. '<?php $x = \\dirname(__FILE__);',
  74. ],
  75. [
  76. '<?php $x = __DIR__.".dist";',
  77. '<?php $x = dirname(__FILE__).".dist";',
  78. ],
  79. [
  80. '<?php $x = __DIR__.".dist";',
  81. '<?php $x = \\dirname(__FILE__).".dist";',
  82. ],
  83. [$multiLinePatternFixed, $multiLinePatternToFix],
  84. [
  85. '<?php $x = /**//**/ /** x*//**//** */__DIR__/***//*xx*/;',
  86. '<?php $x = /**/dirname/**/ /** x*/(/**//** */__FILE__/***/)/*xx*/;',
  87. ],
  88. [
  89. '<?php
  90. interface Test
  91. {
  92. public function dirname($a);
  93. }',
  94. ],
  95. [
  96. '<?php
  97. interface Test
  98. {
  99. public function &dirname($a);
  100. }',
  101. ],
  102. [
  103. "<?php echo __DIR__\n?>",
  104. "<?php echo dirname\n(\n__FILE__\n)\n?>",
  105. ],
  106. [
  107. '<?php $x =# A
  108. # A1
  109. # B
  110. # C
  111. __DIR__# D
  112. # E
  113. ;# F
  114. ',
  115. '<?php $x =# A
  116. \
  117. # A1
  118. dirname# B
  119. (# C
  120. __FILE__# D
  121. )# E
  122. ;# F
  123. ',
  124. ],
  125. ];
  126. }
  127. }