PhpdocSummaryFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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\Phpdoc;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Graham Campbell <hello@gjcampbell.co.uk>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocSummaryFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocSummaryFixer>
  23. */
  24. final class PhpdocSummaryFixerTest extends AbstractFixerTestCase
  25. {
  26. public function testFixWithTrailingSpace(): void
  27. {
  28. $expected = '<?php
  29. /**
  30. * Test.
  31. */';
  32. $input = '<?php
  33. /**
  34. * Test '.'
  35. */';
  36. $this->doTest($expected, $input);
  37. }
  38. public function testFix(): void
  39. {
  40. $expected = <<<'EOF'
  41. <?php
  42. /**
  43. * Hello there.
  44. */
  45. EOF;
  46. $input = <<<'EOF'
  47. <?php
  48. /**
  49. * Hello there
  50. */
  51. EOF;
  52. $this->doTest($expected, $input);
  53. }
  54. public function testWithPeriod(): void
  55. {
  56. $expected = <<<'EOF'
  57. <?php
  58. /**
  59. * Hello.
  60. */
  61. EOF;
  62. $this->doTest($expected);
  63. }
  64. public function testWithQuestionMark(): void
  65. {
  66. $expected = <<<'EOF'
  67. <?php
  68. /**
  69. * Hello?
  70. */
  71. EOF;
  72. $this->doTest($expected);
  73. }
  74. public function testWithExclamationMark(): void
  75. {
  76. $expected = <<<'EOF'
  77. <?php
  78. /**
  79. * Hello!
  80. */
  81. EOF;
  82. $this->doTest($expected);
  83. }
  84. public function testWithInvertedQuestionMark(): void
  85. {
  86. $expected = <<<'EOF'
  87. <?php
  88. /**
  89. * Hello¿
  90. */
  91. EOF;
  92. $this->doTest($expected);
  93. }
  94. public function testWithInvertedExclamationMark(): void
  95. {
  96. $expected = <<<'EOF'
  97. <?php
  98. /**
  99. * Hello¡
  100. */
  101. EOF;
  102. $this->doTest($expected);
  103. }
  104. public function testWithUnicodeQuestionMark(): void
  105. {
  106. $expected = <<<'EOF'
  107. <?php
  108. /**
  109. * ハロー?
  110. */
  111. EOF;
  112. $this->doTest($expected);
  113. }
  114. public function testWithUnicodeExclamationMark(): void
  115. {
  116. $expected = <<<'EOF'
  117. <?php
  118. /**
  119. * ハロー!
  120. */
  121. EOF;
  122. $this->doTest($expected);
  123. }
  124. public function testWithJapanesePeriod(): void
  125. {
  126. $expected = <<<'EOF'
  127. <?php
  128. /**
  129. * ハロー。
  130. */
  131. EOF;
  132. $this->doTest($expected);
  133. }
  134. public function testFixIncBlank(): void
  135. {
  136. $expected = <<<'EOF'
  137. <?php
  138. /**
  139. * Hi.
  140. *
  141. */
  142. EOF;
  143. $input = <<<'EOF'
  144. <?php
  145. /**
  146. * Hi
  147. *
  148. */
  149. EOF;
  150. $this->doTest($expected, $input);
  151. }
  152. public function testFixMultiline(): void
  153. {
  154. $expected = <<<'EOF'
  155. <?php
  156. /**
  157. * Hello
  158. * there.
  159. */
  160. EOF;
  161. $input = <<<'EOF'
  162. <?php
  163. /**
  164. * Hello
  165. * there
  166. */
  167. EOF;
  168. $this->doTest($expected, $input);
  169. }
  170. public function testWithList(): void
  171. {
  172. $expected = <<<'EOF'
  173. <?php
  174. /**
  175. * Options:
  176. * * a: aaa
  177. * * b: bbb
  178. * * c: ccc
  179. */
  180. /**
  181. * Options:
  182. *
  183. * * a: aaa
  184. * * b: bbb
  185. * * c: ccc
  186. */
  187. EOF;
  188. $this->doTest($expected);
  189. }
  190. public function testWithTags(): void
  191. {
  192. $expected = <<<'EOF'
  193. <?php
  194. /**
  195. * Hello there.
  196. *
  197. * @param string $foo
  198. *
  199. * @return bool
  200. */
  201. EOF;
  202. $input = <<<'EOF'
  203. <?php
  204. /**
  205. * Hello there
  206. *
  207. * @param string $foo
  208. *
  209. * @return bool
  210. */
  211. EOF;
  212. $this->doTest($expected, $input);
  213. }
  214. public function testWithLongDescription(): void
  215. {
  216. $expected = <<<'EOF'
  217. <?php
  218. /**
  219. * Hello there.
  220. *
  221. * Long description
  222. * goes here.
  223. *
  224. * @return bool
  225. */
  226. EOF;
  227. $input = <<<'EOF'
  228. <?php
  229. /**
  230. * Hello there
  231. *
  232. * Long description
  233. * goes here.
  234. *
  235. * @return bool
  236. */
  237. EOF;
  238. $this->doTest($expected, $input);
  239. }
  240. public function testCrazyMultiLineComments(): void
  241. {
  242. $expected = <<<'EOF'
  243. <?php
  244. /**
  245. * Clients accept an array of constructor parameters.
  246. *
  247. * Here's an example of creating a client using a URI template for the
  248. * client's base_url and an array of default request options to apply
  249. * to each request:
  250. *
  251. * $client = new Client([
  252. * 'base_url' => [
  253. * 'https://www.foo.com/{version}/',
  254. * ['version' => '123']
  255. * ],
  256. * 'defaults' => [
  257. * 'timeout' => 10,
  258. * 'allow_redirects' => false,
  259. * 'proxy' => '192.168.16.1:10'
  260. * ]
  261. * ]);
  262. *
  263. * @param _AutogeneratedInputConfiguration $config Client configuration settings
  264. * - base_url: Base URL of the client that is merged into relative URLs.
  265. * Can be a string or an array that contains a URI template followed
  266. * by an associative array of expansion variables to inject into the
  267. * URI template.
  268. * - handler: callable RingPHP handler used to transfer requests
  269. * - message_factory: Factory used to create request and response object
  270. * - defaults: Default request options to apply to each request
  271. * - emitter: Event emitter used for request events
  272. * - fsm: (internal use only) The request finite state machine. A
  273. * function that accepts a transaction and optional final state. The
  274. * function is responsible for transitioning a request through its
  275. * lifecycle events.
  276. * @param string $foo
  277. */
  278. EOF;
  279. $input = <<<'EOF'
  280. <?php
  281. /**
  282. * Clients accept an array of constructor parameters
  283. *
  284. * Here's an example of creating a client using a URI template for the
  285. * client's base_url and an array of default request options to apply
  286. * to each request:
  287. *
  288. * $client = new Client([
  289. * 'base_url' => [
  290. * 'https://www.foo.com/{version}/',
  291. * ['version' => '123']
  292. * ],
  293. * 'defaults' => [
  294. * 'timeout' => 10,
  295. * 'allow_redirects' => false,
  296. * 'proxy' => '192.168.16.1:10'
  297. * ]
  298. * ]);
  299. *
  300. * @param _AutogeneratedInputConfiguration $config Client configuration settings
  301. * - base_url: Base URL of the client that is merged into relative URLs.
  302. * Can be a string or an array that contains a URI template followed
  303. * by an associative array of expansion variables to inject into the
  304. * URI template.
  305. * - handler: callable RingPHP handler used to transfer requests
  306. * - message_factory: Factory used to create request and response object
  307. * - defaults: Default request options to apply to each request
  308. * - emitter: Event emitter used for request events
  309. * - fsm: (internal use only) The request finite state machine. A
  310. * function that accepts a transaction and optional final state. The
  311. * function is responsible for transitioning a request through its
  312. * lifecycle events.
  313. * @param string $foo
  314. */
  315. EOF;
  316. $this->doTest($expected, $input);
  317. }
  318. public function testWithNoDescription(): void
  319. {
  320. $expected = <<<'EOF'
  321. <?php
  322. /**
  323. * @return bool
  324. */
  325. EOF;
  326. $this->doTest($expected);
  327. }
  328. /**
  329. * @dataProvider provideWithInheritDocCases
  330. */
  331. public function testWithInheritDoc(string $expected): void
  332. {
  333. $this->doTest($expected);
  334. }
  335. /**
  336. * @return iterable<array{string}>
  337. */
  338. public static function provideWithInheritDocCases(): iterable
  339. {
  340. yield [
  341. '<?php
  342. /**
  343. * {@inheritdoc}
  344. */
  345. ',
  346. ];
  347. yield [
  348. '<?php
  349. /**
  350. * @inheritDoc
  351. */
  352. ',
  353. ];
  354. }
  355. public function testEmptyDocBlock(): void
  356. {
  357. $expected = <<<'EOF'
  358. <?php
  359. /**
  360. *
  361. */
  362. EOF;
  363. $this->doTest($expected);
  364. }
  365. /**
  366. * @dataProvider provideMessyWhitespacesCases
  367. */
  368. public function testMessyWhitespaces(string $expected, ?string $input = null): void
  369. {
  370. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  371. $this->doTest($expected, $input);
  372. }
  373. /**
  374. * @return iterable<array{string, string}>
  375. */
  376. public static function provideMessyWhitespacesCases(): iterable
  377. {
  378. yield [
  379. "<?php\r\n\t/**\r\n\t * Hello there.\r\n\t */",
  380. "<?php\r\n\t/**\r\n\t * Hello there\r\n\t */",
  381. ];
  382. }
  383. }