ShortScalarCastFixerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  14. * @author SpacePossum
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer
  19. */
  20. final class ShortScalarCastFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. $cases = [];
  35. foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'real' => 'float'] as $from => $to) {
  36. $cases[] =
  37. [
  38. sprintf('<?php echo ( %s )$a;', $to),
  39. sprintf('<?php echo ( %s )$a;', $from),
  40. ];
  41. $cases[] =
  42. [
  43. sprintf('<?php $b=(%s) $d;', $to),
  44. sprintf('<?php $b=(%s) $d;', $from),
  45. ];
  46. $cases[] =
  47. [
  48. sprintf('<?php $b= (%s)$d;', $to),
  49. sprintf('<?php $b= (%s)$d;', strtoupper($from)),
  50. ];
  51. $cases[] =
  52. [
  53. sprintf('<?php $b=( %s) $d;', $to),
  54. sprintf('<?php $b=( %s) $d;', ucfirst($from)),
  55. ];
  56. $cases[] =
  57. [
  58. sprintf('<?php $b=(%s ) $d;', $to),
  59. sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
  60. ];
  61. }
  62. return $cases;
  63. }
  64. /**
  65. * @param string $expected
  66. *
  67. * @dataProvider provideNoFixCases
  68. */
  69. public function testNoFix($expected)
  70. {
  71. $this->doTest($expected);
  72. }
  73. public function provideNoFixCases()
  74. {
  75. $cases = [];
  76. foreach (['string', 'array', 'object', 'unset', 'binary'] as $cast) {
  77. $cases[] = [sprintf('<?php $b=(%s) $d;', $cast)];
  78. $cases[] = [sprintf('<?php $b=( %s ) $d;', $cast)];
  79. $cases[] = [sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];
  80. $cases[] = [sprintf('<?php $b=(%s ) $d;', strtoupper($cast))];
  81. }
  82. return $cases;
  83. }
  84. }