DeclareStrictTypesFixerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer>
  21. */
  22. final class DeclareStrictTypesFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null): void
  28. {
  29. $this->doTest($expected, $input);
  30. }
  31. /**
  32. * @return iterable<int|string, array{0: string, 1?: string}>
  33. */
  34. public static function provideFixCases(): iterable
  35. {
  36. yield [
  37. '<?php
  38. declare(ticks=1);
  39. //
  40. declare(strict_types=1);
  41. namespace A\B\C;
  42. class A {
  43. }',
  44. ];
  45. yield [
  46. '<?php declare/* A b C*/(strict_types=1);',
  47. ];
  48. yield 'monolithic file with closing tag' => [
  49. '<?php /**/ /**/ deClarE (strict_types=1) ?>',
  50. '<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>',
  51. ];
  52. yield 'monolithic file with closing tag and extra new line' => [
  53. '<?php /**/ /**/ deClarE (strict_types=1) ?>'."\n",
  54. '<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>'."\n",
  55. ];
  56. yield 'monolithic file with closing tag and extra content' => [
  57. '<?php /**/ /**/ deClarE (STRICT_TYPES=1) ?>Test',
  58. ];
  59. yield [
  60. '<?php DECLARE ( strict_types=1 ) ;',
  61. ];
  62. yield [
  63. '<?php
  64. /**/
  65. declare(strict_types=1);',
  66. ];
  67. yield [
  68. '<?php declare(strict_types=1);
  69. phpinfo();',
  70. '<?php
  71. phpinfo();',
  72. ];
  73. yield [
  74. '<?php declare(strict_types=1);
  75. /**
  76. * Foo
  77. */
  78. phpinfo();',
  79. '<?php
  80. /**
  81. * Foo
  82. */
  83. phpinfo();',
  84. ];
  85. yield [
  86. '<?php declare(strict_types=1);
  87. // comment after empty line',
  88. '<?php
  89. // comment after empty line',
  90. ];
  91. yield [
  92. '<?php declare(strict_types=1);
  93. // comment without empty line before',
  94. '<?php
  95. // comment without empty line before',
  96. ];
  97. yield [
  98. '<?php declare(strict_types=1);
  99. phpinfo();',
  100. '<?php phpinfo();',
  101. ];
  102. yield [
  103. '<?php declare(strict_types=1);
  104. $a = 456;
  105. ',
  106. '<?php
  107. $a = 456;
  108. ',
  109. ];
  110. yield [
  111. '<?php declare(strict_types=1);
  112. /**/',
  113. '<?php /**/',
  114. ];
  115. yield [
  116. '<?php declare(strict_types=1);',
  117. '<?php declare(strict_types=0);',
  118. ];
  119. yield [' <?php echo 123;']; // first statement must be an open tag
  120. yield ['<?= 123;']; // first token open with echo is not fixed
  121. yield 'empty file /wo open tag' => [
  122. '',
  123. ];
  124. yield 'empty file /w open tag' => [
  125. '<?php declare(strict_types=1);',
  126. '<?php',
  127. ];
  128. yield 'non-empty file /wo open tag' => [
  129. 'x',
  130. ];
  131. yield 'non-empty file /w open tag' => [
  132. 'x<?php',
  133. ];
  134. yield 'file with shebang /w open tag' => [
  135. <<<'EOD'
  136. #!x
  137. <?php declare(strict_types=1);
  138. EOD,
  139. <<<'EOD'
  140. #!x
  141. <?php
  142. EOD,
  143. ];
  144. yield 'file with shebang /wo open tag' => [
  145. <<<'EOD'
  146. #!x
  147. y
  148. EOD,
  149. ];
  150. yield 'file with shebang not followed by open tag' => [
  151. <<<'EOD'
  152. #!x
  153. #!not_a_shebang
  154. <?php
  155. EOD,
  156. ];
  157. }
  158. /**
  159. * @dataProvider provideWithWhitespacesConfigCases
  160. */
  161. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  162. {
  163. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  164. $this->doTest($expected, $input);
  165. }
  166. /**
  167. * @return iterable<array{string, string}>
  168. */
  169. public static function provideWithWhitespacesConfigCases(): iterable
  170. {
  171. yield [
  172. "<?php declare(strict_types=1);\r\nphpinfo();",
  173. "<?php\r\n\tphpinfo();",
  174. ];
  175. yield [
  176. "<?php declare(strict_types=1);\r\nphpinfo();",
  177. "<?php\nphpinfo();",
  178. ];
  179. }
  180. }