GeneralPhpdocTagRenameFixerTest.php 8.5 KB

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