FopenFlagOrderFixerTest.php 5.0 KB

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