DirConstantFixerTest.php 4.8 KB

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