PhpdocTypesFixerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * @author Graham Campbell <graham@alt-three.com>
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\AbstractPhpdocTypesFixer
  20. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer
  21. */
  22. final class PhpdocTypesFixerTest extends AbstractFixerTestCase
  23. {
  24. public function testWindowsLinebreaks()
  25. {
  26. $this->doTest(
  27. "<?php /**\r\n * @param string|string[] \$bar\r\n *\r\n * @return int[]\r\n */\r\n",
  28. "<?php /**\r\n * @param STRING|String[] \$bar\r\n *\r\n * @return inT[]\r\n */\r\n"
  29. );
  30. }
  31. public function testConversion()
  32. {
  33. $expected = <<<'EOF'
  34. <?php
  35. /**
  36. * @param boolean|array|Foo $bar
  37. *
  38. * @return int|float
  39. */
  40. EOF;
  41. $input = <<<'EOF'
  42. <?php
  43. /**
  44. * @param Boolean|Array|Foo $bar
  45. *
  46. * @return inT|Float
  47. */
  48. EOF;
  49. $this->doTest($expected, $input);
  50. }
  51. public function testArrayStuff()
  52. {
  53. $expected = <<<'EOF'
  54. <?php
  55. /**
  56. * @param string|string[] $bar
  57. *
  58. * @return int[]
  59. */
  60. EOF;
  61. $input = <<<'EOF'
  62. <?php
  63. /**
  64. * @param STRING|String[] $bar
  65. *
  66. * @return inT[]
  67. */
  68. EOF;
  69. $this->doTest($expected, $input);
  70. }
  71. public function testNestedArrayStuff()
  72. {
  73. $expected = <<<'EOF'
  74. <?php
  75. /**
  76. * @return int[][][]
  77. */
  78. EOF;
  79. $input = <<<'EOF'
  80. <?php
  81. /**
  82. * @return INT[][][]
  83. */
  84. EOF;
  85. $this->doTest($expected, $input);
  86. }
  87. public function testMixedAndVoid()
  88. {
  89. $expected = <<<'EOF'
  90. <?php
  91. /**
  92. * @param mixed $foo
  93. *
  94. * @return void
  95. */
  96. EOF;
  97. $input = <<<'EOF'
  98. <?php
  99. /**
  100. * @param Mixed $foo
  101. *
  102. * @return Void
  103. */
  104. EOF;
  105. $this->doTest($expected, $input);
  106. }
  107. public function testIterableFix()
  108. {
  109. $expected = <<<'EOF'
  110. <?php
  111. /**
  112. * @param iterable $foo
  113. *
  114. * @return Itter
  115. */
  116. EOF;
  117. $input = <<<'EOF'
  118. <?php
  119. /**
  120. * @param Iterable $foo
  121. *
  122. * @return Itter
  123. */
  124. EOF;
  125. $this->doTest($expected, $input);
  126. }
  127. public function testMethodAndPropertyFix()
  128. {
  129. $expected = <<<'EOF'
  130. <?php
  131. /**
  132. * @method self foo()
  133. * @property int $foo
  134. * @property-read boolean $bar
  135. * @property-write mixed $baz
  136. */
  137. EOF;
  138. $input = <<<'EOF'
  139. <?php
  140. /**
  141. * @method Self foo()
  142. * @property Int $foo
  143. * @property-read Boolean $bar
  144. * @property-write MIXED $baz
  145. */
  146. EOF;
  147. $this->doTest($expected, $input);
  148. }
  149. public function testThrows()
  150. {
  151. $expected = <<<'EOF'
  152. <?php
  153. /**
  154. * @throws static
  155. */
  156. EOF;
  157. $input = <<<'EOF'
  158. <?php
  159. /**
  160. * @throws STATIC
  161. */
  162. EOF;
  163. $this->doTest($expected, $input);
  164. }
  165. public function testInlineDoc()
  166. {
  167. $expected = <<<'EOF'
  168. <?php
  169. /**
  170. * Does stuffs with stuffs.
  171. *
  172. * @param array $stuffs {
  173. * @var bool $foo
  174. * @var int $bar
  175. * }
  176. */
  177. EOF;
  178. $input = <<<'EOF'
  179. <?php
  180. /**
  181. * Does stuffs with stuffs.
  182. *
  183. * @param array $stuffs {
  184. * @var Bool $foo
  185. * @var INT $bar
  186. * }
  187. */
  188. EOF;
  189. $this->doTest($expected, $input);
  190. }
  191. public function testWithConfig()
  192. {
  193. $expected = <<<'EOF'
  194. <?php
  195. /**
  196. * @param self|array|Foo $bar
  197. *
  198. * @return int|float|callback
  199. */
  200. EOF;
  201. $input = <<<'EOF'
  202. <?php
  203. /**
  204. * @param SELF|Array|Foo $bar
  205. *
  206. * @return inT|Float|callback
  207. */
  208. EOF;
  209. $this->fixer->configure(['groups' => ['simple', 'meta']]);
  210. $this->doTest($expected, $input);
  211. }
  212. public function testWrongConfig()
  213. {
  214. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  215. $this->expectExceptionMessageMatches('/^\[phpdoc_types\] Invalid configuration: The option "groups" .*\.$/');
  216. $this->fixer->configure(['groups' => ['__TEST__']]);
  217. }
  218. }