ShortScalarCastFixerTest.php 3.2 KB

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