LowercaseCastFixerTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\LowercaseCastFixer
  18. */
  19. final class LowercaseCastFixerTest 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. $types = ['boolean', 'bool', 'integer', 'int', 'double', 'float', 'float', 'string', 'array', 'object', 'binary'];
  43. if (\PHP_VERSION_ID < 8_00_00) {
  44. $types[] = 'unset';
  45. }
  46. foreach ($types as $from) {
  47. foreach (self::createCasesFor($from) as $case) {
  48. yield $case;
  49. }
  50. }
  51. }
  52. public static function provideFix74DeprecatedCases(): iterable
  53. {
  54. return self::createCasesFor('real');
  55. }
  56. /**
  57. * @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
  58. */
  59. private static function createCasesFor(string $type): iterable
  60. {
  61. yield [
  62. sprintf('<?php $b= (%s)$d;', $type),
  63. sprintf('<?php $b= (%s)$d;', strtoupper($type)),
  64. ];
  65. yield [
  66. sprintf('<?php $b=( %s) $d;', $type),
  67. sprintf('<?php $b=( %s) $d;', ucfirst($type)),
  68. ];
  69. yield [
  70. sprintf('<?php $b=(%s ) $d;', $type),
  71. sprintf('<?php $b=(%s ) $d;', strtoupper($type)),
  72. ];
  73. yield [
  74. sprintf('<?php $b=( %s ) $d;', $type),
  75. sprintf('<?php $b=( %s ) $d;', ucfirst($type)),
  76. ];
  77. }
  78. }