PhpdocInlineTagFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Phpdoc;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @internal
  15. *
  16. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocInlineTagFixer
  17. */
  18. final class PhpdocInlineTagFixerTest extends AbstractFixerTestCase
  19. {
  20. /**
  21. * @param string $expected
  22. * @param null|string $input
  23. *
  24. * @dataProvider provideTestFixInlineDocCases
  25. */
  26. public function testFixInlineDoc($expected, $input = null)
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public function provideTestFixInlineDocCases()
  31. {
  32. $cases = [
  33. [
  34. '<?php
  35. /**
  36. * {link} { LINK }
  37. * { test }
  38. * {@inheritdoc rire éclatant des écoliers qui décontenança®¶ñ¿}
  39. * test other comment
  40. * {@inheritdoc test} a
  41. * {@inheritdoc test} b
  42. * {@inheritdoc test} c
  43. * {@inheritdoc foo bar.} d
  44. * {@inheritdoc foo bar.} e
  45. * {@inheritdoc test} f
  46. * end comment {@inheritdoc here we are done} @spacepossum {1}
  47. */
  48. ',
  49. '<?php
  50. /**
  51. * {link} { LINK }
  52. * { test }
  53. * {@inheritDoc rire éclatant des écoliers qui décontenança®¶ñ¿ }
  54. * test other comment
  55. * @{inheritdoc test} a
  56. * {{@inheritdoc test}} b
  57. * {@ inheritdoc test} c
  58. * { @inheritdoc foo bar. } d
  59. * {@ inheritdoc foo bar. } e
  60. * @{{inheritdoc test}} f
  61. * end comment {@inheritdoc here we are done} @spacepossum {1}
  62. */
  63. ',
  64. ],
  65. ];
  66. foreach (['example', 'id', 'internal', 'inheritdoc', 'link', 'source', 'toc', 'tutorial'] as $tag) {
  67. $cases[] = [
  68. sprintf("<?php\n /**\n * {@%s}a\n */\n", $tag),
  69. sprintf("<?php\n /**\n * @{%s}a\n */\n", $tag),
  70. ];
  71. $cases[] = [
  72. sprintf("<?php\n /**\n * {@%s} b\n */\n", $tag),
  73. sprintf("<?php\n /**\n * {{@%s}} b\n */\n", $tag),
  74. ];
  75. $cases[] = [
  76. sprintf("<?php\n /**\n * c {@%s}\n */\n", $tag),
  77. sprintf("<?php\n /**\n * c @{{%s}}\n */\n", $tag),
  78. ];
  79. $cases[] = [
  80. sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
  81. sprintf("<?php\n /**\n * c @{{%s test}}\n */\n", $tag),
  82. ];
  83. // test unbalanced { tags
  84. $cases[] = [
  85. sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
  86. sprintf("<?php\n /**\n * c {@%s test}}\n */\n", $tag),
  87. ];
  88. $cases[] = [
  89. sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
  90. sprintf("<?php\n /**\n * c {{@%s test}\n */\n", $tag),
  91. ];
  92. $cases[] = [
  93. sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
  94. sprintf("<?php\n /**\n * c {@%s test}}\n */\n", $tag),
  95. ];
  96. $cases[] = [
  97. sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
  98. sprintf("<?php\n /**\n * c @{{%s test}}}\n */\n", $tag),
  99. ];
  100. }
  101. // don't touch custom tags
  102. $tag = 'foo';
  103. $cases[] = [
  104. sprintf("<?php\n /**\n * @{%s}a\n */\n", $tag),
  105. ];
  106. $cases[] = [
  107. sprintf("<?php\n /**\n * {{@%s}} b\n */\n", $tag),
  108. ];
  109. $cases[] = [
  110. sprintf("<?php\n /**\n * c @{{%s}}\n */\n", $tag),
  111. ];
  112. // don't auto inline tags with the exception of inheritdoc
  113. foreach (['example', 'id', 'internal', 'foo', 'link', 'source', 'toc', 'tutorial'] as $tag) {
  114. $cases[] = [
  115. sprintf("<?php\n /**\n * @%s\n */\n", $tag),
  116. ];
  117. }
  118. // don't touch well formatted tags
  119. foreach (['example', 'id', 'internal', 'inheritdoc', 'link', 'source', 'toc', 'tutorial'] as $tag) {
  120. $cases[] = [
  121. sprintf("<?php\n /**\n * {@%s}\n */\n", $tag),
  122. ];
  123. }
  124. // common typos
  125. $cases[] = [
  126. '<?php
  127. /**
  128. * Typo {@inheritdoc} {@example} {@id} {@source} {@tutorial} {links}
  129. * inheritdocs
  130. */
  131. ',
  132. '<?php
  133. /**
  134. * Typo {@inheritdocs} {@exampleS} { @ids} { @sources } {{{ @tutorials }} {links}
  135. * inheritdocs
  136. */
  137. ',
  138. ];
  139. // invalid syntax
  140. $cases[] = [
  141. '<?php
  142. /**
  143. * {@link https://www.ietf.org/rfc/rfc1035.txt)
  144. */
  145. $someVar = "hello";',
  146. ];
  147. return $cases;
  148. }
  149. /**
  150. * @dataProvider provideTestFixInheritDocCases
  151. */
  152. public function testFixInheritDoc($expected, $input = null)
  153. {
  154. $this->doTest($expected, $input);
  155. }
  156. public function provideTestFixInheritDocCases()
  157. {
  158. return [
  159. [
  160. '<?php
  161. /**
  162. * {@inheritdoc} should this be inside the tag?
  163. * {@inheritdoc}
  164. * {@inheritdoc}
  165. * {@inheritdoc}
  166. * inheritdoc
  167. */
  168. ',
  169. // missing { } test for inheritdoc
  170. '<?php
  171. /**
  172. * @inheritdoc should this be inside the tag?
  173. * @inheritdoc
  174. * @inheritdocs
  175. * {@inheritdocs}
  176. * inheritdoc
  177. */
  178. ',
  179. ],
  180. ];
  181. }
  182. }