DeclareStrictTypesFixerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Strict;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer
  19. */
  20. final class DeclareStrictTypesFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @dataProvider provideFixCases
  24. */
  25. public function testFix(string $expected, ?string $input = null): void
  26. {
  27. $this->doTest($expected, $input);
  28. }
  29. public static function provideFixCases(): iterable
  30. {
  31. yield [
  32. '<?php
  33. declare(ticks=1);
  34. //
  35. declare(strict_types=1);
  36. namespace A\B\C;
  37. class A {
  38. }',
  39. ];
  40. yield [
  41. '<?php declare/* A b C*/(strict_types=1);',
  42. ];
  43. yield [
  44. '<?php /**/ /**/ deClarE (strict_types=1) ?>Test',
  45. '<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>Test',
  46. ];
  47. yield [
  48. '<?php DECLARE ( strict_types=1 ) ;',
  49. ];
  50. yield [
  51. '<?php
  52. /**/
  53. declare(strict_types=1);',
  54. ];
  55. yield [
  56. '<?php declare(strict_types=1);
  57. phpinfo();',
  58. '<?php
  59. phpinfo();',
  60. ];
  61. yield [
  62. '<?php declare(strict_types=1);
  63. /**
  64. * Foo
  65. */
  66. phpinfo();',
  67. '<?php
  68. /**
  69. * Foo
  70. */
  71. phpinfo();',
  72. ];
  73. yield [
  74. '<?php declare(strict_types=1);
  75. // comment after empty line',
  76. '<?php
  77. // comment after empty line',
  78. ];
  79. yield [
  80. '<?php declare(strict_types=1);
  81. // comment without empty line before',
  82. '<?php
  83. // comment without empty line before',
  84. ];
  85. yield [
  86. '<?php declare(strict_types=1);
  87. phpinfo();',
  88. '<?php phpinfo();',
  89. ];
  90. yield [
  91. '<?php declare(strict_types=1);
  92. $a = 456;
  93. ',
  94. '<?php
  95. $a = 456;
  96. ',
  97. ];
  98. yield [
  99. '<?php declare(strict_types=1);
  100. /**/',
  101. '<?php /**/',
  102. ];
  103. yield [
  104. '<?php declare(strict_types=1);',
  105. '<?php declare(strict_types=0);',
  106. ];
  107. }
  108. /**
  109. * @dataProvider provideDoNotFixCases
  110. */
  111. public function testDoNotFix(string $input): void
  112. {
  113. $this->doTest($input);
  114. }
  115. public static function provideDoNotFixCases(): iterable
  116. {
  117. yield [' <?php echo 123;']; // first statement must be an open tag
  118. yield ['<?= 123;']; // first token open with echo is not fixed
  119. }
  120. /**
  121. * @dataProvider provideMessyWhitespacesCases
  122. */
  123. public function testMessyWhitespaces(string $expected, ?string $input = null): void
  124. {
  125. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  126. $this->doTest($expected, $input);
  127. }
  128. public static function provideMessyWhitespacesCases(): iterable
  129. {
  130. yield [
  131. "<?php declare(strict_types=1);\r\nphpinfo();",
  132. "<?php\r\n\tphpinfo();",
  133. ];
  134. yield [
  135. "<?php declare(strict_types=1);\r\nphpinfo();",
  136. "<?php\nphpinfo();",
  137. ];
  138. }
  139. }