ShortScalarCastFixerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. final class ShortScalarCastFixerTest 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. /**
  29. * @dataProvider provideFix74DeprecatedCases
  30. *
  31. * @group legacy
  32. *
  33. * @requires PHP <8.0
  34. */
  35. public function testFix74Deprecated(string $expected, ?string $input = null): void
  36. {
  37. $this->expectDeprecation('%AThe (real) cast is deprecated, use (float) instead');
  38. $this->doTest($expected, $input);
  39. }
  40. public static function provideFixCases(): iterable
  41. {
  42. foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'binary' => 'string'] as $from => $to) {
  43. foreach (self::createCasesFor($from, $to) as $case) {
  44. yield $case;
  45. }
  46. }
  47. }
  48. public static function provideFix74DeprecatedCases(): iterable
  49. {
  50. return self::createCasesFor('real', 'float');
  51. }
  52. /**
  53. * @dataProvider provideNoFixCases
  54. */
  55. public function testNoFix(string $expected): void
  56. {
  57. $this->doTest($expected);
  58. }
  59. public static function provideNoFixCases(): iterable
  60. {
  61. $types = ['string', 'array', 'object'];
  62. if (\PHP_VERSION_ID < 8_00_00) {
  63. $types[] = 'unset';
  64. }
  65. foreach ($types as $cast) {
  66. yield [sprintf('<?php $b=(%s) $d;', $cast)];
  67. yield [sprintf('<?php $b=( %s ) $d;', $cast)];
  68. yield [sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];
  69. yield [sprintf('<?php $b=(%s ) $d;', strtoupper($cast))];
  70. }
  71. }
  72. /**
  73. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  74. */
  75. private static function createCasesFor(string $from, string $to): iterable
  76. {
  77. yield [
  78. sprintf('<?php echo ( %s )$a;', $to),
  79. sprintf('<?php echo ( %s )$a;', $from),
  80. ];
  81. yield [
  82. sprintf('<?php $b=(%s) $d;', $to),
  83. sprintf('<?php $b=(%s) $d;', $from),
  84. ];
  85. yield [
  86. sprintf('<?php $b= (%s)$d;', $to),
  87. sprintf('<?php $b= (%s)$d;', strtoupper($from)),
  88. ];
  89. yield [
  90. sprintf('<?php $b=( %s) $d;', $to),
  91. sprintf('<?php $b=( %s) $d;', ucfirst($from)),
  92. ];
  93. yield [
  94. sprintf('<?php $b=(%s ) $d;', $to),
  95. sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
  96. ];
  97. }
  98. }