PhpdocSummaryFixerTest.php 10 KB

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