FopenFlagsFixerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\FopenFlagsFixer
  19. *
  20. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FopenFlagsFixer>
  21. *
  22. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\FopenFlagsFixer
  23. */
  24. final class FopenFlagsFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @param _AutogeneratedInputConfiguration $config
  28. *
  29. * @dataProvider provideFixCases
  30. */
  31. public function testFix(string $expected, ?string $input = null, array $config = []): void
  32. {
  33. $this->fixer->configure($config);
  34. $this->doTest($expected, $input);
  35. }
  36. public static function provideFixCases(): iterable
  37. {
  38. yield 'missing "b"' => [
  39. '<?php
  40. $a = fopen($foo, \'rw+b\');
  41. ',
  42. '<?php
  43. $a = fopen($foo, \'rw+\');
  44. ',
  45. ];
  46. yield 'has "t" and "b"' => [
  47. '<?php
  48. $a = \fopen($foo, "rw+b");
  49. ',
  50. '<?php
  51. $a = \fopen($foo, "rw+bt");
  52. ',
  53. ];
  54. yield 'has "t" and no "b" and binary string mod' => [
  55. '<?php
  56. $a = fopen($foo, b\'rw+b\');
  57. ',
  58. '<?php
  59. $a = fopen($foo, b\'trw+\');
  60. ',
  61. ];
  62. // configure remove b
  63. yield 'missing "b" but not configured' => [
  64. '<?php
  65. $a = fopen($foo, \'rw+\');
  66. ',
  67. '<?php
  68. $a = fopen($foo, \'rw+t\');
  69. ',
  70. ['b_mode' => false],
  71. ];
  72. yield '"t" and superfluous "b"' => [
  73. '<?php
  74. $a = fopen($foo, \'r+\');
  75. $a = fopen($foo, \'w+r\');
  76. $a = fopen($foo, \'r+\');
  77. $a = fopen($foo, \'w+r\');
  78. ',
  79. '<?php
  80. $a = fopen($foo, \'r+bt\');
  81. $a = fopen($foo, \'btw+r\');
  82. $a = fopen($foo, \'r+tb\');
  83. $a = fopen($foo, \'tbw+r\');
  84. ',
  85. ['b_mode' => false],
  86. ];
  87. yield 'superfluous "b"' => [
  88. '<?php
  89. $a = fopen($foo, \'r+\');
  90. $a = fopen($foo, \'w+r\');
  91. ',
  92. '<?php
  93. $a = fopen($foo, \'r+b\');
  94. $a = fopen($foo, \'bw+r\');
  95. ',
  96. ['b_mode' => false],
  97. ];
  98. foreach (self::provideDoNotFixCodeSamples() as $name => $code) {
  99. yield $name.' with b_mode' => [$code];
  100. yield $name.' without b_mode' => [$code, null, ['b_mode' => false]];
  101. }
  102. }
  103. /**
  104. * @return iterable<string, string>
  105. */
  106. private static function provideDoNotFixCodeSamples(): iterable
  107. {
  108. yield 'not simple flags' => '<?php $a = fopen($foo, "t".$a);';
  109. yield 'wrong # of arguments' => '<?php
  110. $b = fopen("br+");
  111. $c = fopen($foo, "w+", 1, 2 , 3);
  112. ';
  113. yield '"flags" is too long (must be overridden)' => '<?php $d = fopen($foo, "r+w+a+x+c+etXY");';
  114. yield '"flags" is too short (must be overridden)' => '<?php $d = fopen($foo, "");';
  115. yield 'static method call' => '<?php $e = A::fopen($foo, "w+");';
  116. yield 'method call' => '<?php $f = $b->fopen($foo, "r+");';
  117. yield 'comments, PHPDoc and literal' => '<?php
  118. // fopen($foo, "rw");
  119. /* fopen($foo, "rw"); */
  120. echo("fopen($foo, \"rw\")");
  121. ';
  122. yield 'invalid flag values' => '<?php
  123. $a = fopen($foo, \'\');
  124. $a = fopen($foo, \'k\');
  125. $a = fopen($foo, \'kz\');
  126. $a = fopen($foo, \'k+\');
  127. $a = fopen($foo, \'+k\');
  128. $a = fopen($foo, \'xct++\');
  129. $a = fopen($foo, \'w+r+r+\');
  130. $a = fopen($foo, \'+btrw+\');
  131. $a = fopen($foo, \'b+rw\');
  132. $a = fopen($foo, \'bbrw+\');
  133. $a = fopen($foo, \'brw++\');
  134. $a = fopen($foo, \'++brw\');
  135. $a = fopen($foo, \'ybrw+\');
  136. $a = fopen($foo, \'rr\');
  137. $a = fopen($foo, \'ロ\');
  138. $a = fopen($foo, \'ロ+\');
  139. $a = fopen($foo, \'rロ\');
  140. $a = \fopen($foo, \'w+ロ\');
  141. ';
  142. yield 'second argument not string' => '<?php
  143. echo "abc"; // to pass the candidate check
  144. $a = fopen($foo, 1);
  145. $a = fopen($foo, $a);
  146. $a = fopen($foo, null);
  147. ';
  148. }
  149. }