DeclareStrictTypesFixerTest.php 3.6 KB

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