NoUselessSprintfFixerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Fixer\FunctionNotation\NoUselessSprintfFixer
  18. */
  19. final class NoUselessSprintfFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @dataProvider provideFixCases
  23. */
  24. public function testFix(string $expected, ?string $input = null): void
  25. {
  26. $this->doTest($expected, $input);
  27. }
  28. public static function provideFixCases(): iterable
  29. {
  30. yield 'simple' => [
  31. '<?php echo "bar";',
  32. '<?php echo sprintf("bar");',
  33. ];
  34. yield 'simple II' => [
  35. "<?php echo 'bar' ?>",
  36. "<?php echo sprintf('bar') ?>",
  37. ];
  38. yield 'simple III' => [
  39. '<?php echo $bar;',
  40. '<?php echo sprintf($bar);',
  41. ];
  42. yield 'simple IV' => [
  43. '<?php echo 1;',
  44. '<?php echo sprintf(1);',
  45. ];
  46. yield 'minimal' => [
  47. '<?php $foo;',
  48. '<?php sprintf($foo);',
  49. ];
  50. yield 'case insensitive' => [
  51. '<?php echo "bar";',
  52. '<?php echo SPRINTF("bar");',
  53. ];
  54. yield 'nested' => [
  55. '<?php echo /* test */"bar";',
  56. '<?php echo sprintf(sprintf(sprintf(/* test */sprintf(sprintf(sprintf("bar"))))));',
  57. ];
  58. yield [
  59. '<?php namespace Foo {
  60. function sprintf($foo) {
  61. echo $foo;
  62. }
  63. }',
  64. ];
  65. yield [
  66. '<?php namespace Foo;
  67. function sprintf($foo) {
  68. echo $foo;
  69. }
  70. ',
  71. ];
  72. yield [
  73. '<?php
  74. echo \Foo\sprintf("abc");
  75. echo $bar->sprintf($foo);
  76. echo Bar::sprintf($foo);
  77. echo sprint(...$foo);
  78. echo sprint("%d", 1);
  79. echo sprint("%d%d%d", 1, 2, 3);
  80. echo sprint();
  81. ',
  82. ];
  83. yield [
  84. '<?php echo sprint[2]("foo");',
  85. ];
  86. yield 'trailing comma' => [
  87. '<?php echo "bar";',
  88. '<?php echo sprintf("bar",);',
  89. ];
  90. }
  91. /**
  92. * @dataProvider provideFixPre80Cases
  93. *
  94. * @requires PHP <8.0
  95. */
  96. public function testFixPre80(string $expected, string $input = null): void
  97. {
  98. $this->doTest($expected, $input);
  99. }
  100. public static function provideFixPre80Cases(): iterable
  101. {
  102. yield [
  103. '<?php echo "bar";',
  104. '<?php echo \ sprintf("bar");',
  105. ];
  106. yield [
  107. '<?php echo A /* 1 */ \ B \ sprintf("bar");',
  108. ];
  109. }
  110. }