GeneralPhpdocTagRenameFixerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer
  19. */
  20. final class GeneralPhpdocTagRenameFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param array<string, mixed> $configuration
  24. *
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  28. {
  29. $this->fixer->configure($configuration);
  30. $this->doTest($expected, $input);
  31. }
  32. public static function provideFixCases(): iterable
  33. {
  34. yield [
  35. '<?php
  36. /**
  37. * @inheritdocs
  38. * @inheritDocs
  39. * {@inheritdocs}
  40. * {@inheritDocs}
  41. * @see Foo::bar()
  42. * {@see Foo::bar()}
  43. */',
  44. ];
  45. yield [
  46. '<?php
  47. /**
  48. * @inheritDoc
  49. * @inheritDoc
  50. * {@inheritDoc}
  51. * {@inheritDoc}
  52. * @see Foo::bar()
  53. * {@see Foo::bar()}
  54. */',
  55. '<?php
  56. /**
  57. * @inheritdocs
  58. * @inheritDocs
  59. * {@inheritdocs}
  60. * {@inheritDocs}
  61. * @see Foo::bar()
  62. * {@see Foo::bar()}
  63. */',
  64. [
  65. 'replacements' => ['inheritDocs' => 'inheritDoc'],
  66. ],
  67. ];
  68. yield [
  69. '<?php
  70. /**
  71. * @inheritdoc
  72. * @inheritdoc
  73. * {@inheritdoc}
  74. * {@inheritdoc}
  75. * @see Foo::bar()
  76. * {@see Foo::bar()}
  77. */',
  78. '<?php
  79. /**
  80. * @inheritdocs
  81. * @inheritDocs
  82. * {@inheritdocs}
  83. * {@inheritDocs}
  84. * @see Foo::bar()
  85. * {@see Foo::bar()}
  86. */',
  87. [
  88. 'fix_annotation' => true,
  89. 'fix_inline' => true,
  90. 'replacements' => ['inheritdocs' => 'inheritdoc'],
  91. 'case_sensitive' => false,
  92. ],
  93. ];
  94. yield [
  95. '<?php
  96. /**
  97. * @inheritDoc
  98. * @inheritDoc
  99. * {@inheritdocs}
  100. * {@inheritDocs}
  101. * @see Foo::bar()
  102. * {@see Foo::bar()}
  103. */',
  104. '<?php
  105. /**
  106. * @inheritdocs
  107. * @inheritDocs
  108. * {@inheritdocs}
  109. * {@inheritDocs}
  110. * @see Foo::bar()
  111. * {@see Foo::bar()}
  112. */',
  113. [
  114. 'fix_inline' => false,
  115. 'replacements' => ['inheritDocs' => 'inheritDoc'],
  116. ],
  117. ];
  118. yield [
  119. '<?php
  120. /**
  121. * @inheritdocs
  122. * @inheritDocs
  123. * {@inheritDoc}
  124. * {@inheritDoc}
  125. * @see Foo::bar()
  126. * {@see Foo::bar()}
  127. */',
  128. '<?php
  129. /**
  130. * @inheritdocs
  131. * @inheritDocs
  132. * {@inheritdocs}
  133. * {@inheritDocs}
  134. * @see Foo::bar()
  135. * {@see Foo::bar()}
  136. */',
  137. [
  138. 'fix_annotation' => false,
  139. 'replacements' => ['inheritDocs' => 'inheritDoc'],
  140. ],
  141. ];
  142. yield [
  143. '<?php
  144. /**
  145. * @inheritdocs
  146. * @inheritDoc
  147. * {@inheritdocs}
  148. * {@inheritDoc}
  149. * @see Foo::bar()
  150. * {@see Foo::bar()}
  151. */',
  152. '<?php
  153. /**
  154. * @inheritdocs
  155. * @inheritDocs
  156. * {@inheritdocs}
  157. * {@inheritDocs}
  158. * @see Foo::bar()
  159. * {@see Foo::bar()}
  160. */',
  161. [
  162. 'case_sensitive' => true,
  163. 'replacements' => ['inheritDocs' => 'inheritDoc'],
  164. ],
  165. ];
  166. yield [
  167. '<?php
  168. /**
  169. * @inheritdoc
  170. * @inheritdoc
  171. * {@inheritdoc}
  172. * {@inheritdoc}
  173. * @see Foo::bar()
  174. * {@see Foo::bar()}
  175. */',
  176. '<?php
  177. /**
  178. * @inheritdocs
  179. * @inheritDocs
  180. * {@inheritdocs}
  181. * {@inheritDocs}
  182. * @link Foo::bar()
  183. * {@link Foo::bar()}
  184. */',
  185. [
  186. 'replacements' => [
  187. 'inheritdocs' => 'inheritdoc',
  188. 'link' => 'see',
  189. ],
  190. ],
  191. ];
  192. yield [
  193. '<?php
  194. /**
  195. * @var int $foo
  196. * @Annotation("@type")
  197. */',
  198. '<?php
  199. /**
  200. * @type int $foo
  201. * @Annotation("@type")
  202. */',
  203. [
  204. 'fix_annotation' => true,
  205. 'fix_inline' => true,
  206. 'replacements' => [
  207. 'type' => 'var',
  208. ],
  209. ],
  210. ];
  211. yield [
  212. '<?php
  213. /**
  214. * @var int $foo
  215. * @Annotation("@type")
  216. */',
  217. '<?php
  218. /**
  219. * @type int $foo
  220. * @Annotation("@type")
  221. */',
  222. [
  223. 'fix_annotation' => true,
  224. 'fix_inline' => false,
  225. 'replacements' => [
  226. 'type' => 'var',
  227. ],
  228. ],
  229. ];
  230. }
  231. public function testConfigureWithInvalidOption(): void
  232. {
  233. $this->expectException(InvalidFixerConfigurationException::class);
  234. $this->expectExceptionMessageMatches('/^\[general_phpdoc_tag_rename\] Invalid configuration: The option "replacements" with value true is expected to be of type "array", but is of type ".*ool.*"\.$/');
  235. $this->fixer->configure([
  236. 'replacements' => true,
  237. ]);
  238. }
  239. public function testConfigureWithUnknownOption(): void
  240. {
  241. $this->expectException(InvalidFixerConfigurationException::class);
  242. $this->expectExceptionMessageMatches('/^\[general_phpdoc_tag_rename\] Invalid configuration: The option "foo" does not exist\. (Known|Defined) options are: "case_sensitive", "fix_annotation", "fix_inline", "replacements"\.$/');
  243. $this->fixer->configure([
  244. 'foo' => true,
  245. ]);
  246. }
  247. /**
  248. * @param array<mixed> $replacements
  249. *
  250. * @dataProvider provideConfigureWithInvalidReplacementsCases
  251. */
  252. public function testConfigureWithInvalidReplacements(array $replacements, bool $caseSensitive, string $expectedMessage): void
  253. {
  254. $this->expectException(InvalidFixerConfigurationException::class);
  255. $this->expectExceptionMessageMatches(sprintf(
  256. '/^\[general_phpdoc_tag_rename\] Invalid configuration: %s$/',
  257. preg_quote($expectedMessage, '/')
  258. ));
  259. $this->fixer->configure([
  260. 'replacements' => $replacements,
  261. 'case_sensitive' => $caseSensitive,
  262. ]);
  263. }
  264. public static function provideConfigureWithInvalidReplacementsCases(): iterable
  265. {
  266. yield [
  267. [1 => 'abc'],
  268. true,
  269. 'Tag to replace must be a string.',
  270. ];
  271. yield [
  272. ['a' => null],
  273. true,
  274. 'Tag to replace to from "a" must be a string.',
  275. ];
  276. yield [
  277. ['see' => 'link*/'],
  278. true,
  279. 'Tag "see" cannot be replaced by invalid tag "link*/".',
  280. ];
  281. yield [
  282. [
  283. 'link' => 'see',
  284. 'a' => 'b',
  285. 'see' => 'link',
  286. ],
  287. true,
  288. 'Cannot change tag "link" to tag "see", as the tag "see" is configured to be replaced to "link".',
  289. ];
  290. yield [
  291. [
  292. 'b' => 'see',
  293. 'see' => 'link',
  294. 'link' => 'b',
  295. ],
  296. true,
  297. 'Cannot change tag "b" to tag "see", as the tag "see" is configured to be replaced to "link".',
  298. ];
  299. yield [
  300. [
  301. 'see' => 'link',
  302. 'link' => 'b',
  303. ],
  304. true,
  305. 'Cannot change tag "see" to tag "link", as the tag "link" is configured to be replaced to "b".',
  306. ];
  307. yield [
  308. [
  309. 'Foo' => 'bar',
  310. 'foo' => 'baz',
  311. ],
  312. false,
  313. 'Tag "foo" cannot be configured to be replaced with several different tags when case sensitivity is off.',
  314. ];
  315. }
  316. }