CombineNestedDirnameFixerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Gregor Harlan
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\FunctionNotation\CombineNestedDirnameFixer
  20. */
  21. final class CombineNestedDirnameFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public static function provideFixCases(): iterable
  31. {
  32. yield [
  33. '<?php dirname();',
  34. ];
  35. yield [
  36. '<?php dirname($path);',
  37. ];
  38. yield [
  39. '<?php dirname($path, 3);',
  40. ];
  41. yield [
  42. '<?php dirname($path, 2);',
  43. '<?php dirname(dirname($path));',
  44. ];
  45. yield [
  46. '<?php dirname /* a */ ( /* b */ /* c */ $path /* d */, 2);',
  47. '<?php dirname /* a */ ( /* b */ dirname( /* c */ $path) /* d */);',
  48. ];
  49. yield [
  50. '<?php dirname($path, 3);',
  51. '<?php dirname(\dirname(dirname($path)));',
  52. ];
  53. yield [
  54. '<?php dirname($path, 4);',
  55. '<?php dirname(dirname($path, 3));',
  56. ];
  57. yield [
  58. '<?php dirname($path, 4);',
  59. '<?php dirname(dirname($path), 3);',
  60. ];
  61. yield [
  62. '<?php dirname($path, 5);',
  63. '<?php dirname(dirname($path, 2), 3);',
  64. ];
  65. yield [
  66. '<?php dirname($path, 5);',
  67. '<?php dirname(dirname(dirname($path), 3));',
  68. ];
  69. yield [
  70. '<?php dirname(dirname($path, $level));',
  71. ];
  72. yield [
  73. '<?php dirname("foo/".dirname($path));',
  74. ];
  75. yield [
  76. '<?php dirname(dirname($path).$foo);',
  77. ];
  78. yield [
  79. '<?php foo\dirname(dirname($path));',
  80. ];
  81. yield [
  82. '<?php dirname(foo(dirname($path, 2)), 2);',
  83. '<?php dirname(dirname(foo(dirname(dirname($path)))));',
  84. ];
  85. yield [
  86. '<?php new dirname(dirname($path, 2));',
  87. '<?php new dirname(dirname(dirname($path)));',
  88. ];
  89. yield [
  90. '<?php dirname($path, 3);',
  91. '<?php dirname(dirname(dirname($path, ), ));',
  92. ];
  93. yield [
  94. '<?php dirname($path, 3);',
  95. '<?php dirname(dirname(dirname($path, ), ), );',
  96. ];
  97. }
  98. /**
  99. * @dataProvider provideFix81Cases
  100. *
  101. * @requires PHP 8.1
  102. */
  103. public function testFix81(string $expected, ?string $input = null): void
  104. {
  105. $this->doTest($expected, $input);
  106. }
  107. public static function provideFix81Cases(): iterable
  108. {
  109. yield ['<?php $a = dirname(dirname(...));'];
  110. }
  111. }