DirConstantFixerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\Fixer\LanguageConstruct;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Vladimir Reznichenko <kalessil@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  20. * @covers \PhpCsFixer\Fixer\LanguageConstruct\DirConstantFixer
  21. */
  22. final class DirConstantFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. public static function provideFixCases(): iterable
  32. {
  33. $multiLinePatternToFix = <<<'FIX'
  34. <?php $x =
  35. dirname
  36. (
  37. __FILE__
  38. )
  39. ;
  40. FIX;
  41. $multiLinePatternFixed = <<<'FIXED'
  42. <?php $x =
  43. __DIR__
  44. ;
  45. FIXED;
  46. yield ['<?php $x = "dirname";'];
  47. yield ['<?php $x = dirname(__FILE__.".dist");'];
  48. yield ['<?php $x = ClassA::dirname(__FILE__);'];
  49. yield ['<?php $x = ScopeA\\dirname(__FILE__);'];
  50. yield ['<?php $x = namespace\\dirname(__FILE__);'];
  51. yield ['<?php $x = $object->dirname(__FILE__);'];
  52. yield ['<?php $x = new \\dirname(__FILE__);'];
  53. yield ['<?php $x = new dirname(__FILE__);'];
  54. yield ['<?php $x = new ScopeB\\dirname(__FILE__);'];
  55. yield ['<?php dirnameSmth(__FILE__);'];
  56. yield ['<?php smth_dirname(__FILE__);'];
  57. yield ['<?php "SELECT ... dirname(__FILE__) ...";'];
  58. yield ['<?php "SELECT ... DIRNAME(__FILE__) ...";'];
  59. yield ['<?php "test" . "dirname" . "in concatenation";'];
  60. yield [
  61. '<?php $x = dirname(__DIR__);',
  62. '<?php $x = dirname(dirname(__FILE__));',
  63. ];
  64. yield [
  65. '<?php $x = __DIR__;',
  66. '<?php $x = dirname(__FILE__);',
  67. ];
  68. yield [
  69. '<?php $x = /* A */ __DIR__ /* B */;',
  70. '<?php $x = dirname ( /* A */ __FILE__ ) /* B */;',
  71. ];
  72. yield [
  73. '<?php $x = __DIR__;',
  74. '<?php $x = \dirname(__FILE__);',
  75. ];
  76. yield [
  77. '<?php $x = __DIR__.".dist";',
  78. '<?php $x = dirname(__FILE__).".dist";',
  79. ];
  80. yield [
  81. '<?php $x = __DIR__.".dist";',
  82. '<?php $x = \dirname(__FILE__).".dist";',
  83. ];
  84. yield [
  85. '<?php $x = /* 0 *//* 1 */ /** x2*//*3*//** 4*/__DIR__/**5*//*xx*/;',
  86. '<?php $x = /* 0 */dirname/* 1 */ /** x2*/(/*3*//** 4*/__FILE__/**5*/)/*xx*/;',
  87. ];
  88. yield [
  89. '<?php
  90. interface Test
  91. {
  92. public function dirname($a);
  93. }',
  94. ];
  95. yield [
  96. '<?php
  97. interface Test
  98. {
  99. public function &dirname($a);
  100. }',
  101. ];
  102. yield [
  103. "<?php echo __DIR__\n?>",
  104. "<?php echo dirname\n(\n__FILE__\n)\n?>",
  105. ];
  106. yield [
  107. "<?php echo __DIR__/*1*/\n?>",
  108. "<?php echo dirname\n(\n__FILE__/*1*/\n)\n?>",
  109. ];
  110. yield [
  111. $multiLinePatternFixed,
  112. $multiLinePatternToFix,
  113. ];
  114. yield [
  115. '<?php $x = __DIR__;',
  116. '<?php $x = \dirname(
  117. __FILE__ '.'
  118. );',
  119. ];
  120. yield [
  121. '<?php
  122. $x = dirname(dirname("a".__FILE__));
  123. $x = dirname(dirname(__FILE__."a"));
  124. $x = dirname(dirname("a".__FILE__."a"));
  125. ',
  126. ];
  127. yield [
  128. '<?php $x = __DIR__.".dist";',
  129. '<?php $x = dirname(__FILE__, ).".dist";',
  130. ];
  131. yield [
  132. '<?php $x = __DIR__/* a */ /* b */ .".dist";',
  133. '<?php $x = \dirname(__FILE__/* a */, /* b */) .".dist";',
  134. ];
  135. yield [
  136. '<?php $x = __DIR__;',
  137. '<?php $x = \dirname(
  138. __FILE__ , '.'
  139. );',
  140. ];
  141. }
  142. /**
  143. * @requires PHP <8.0
  144. */
  145. public function testFixPrePHP80(): void
  146. {
  147. $this->doTest(
  148. '<?php $x =# A
  149. # A1
  150. # B
  151. # C
  152. __DIR__# D
  153. # E
  154. ;# F
  155. ',
  156. '<?php $x =# A
  157. \
  158. # A1
  159. dirname# B
  160. (# C
  161. __FILE__# D
  162. )# E
  163. ;# F
  164. '
  165. );
  166. }
  167. }