PhpdocSeparationFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer
  19. */
  20. final class PhpdocSeparationFixerTest extends AbstractFixerTestCase
  21. {
  22. public function testFix()
  23. {
  24. $this->doTest('<?php
  25. /** @param EngineInterface $templating
  26. *@return void
  27. */');
  28. $expected = <<<'EOF'
  29. <?php
  30. /**
  31. * @param EngineInterface $templating
  32. *
  33. * @return void
  34. */
  35. EOF;
  36. $input = <<<'EOF'
  37. <?php
  38. /**
  39. * @param EngineInterface $templating
  40. * @return void
  41. */
  42. EOF;
  43. $this->doTest($expected, $input);
  44. }
  45. public function testFixMoreTags()
  46. {
  47. $expected = <<<'EOF'
  48. <?php
  49. /**
  50. * Hello there!
  51. *
  52. * @internal
  53. *
  54. * @param string $foo
  55. *
  56. * @throws Exception
  57. *
  58. * @return bool
  59. */
  60. EOF;
  61. $input = <<<'EOF'
  62. <?php
  63. /**
  64. * Hello there!
  65. * @internal
  66. * @param string $foo
  67. * @throws Exception
  68. *
  69. *
  70. *
  71. * @return bool
  72. */
  73. EOF;
  74. $this->doTest($expected, $input);
  75. }
  76. public function testFixSpreadOut()
  77. {
  78. $expected = <<<'EOF'
  79. <?php
  80. /**
  81. * Hello there!
  82. *
  83. * Long description
  84. * goes here.
  85. *
  86. * @param string $foo
  87. * @param bool $bar Bar
  88. *
  89. * @throws Exception|RuntimeException
  90. *
  91. * @return bool
  92. */
  93. EOF;
  94. $input = <<<'EOF'
  95. <?php
  96. /**
  97. * Hello there!
  98. *
  99. * Long description
  100. * goes here.
  101. * @param string $foo
  102. *
  103. *
  104. * @param bool $bar Bar
  105. *
  106. *
  107. *
  108. * @throws Exception|RuntimeException
  109. *
  110. *
  111. *
  112. *
  113. * @return bool
  114. */
  115. EOF;
  116. $this->doTest($expected, $input);
  117. }
  118. public function testMultiLineComments()
  119. {
  120. $expected = <<<'EOF'
  121. <?php
  122. /**
  123. * Hello there!
  124. *
  125. * Long description
  126. * goes here.
  127. *
  128. * @param string $foo test 123
  129. * asdasdasd
  130. * @param bool $bar qwerty
  131. *
  132. * @throws Exception|RuntimeException
  133. *
  134. * @return bool
  135. */
  136. EOF;
  137. $input = <<<'EOF'
  138. <?php
  139. /**
  140. * Hello there!
  141. *
  142. * Long description
  143. * goes here.
  144. * @param string $foo test 123
  145. * asdasdasd
  146. * @param bool $bar qwerty
  147. * @throws Exception|RuntimeException
  148. * @return bool
  149. */
  150. EOF;
  151. $this->doTest($expected, $input);
  152. }
  153. public function testCrazyMultiLineComments()
  154. {
  155. $expected = <<<'EOF'
  156. <?php
  157. /**
  158. * Clients accept an array of constructor parameters.
  159. *
  160. * Here's an example of creating a client using an URI template for the
  161. * client's base_url and an array of default request options to apply
  162. * to each request:
  163. *
  164. * $client = new Client([
  165. * 'base_url' => [
  166. * 'http://www.foo.com/{version}/',
  167. * ['version' => '123']
  168. * ],
  169. * 'defaults' => [
  170. * 'timeout' => 10,
  171. * 'allow_redirects' => false,
  172. * 'proxy' => '192.168.16.1:10'
  173. * ]
  174. * ]);
  175. *
  176. * @param array $config Client configuration settings
  177. * - base_url: Base URL of the client that is merged into relative URLs.
  178. * Can be a string or an array that contains a URI template followed
  179. * by an associative array of expansion variables to inject into the
  180. * URI template.
  181. * - handler: callable RingPHP handler used to transfer requests
  182. * - message_factory: Factory used to create request and response object
  183. * - defaults: Default request options to apply to each request
  184. * - emitter: Event emitter used for request events
  185. * - fsm: (internal use only) The request finite state machine. A
  186. * function that accepts a transaction and optional final state. The
  187. * function is responsible for transitioning a request through its
  188. * lifecycle events.
  189. * @param string $foo
  190. */
  191. EOF;
  192. $this->doTest($expected);
  193. }
  194. public function testDoctrineExample()
  195. {
  196. $expected = <<<'EOF'
  197. <?php
  198. /**
  199. * PersistentObject base class that implements getter/setter methods for all mapped fields and associations
  200. * by overriding __call.
  201. *
  202. * This class is a forward compatible implementation of the PersistentObject trait.
  203. *
  204. * Limitations:
  205. *
  206. * 1. All persistent objects have to be associated with a single ObjectManager, multiple
  207. * ObjectManagers are not supported. You can set the ObjectManager with `PersistentObject#setObjectManager()`.
  208. * 2. Setters and getters only work if a ClassMetadata instance was injected into the PersistentObject.
  209. * This is either done on `postLoad` of an object or by accessing the global object manager.
  210. * 3. There are no hooks for setters/getters. Just implement the method yourself instead of relying on __call().
  211. * 4. Slower than handcoded implementations: An average of 7 method calls per access to a field and 11 for an association.
  212. * 5. Only the inverse side associations get autoset on the owning side as well. Setting objects on the owning side
  213. * will not set the inverse side associations.
  214. *
  215. * @example
  216. *
  217. * PersistentObject::setObjectManager($em);
  218. *
  219. * class Foo extends PersistentObject
  220. * {
  221. * private $id;
  222. * }
  223. *
  224. * $foo = new Foo();
  225. * $foo->getId(); // method exists through __call
  226. *
  227. * @author Benjamin Eberlei <kontakt@beberlei.de>
  228. */
  229. EOF;
  230. $this->doTest($expected);
  231. }
  232. public function testSymfonyExample()
  233. {
  234. $expected = <<<'EOF'
  235. <?php
  236. /**
  237. * Constructor.
  238. *
  239. * Depending on how you want the storage driver to behave you probably
  240. * want to override this constructor entirely.
  241. *
  242. * List of options for $options array with their defaults.
  243. *
  244. * @see https://php.net/session.configuration for options
  245. *
  246. * but we omit 'session.' from the beginning of the keys for convenience.
  247. *
  248. * ("auto_start", is not supported as it tells PHP to start a session before
  249. * PHP starts to execute user-land code. Setting during runtime has no effect).
  250. *
  251. * cache_limiter, "nocache" (use "0" to prevent headers from being sent entirely).
  252. * cookie_domain, ""
  253. * cookie_httponly, ""
  254. * cookie_lifetime, "0"
  255. * cookie_path, "/"
  256. * cookie_secure, ""
  257. * entropy_file, ""
  258. * entropy_length, "0"
  259. * gc_divisor, "100"
  260. * gc_maxlifetime, "1440"
  261. * gc_probability, "1"
  262. * hash_bits_per_character, "4"
  263. * hash_function, "0"
  264. * name, "PHPSESSID"
  265. * referer_check, ""
  266. * serialize_handler, "php"
  267. * use_cookies, "1"
  268. * use_only_cookies, "1"
  269. * use_trans_sid, "0"
  270. * upload_progress.enabled, "1"
  271. * upload_progress.cleanup, "1"
  272. * upload_progress.prefix, "upload_progress_"
  273. * upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
  274. * upload_progress.freq, "1%"
  275. * upload_progress.min-freq, "1"
  276. * url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
  277. *
  278. * @param array $options Session configuration options.
  279. * @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $handler
  280. * @param MetadataBag $metaBag MetadataBag.
  281. */
  282. EOF;
  283. $this->doTest($expected);
  284. }
  285. public function testDeprecatedAndSeeTags()
  286. {
  287. $expected = <<<'EOF'
  288. <?php
  289. /**
  290. * Hi!
  291. *
  292. * @author Bar Baz <foo@example.com>
  293. *
  294. * @deprecated As of some version.
  295. * @see Replacement
  296. * described here.
  297. *
  298. * @param string $foo test 123
  299. * @param bool $bar qwerty
  300. *
  301. * @return void
  302. */
  303. EOF;
  304. $input = <<<'EOF'
  305. <?php
  306. /**
  307. * Hi!
  308. *
  309. * @author Bar Baz <foo@example.com>
  310. * @deprecated As of some version.
  311. *
  312. * @see Replacement
  313. * described here.
  314. * @param string $foo test 123
  315. * @param bool $bar qwerty
  316. *
  317. * @return void
  318. */
  319. EOF;
  320. $this->doTest($expected, $input);
  321. }
  322. public function testPropertyTags()
  323. {
  324. $expected = <<<'EOF'
  325. <?php
  326. /**
  327. * @author Bar Baz <foo@example.com>
  328. *
  329. * @property int $foo
  330. * @property-read int $foo
  331. * @property-write int $bar
  332. */
  333. EOF;
  334. $input = <<<'EOF'
  335. <?php
  336. /**
  337. * @author Bar Baz <foo@example.com>
  338. * @property int $foo
  339. *
  340. * @property-read int $foo
  341. *
  342. * @property-write int $bar
  343. */
  344. EOF;
  345. $this->doTest($expected, $input);
  346. }
  347. public function testClassDocBlock()
  348. {
  349. $expected = <<<'EOF'
  350. <?php
  351. namespace Foo;
  352. /**
  353. * This is a class that does classy things.
  354. *
  355. * @internal
  356. *
  357. * @package Foo
  358. * @subpackage Foo\Bar
  359. *
  360. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  361. * @author Graham Campbell <graham@alt-three.com>
  362. * @copyright Foo Bar
  363. * @license MIT
  364. */
  365. class Bar {}
  366. EOF;
  367. $input = <<<'EOF'
  368. <?php
  369. namespace Foo;
  370. /**
  371. * This is a class that does classy things.
  372. * @internal
  373. * @package Foo
  374. *
  375. *
  376. * @subpackage Foo\Bar
  377. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  378. *
  379. * @author Graham Campbell <graham@alt-three.com>
  380. *
  381. * @copyright Foo Bar
  382. *
  383. *
  384. * @license MIT
  385. */
  386. class Bar {}
  387. EOF;
  388. $this->doTest($expected, $input);
  389. }
  390. public function testPoorAlignment()
  391. {
  392. $expected = <<<'EOF'
  393. <?php
  394. namespace Foo;
  395. /**
  396. * This is a class that does classy things.
  397. *
  398. * @internal
  399. *
  400. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  401. *@author Graham Campbell <graham@alt-three.com>
  402. */
  403. class Bar {}
  404. EOF;
  405. $input = <<<'EOF'
  406. <?php
  407. namespace Foo;
  408. /**
  409. * This is a class that does classy things.
  410. *
  411. * @internal
  412. *
  413. *
  414. *
  415. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  416. *
  417. *
  418. *@author Graham Campbell <graham@alt-three.com>
  419. */
  420. class Bar {}
  421. EOF;
  422. $this->doTest($expected, $input);
  423. }
  424. public function testDoNotMoveUnknownAnnotations()
  425. {
  426. $expected = <<<'EOF'
  427. <?php
  428. /**
  429. * @expectedException Exception
  430. * @expectedExceptionMessage Oh Noes!
  431. * Something when wrong!
  432. *
  433. *
  434. * @Hello\Test\Foo(asd)
  435. * @Method("GET")
  436. *
  437. * @param string $expected
  438. * @param string $input
  439. */
  440. EOF;
  441. $input = <<<'EOF'
  442. <?php
  443. /**
  444. * @expectedException Exception
  445. * @expectedExceptionMessage Oh Noes!
  446. * Something when wrong!
  447. *
  448. *
  449. * @Hello\Test\Foo(asd)
  450. * @Method("GET")
  451. *
  452. * @param string $expected
  453. *
  454. * @param string $input
  455. */
  456. EOF;
  457. $this->doTest($expected, $input);
  458. }
  459. public function testInheritDoc()
  460. {
  461. $expected = <<<'EOF'
  462. <?php
  463. /**
  464. * {@inheritdoc}
  465. *
  466. * @param string $expected
  467. * @param string $input
  468. */
  469. EOF;
  470. $input = <<<'EOF'
  471. <?php
  472. /**
  473. * {@inheritdoc}
  474. * @param string $expected
  475. * @param string $input
  476. */
  477. EOF;
  478. $this->doTest($expected, $input);
  479. }
  480. public function testEmptyDocBlock()
  481. {
  482. $expected = <<<'EOF'
  483. <?php
  484. /**
  485. *
  486. */
  487. EOF;
  488. $this->doTest($expected);
  489. }
  490. public function testLargerEmptyDocBlock()
  491. {
  492. $expected = <<<'EOF'
  493. <?php
  494. /**
  495. *
  496. *
  497. *
  498. *
  499. */
  500. EOF;
  501. $this->doTest($expected);
  502. }
  503. public function testMessyWhitespaces()
  504. {
  505. $expected = "<?php\t/**\r\n\t * @param string \$text\r\n\t *\r\n\t * @return string\r\n\t */";
  506. $input = "<?php\t/**\r\n\t * @param string \$text\r\n\t * @return string\r\n\t */";
  507. $this->doTest($expected, $input);
  508. }
  509. public function testWithSpacing()
  510. {
  511. $expected = '<?php
  512. /**
  513. * Foo
  514. *
  515. * @bar 123
  516. *
  517. * {@inheritdoc} '.'
  518. *
  519. * @param string $expected
  520. * @param string $input
  521. */';
  522. $input = '<?php
  523. /**
  524. * Foo
  525. * @bar 123
  526. *
  527. * {@inheritdoc} '.'
  528. * @param string $expected
  529. * @param string $input
  530. */';
  531. $this->doTest($expected, $input);
  532. }
  533. }