ShortScalarCastFixerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\CastNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer>
  20. */
  21. final class ShortScalarCastFixerTest 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. /**
  31. * @return iterable<array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'binary' => 'string'] as $from => $to) {
  36. yield from self::createCasesFor($from, $to);
  37. }
  38. $types = ['string', 'array', 'object'];
  39. foreach ($types as $cast) {
  40. yield [\sprintf('<?php $b=(%s) $d;', $cast)];
  41. yield [\sprintf('<?php $b=( %s ) $d;', $cast)];
  42. yield [\sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];
  43. yield [\sprintf('<?php $b=(%s ) $d;', strtoupper($cast))];
  44. }
  45. }
  46. /**
  47. * @dataProvider provideFixPre80Cases
  48. *
  49. * @requires PHP <8.0
  50. */
  51. public function testFixPre80(string $expected, ?string $input = null): void
  52. {
  53. $this->doTest($expected, $input);
  54. }
  55. /**
  56. * @return iterable<array{string}>
  57. */
  58. public static function provideFixPre80Cases(): iterable
  59. {
  60. yield ['<?php $b=(unset) $d;'];
  61. yield ['<?php $b=( unset ) $d;'];
  62. yield ['<?php $b=(Unset ) $d;'];
  63. yield ['<?php $b=(UNSET ) $d;'];
  64. }
  65. /**
  66. * @dataProvider provideFix74DeprecatedCases
  67. *
  68. * @group legacy
  69. *
  70. * @requires PHP <8.0
  71. */
  72. public function testFix74Deprecated(string $expected, ?string $input = null): void
  73. {
  74. $this->expectDeprecation('The (real) cast is deprecated, use (float) instead');
  75. $this->doTest($expected, $input);
  76. }
  77. /**
  78. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  79. */
  80. public static function provideFix74DeprecatedCases(): iterable
  81. {
  82. yield from self::createCasesFor('real', 'float');
  83. }
  84. /**
  85. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  86. */
  87. private static function createCasesFor(string $from, string $to): iterable
  88. {
  89. yield [
  90. \sprintf('<?php echo ( %s )$a;', $to),
  91. \sprintf('<?php echo ( %s )$a;', $from),
  92. ];
  93. yield [
  94. \sprintf('<?php $b=(%s) $d;', $to),
  95. \sprintf('<?php $b=(%s) $d;', $from),
  96. ];
  97. yield [
  98. \sprintf('<?php $b= (%s)$d;', $to),
  99. \sprintf('<?php $b= (%s)$d;', strtoupper($from)),
  100. ];
  101. yield [
  102. \sprintf('<?php $b=( %s) $d;', $to),
  103. \sprintf('<?php $b=( %s) $d;', ucfirst($from)),
  104. ];
  105. yield [
  106. \sprintf('<?php $b=(%s ) $d;', $to),
  107. \sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
  108. ];
  109. }
  110. }