ShortScalarCastFixerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\CastNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  14. /**
  15. * @author SpacePossum
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer
  20. */
  21. final class ShortScalarCastFixerTest extends AbstractFixerTestCase
  22. {
  23. use ExpectDeprecationTrait;
  24. /**
  25. * @param string $expected
  26. * @param null|string $input
  27. *
  28. * @dataProvider provideFixCases
  29. * @dataProvider provideFixDeprecatedCases
  30. * @requires PHP < 7.4
  31. */
  32. public function testFix($expected, $input = null)
  33. {
  34. $this->doTest($expected, $input);
  35. }
  36. /**
  37. * @param string $expected
  38. * @param null|string $input
  39. *
  40. * @dataProvider provideFixCases
  41. * @requires PHP 7.4
  42. */
  43. public function testFix74($expected, $input = null)
  44. {
  45. $this->doTest($expected, $input);
  46. }
  47. /**
  48. * @param string $expected
  49. * @param null|string $input
  50. *
  51. * @dataProvider provideFixDeprecatedCases
  52. * @requires PHP 7.4
  53. * @group legacy
  54. */
  55. public function testFix74Deprecated($expected, $input = null)
  56. {
  57. $this->expectDeprecation('The (real) cast is deprecated, use (float) instead');
  58. $this->doTest($expected, $input);
  59. }
  60. public function provideFixCases()
  61. {
  62. foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'binary' => 'string'] as $from => $to) {
  63. foreach ($this->createCasesFor($from, $to) as $case) {
  64. yield $case;
  65. }
  66. }
  67. }
  68. public function provideFixDeprecatedCases()
  69. {
  70. return $this->createCasesFor('real', 'float');
  71. }
  72. /**
  73. * @param string $expected
  74. *
  75. * @dataProvider provideNoFixCases
  76. */
  77. public function testNoFix($expected)
  78. {
  79. $this->doTest($expected);
  80. }
  81. public function provideNoFixCases()
  82. {
  83. $cases = [];
  84. foreach (['string', 'array', 'object', 'unset'] as $cast) {
  85. $cases[] = [sprintf('<?php $b=(%s) $d;', $cast)];
  86. $cases[] = [sprintf('<?php $b=( %s ) $d;', $cast)];
  87. $cases[] = [sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];
  88. $cases[] = [sprintf('<?php $b=(%s ) $d;', strtoupper($cast))];
  89. }
  90. return $cases;
  91. }
  92. private function createCasesFor($from, $to)
  93. {
  94. yield [
  95. sprintf('<?php echo ( %s )$a;', $to),
  96. sprintf('<?php echo ( %s )$a;', $from),
  97. ];
  98. yield [
  99. sprintf('<?php $b=(%s) $d;', $to),
  100. sprintf('<?php $b=(%s) $d;', $from),
  101. ];
  102. yield [
  103. sprintf('<?php $b= (%s)$d;', $to),
  104. sprintf('<?php $b= (%s)$d;', strtoupper($from)),
  105. ];
  106. yield [
  107. sprintf('<?php $b=( %s) $d;', $to),
  108. sprintf('<?php $b=( %s) $d;', ucfirst($from)),
  109. ];
  110. yield [
  111. sprintf('<?php $b=(%s ) $d;', $to),
  112. sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
  113. ];
  114. }
  115. }