HeaderCommentFixerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests\Fixer\Contrib;
  11. use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
  12. use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
  13. class HeaderCommentFixerTest extends AbstractFixerTestBase
  14. {
  15. protected static $savedHeader;
  16. protected static $testHeader = <<<'EOH'
  17. This file is part of the PHP CS utility.
  18. (c) Fabien Potencier <fabien@symfony.com>
  19. This source file is subject to the MIT license that is bundled
  20. with this source code in the file LICENSE.
  21. EOH;
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. self::$savedHeader = HeaderCommentFixer::getHeader();
  26. HeaderCommentFixer::setHeader(self::$testHeader);
  27. }
  28. protected function tearDown()
  29. {
  30. HeaderCommentFixer::setHeader(self::$savedHeader);
  31. parent::tearDown();
  32. }
  33. public function testFixWithPreviousHeader()
  34. {
  35. $expected = <<<'EOH'
  36. <?php
  37. /*
  38. * This file is part of the PHP CS utility.
  39. *
  40. * (c) Fabien Potencier <fabien@symfony.com>
  41. *
  42. * This source file is subject to the MIT license that is bundled
  43. * with this source code in the file LICENSE.
  44. */
  45. phpinfo();
  46. EOH;
  47. $input = <<<'EOH'
  48. <?php
  49. /*
  50. * Previous Header
  51. */
  52. phpinfo();
  53. EOH;
  54. $this->makeTest($expected, $input);
  55. }
  56. public function testFixWithoutPreviousHeader()
  57. {
  58. $expected = <<<'EOH'
  59. <?php
  60. /*
  61. * This file is part of the PHP CS utility.
  62. *
  63. * (c) Fabien Potencier <fabien@symfony.com>
  64. *
  65. * This source file is subject to the MIT license that is bundled
  66. * with this source code in the file LICENSE.
  67. */
  68. phpinfo();
  69. EOH;
  70. $input = <<<'EOH'
  71. <?php
  72. phpinfo();
  73. EOH;
  74. $this->makeTest($expected, $input);
  75. }
  76. public function testFixWithClassDocblock()
  77. {
  78. $expected = <<<'EOH'
  79. <?php
  80. /*
  81. * This file is part of the PHP CS utility.
  82. *
  83. * (c) Fabien Potencier <fabien@symfony.com>
  84. *
  85. * This source file is subject to the MIT license that is bundled
  86. * with this source code in the file LICENSE.
  87. */
  88. /**
  89. * @author Antonio J. García Lagar <aj@garcialagar.es>
  90. */
  91. class Foo
  92. {
  93. }
  94. EOH;
  95. $input = <<<'EOH'
  96. <?php
  97. /**
  98. * @author Antonio J. García Lagar <aj@garcialagar.es>
  99. */
  100. class Foo
  101. {
  102. }
  103. EOH;
  104. $this->makeTest($expected, $input);
  105. }
  106. public function testFixRemovePreviousHeader()
  107. {
  108. HeaderCommentFixer::setHeader('');
  109. $expected = <<<'EOH'
  110. <?php
  111. phpinfo();
  112. EOH;
  113. $input = <<<'EOH'
  114. <?php
  115. /*
  116. * This file is part of the PHP CS utility.
  117. *
  118. * (c) Fabien Potencier <fabien@symfony.com>
  119. *
  120. * This source file is subject to the MIT license that is bundled
  121. * with this source code in the file LICENSE.
  122. */
  123. phpinfo();
  124. EOH;
  125. $this->makeTest($expected, $input);
  126. }
  127. public function testFixDoNotTouchFilesWithSeveralOpenTags()
  128. {
  129. $input = "<?php\nphpinfo();\n?>\n<?";
  130. $this->makeTest($input);
  131. }
  132. public function testFixDoNotTouchFilesNotStartingWithOpenTag()
  133. {
  134. $input = " <?php\nphpinfo();\n";
  135. $this->makeTest($input);
  136. }
  137. public function testFixDoNotTouchFilesWithInlineHtml()
  138. {
  139. $input = "<?php\nphpinfo();\n?><hr/>";
  140. $this->makeTest($input);
  141. }
  142. public function testFixAddHeaderToEmptyFile()
  143. {
  144. $expected = <<<'EOH'
  145. <?php
  146. /*
  147. * This file is part of the PHP CS utility.
  148. *
  149. * (c) Fabien Potencier <fabien@symfony.com>
  150. *
  151. * This source file is subject to the MIT license that is bundled
  152. * with this source code in the file LICENSE.
  153. */
  154. EOH;
  155. $input = "<?php\n";
  156. $this->makeTest($expected, $input);
  157. }
  158. /**
  159. * @expectedException \Symfony\CS\ConfigurationException\InvalidFixerConfigurationException
  160. * @expectedExceptionMessage [header_comment] Header configuration is invalid. Expected "string", got "stdClass".
  161. */
  162. public function testInvalidConfig()
  163. {
  164. HeaderCommentFixer::setHeader(new \stdClass());
  165. }
  166. }