LinebreakAfterOpeningTagFixerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\PhpTag;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Ceeram <ceeram@cakephp.org>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\PhpTag\LinebreakAfterOpeningTagFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\LinebreakAfterOpeningTagFixer>
  24. */
  25. final class LinebreakAfterOpeningTagFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null): void
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return iterable<int|string, array{0: string, 1?: string}>
  36. */
  37. public static function provideFixCases(): iterable
  38. {
  39. yield [
  40. '<?php
  41. $a = function(){
  42. echo 1;
  43. };',
  44. '<?php $a = function(){
  45. echo 1;
  46. };',
  47. ];
  48. yield [
  49. '<?php $foo = true; ?>',
  50. ];
  51. yield [
  52. '<?php $foo = true; ?>
  53. ',
  54. ];
  55. yield [
  56. '<?php
  57. $foo = true;
  58. ?>',
  59. ];
  60. yield [
  61. '<?php
  62. $foo = true;
  63. $bar = false;
  64. ?>',
  65. '<?php $foo = true;
  66. $bar = false;
  67. ?>',
  68. ];
  69. yield [
  70. '<?php $foo = true; ?>
  71. Html here
  72. <?php $bar = false; ?>',
  73. ];
  74. yield [
  75. '<?= $bar;
  76. $foo = $bar;
  77. ?>',
  78. ];
  79. yield [
  80. str_replace("\n", "\r\n", '<?php
  81. // linebreak already present in file with Windows line endings
  82. '),
  83. ];
  84. yield 'file with shebang' => [
  85. <<<'EOD'
  86. #!x
  87. <?php
  88. echo 1;
  89. echo 2;
  90. EOD,
  91. <<<'EOD'
  92. #!x
  93. <?php echo 1;
  94. echo 2;
  95. EOD,
  96. ];
  97. }
  98. /**
  99. * @dataProvider provideWithWhitespacesConfigCases
  100. */
  101. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  102. {
  103. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  104. $this->doTest($expected, $input);
  105. }
  106. /**
  107. * @return iterable<array{string, string}>
  108. */
  109. public static function provideWithWhitespacesConfigCases(): iterable
  110. {
  111. yield [
  112. "<?php\r\n\$foo = true;\n",
  113. "<?php \$foo = true;\n",
  114. ];
  115. }
  116. }