DeclareStrictTypesFixerTest.php 3.3 KB

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