FopenFlagOrderFixerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @internal
  16. *
  17. * @covers \PhpCsFixer\AbstractFopenFlagFixer
  18. * @covers \PhpCsFixer\Fixer\FunctionNotation\FopenFlagOrderFixer
  19. *
  20. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FopenFlagOrderFixer>
  21. */
  22. final class FopenFlagOrderFixerTest 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. /**
  32. * @return iterable<int|string, array{0: string, 1?: string}>
  33. */
  34. public static function provideFixCases(): iterable
  35. {
  36. yield 'most simple fix case' => [
  37. '<?php
  38. $a = fopen($foo, \'rw+b\');
  39. ',
  40. '<?php
  41. $a = fopen($foo, \'brw+\');
  42. ',
  43. ];
  44. yield '"fopen" casing insensitive' => [
  45. '<?php
  46. $a = \FOPen($foo, "cr+w+b");
  47. $a = \FOPEN($foo, "crw+b");
  48. ',
  49. '<?php
  50. $a = \FOPen($foo, "bw+r+c");
  51. $a = \FOPEN($foo, "bw+rc");
  52. ',
  53. ];
  54. yield 'comments around flags' => [
  55. '<?php
  56. $a = fopen($foo,/*0*/\'rb\'/*1*/);
  57. ',
  58. '<?php
  59. $a = fopen($foo,/*0*/\'br\'/*1*/);
  60. ',
  61. ];
  62. yield 'binary string' => [
  63. '<?php
  64. $a = \fopen($foo, b"cr+w+b");
  65. $b = \fopen($foo, B"crw+b");
  66. $c = \fopen($foo, b\'cr+w+b\');
  67. $d = \fopen($foo, B\'crw+b\');
  68. ',
  69. '<?php
  70. $a = \fopen($foo, b"bw+r+c");
  71. $b = \fopen($foo, B"bw+rc");
  72. $c = \fopen($foo, b\'bw+r+c\');
  73. $d = \fopen($foo, B\'bw+rc\');
  74. ',
  75. ];
  76. yield 'common typos' => [
  77. '<?php
  78. $a = fopen($a, "b+r");
  79. $b = fopen($b, \'b+w\');
  80. ',
  81. ];
  82. // `t` cases
  83. yield [
  84. '<?php
  85. $a = fopen($foo, \'rw+t\');
  86. ',
  87. '<?php
  88. $a = fopen($foo, \'trw+\');
  89. ',
  90. ];
  91. yield [
  92. '<?php
  93. $a = \fopen($foo, \'rw+tb\');
  94. ',
  95. '<?php
  96. $a = \fopen($foo, \'btrw+\');
  97. ',
  98. ];
  99. // don't fix cases
  100. yield 'single flag' => [
  101. '<?php
  102. $a = fopen($foo, "r");
  103. $a = fopen($foo, "r+");
  104. ',
  105. ];
  106. yield 'not simple flags' => [
  107. '<?php
  108. $a = fopen($foo, "br+".$a);
  109. ',
  110. ];
  111. yield 'wrong # of arguments' => [
  112. '<?php
  113. $b = \fopen("br+");
  114. $c = fopen($foo, "bw+", 1, 2 , 3);
  115. ',
  116. ];
  117. yield '"flags" is too long (must be overridden)' => [
  118. '<?php
  119. $d = fopen($foo, "r+w+a+x+c+etbX");
  120. ',
  121. ];
  122. yield 'static method call' => [
  123. '<?php
  124. $e = A::fopen($foo, "bw+");
  125. ',
  126. ];
  127. yield 'method call' => [
  128. '<?php
  129. $f = $b->fopen($foo, "br+");
  130. ',
  131. ];
  132. yield 'comments, PHPDoc and literal' => [
  133. '<?php
  134. // fopen($foo, "brw");
  135. /* fopen($foo, "brw"); */
  136. echo("fopen($foo, \"brw\")");
  137. ',
  138. ];
  139. yield 'invalid flag values' => [
  140. '<?php
  141. $a = fopen($foo, \'\');
  142. $a = fopen($foo, \'x\'); // ok but should not mark collection as changed
  143. $a = fopen($foo, \'k\');
  144. $a = fopen($foo, \'kz\');
  145. $a = fopen($foo, \'k+\');
  146. $a = fopen($foo, \'+k\');
  147. $a = fopen($foo, \'xc++\');
  148. $a = fopen($foo, \'w+r+r+\');
  149. $a = fopen($foo, \'+brw+\');
  150. $a = fopen($foo, \'b+rw\');
  151. $a = fopen($foo, \'bbrw+\');
  152. $a = fopen($foo, \'brw++\');
  153. $a = fopen($foo, \'++brw\');
  154. $a = fopen($foo, \'ybrw+\');
  155. $a = fopen($foo, \'rr\');
  156. $a = fopen($foo, \'ロ\');
  157. $a = fopen($foo, \'ロ+\');
  158. $a = \fopen($foo, \'rロ\');
  159. $a = \fopen($foo, \'w+ロ\');
  160. ',
  161. ];
  162. }
  163. }