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. $cases = [];
  62. $types = ['string', 'array', 'object'];
  63. if (\PHP_VERSION_ID < 8_00_00) {
  64. $types[] = 'unset';
  65. }
  66. foreach ($types as $cast) {
  67. $cases[] = [sprintf('<?php $b=(%s) $d;', $cast)];
  68. $cases[] = [sprintf('<?php $b=( %s ) $d;', $cast)];
  69. $cases[] = [sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];
  70. $cases[] = [sprintf('<?php $b=(%s ) $d;', strtoupper($cast))];
  71. }
  72. return $cases;
  73. }
  74. /**
  75. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  76. */
  77. private static function createCasesFor(string $from, string $to): iterable
  78. {
  79. yield [
  80. sprintf('<?php echo ( %s )$a;', $to),
  81. sprintf('<?php echo ( %s )$a;', $from),
  82. ];
  83. yield [
  84. sprintf('<?php $b=(%s) $d;', $to),
  85. sprintf('<?php $b=(%s) $d;', $from),
  86. ];
  87. yield [
  88. sprintf('<?php $b= (%s)$d;', $to),
  89. sprintf('<?php $b= (%s)$d;', strtoupper($from)),
  90. ];
  91. yield [
  92. sprintf('<?php $b=( %s) $d;', $to),
  93. sprintf('<?php $b=( %s) $d;', ucfirst($from)),
  94. ];
  95. yield [
  96. sprintf('<?php $b=(%s ) $d;', $to),
  97. sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
  98. ];
  99. }
  100. }