ShortScalarCastFixerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (\PHP_VERSION_ID >= 80000) {
  58. static::markTestSkipped('PHP < 8.0 is required.');
  59. }
  60. $this->expectDeprecation('%AThe (real) cast is deprecated, use (float) instead');
  61. $this->doTest($expected, $input);
  62. }
  63. public function provideFixCases()
  64. {
  65. foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'binary' => 'string'] as $from => $to) {
  66. foreach ($this->createCasesFor($from, $to) as $case) {
  67. yield $case;
  68. }
  69. }
  70. }
  71. public function provideFixDeprecatedCases()
  72. {
  73. return $this->createCasesFor('real', 'float');
  74. }
  75. /**
  76. * @param string $expected
  77. *
  78. * @dataProvider provideNoFixCases
  79. */
  80. public function testNoFix($expected)
  81. {
  82. $this->doTest($expected);
  83. }
  84. public function provideNoFixCases()
  85. {
  86. $cases = [];
  87. $types = ['string', 'array', 'object'];
  88. if (\PHP_VERSION_ID < 80000) {
  89. $types[] = 'unset';
  90. }
  91. foreach ($types as $cast) {
  92. $cases[] = [sprintf('<?php $b=(%s) $d;', $cast)];
  93. $cases[] = [sprintf('<?php $b=( %s ) $d;', $cast)];
  94. $cases[] = [sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];
  95. $cases[] = [sprintf('<?php $b=(%s ) $d;', strtoupper($cast))];
  96. }
  97. return $cases;
  98. }
  99. private function createCasesFor($from, $to)
  100. {
  101. yield [
  102. sprintf('<?php echo ( %s )$a;', $to),
  103. sprintf('<?php echo ( %s )$a;', $from),
  104. ];
  105. yield [
  106. sprintf('<?php $b=(%s) $d;', $to),
  107. sprintf('<?php $b=(%s) $d;', $from),
  108. ];
  109. yield [
  110. sprintf('<?php $b= (%s)$d;', $to),
  111. sprintf('<?php $b= (%s)$d;', strtoupper($from)),
  112. ];
  113. yield [
  114. sprintf('<?php $b=( %s) $d;', $to),
  115. sprintf('<?php $b=( %s) $d;', ucfirst($from)),
  116. ];
  117. yield [
  118. sprintf('<?php $b=(%s ) $d;', $to),
  119. sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
  120. ];
  121. }
  122. }