DoctrineAnnotationArrayAssignmentFixerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\DoctrineAnnotation;
  13. use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
  18. * @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
  19. * @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer
  20. */
  21. final class DoctrineAnnotationArrayAssignmentFixerTest extends AbstractDoctrineAnnotationFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @dataProvider provideFixCases
  32. */
  33. public function testFixWithEqual(string $expected, ?string $input = null): void
  34. {
  35. $this->fixer->configure(['operator' => '=']);
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixCases(): iterable
  39. {
  40. yield from self::createTestCases([
  41. ['
  42. /**
  43. * @Foo
  44. */'],
  45. ['
  46. /**
  47. * @Foo()
  48. */'],
  49. ['
  50. /**
  51. * @Foo(bar="baz")
  52. */'],
  53. [
  54. '
  55. /**
  56. * @Foo(bar="baz")
  57. */',
  58. ],
  59. [
  60. '
  61. /**
  62. * @Foo({bar="baz"})
  63. */',
  64. '
  65. /**
  66. * @Foo({bar:"baz"})
  67. */',
  68. ],
  69. [
  70. '
  71. /**
  72. * @Foo({bar="baz"})
  73. */',
  74. '
  75. /**
  76. * @Foo({bar:"baz"})
  77. */',
  78. ],
  79. [
  80. '
  81. /**
  82. * @Foo({bar = "baz"})
  83. */',
  84. '
  85. /**
  86. * @Foo({bar : "baz"})
  87. */',
  88. ],
  89. ['
  90. /**
  91. * See {@link https://help Help} or {@see BarClass} for details.
  92. */'],
  93. ]);
  94. yield [
  95. '<?php
  96. /**
  97. * @see \User getId()
  98. */
  99. ',
  100. ];
  101. }
  102. /**
  103. * @dataProvider provideFixWithColonCases
  104. */
  105. public function testFixWithColon(string $expected, ?string $input = null): void
  106. {
  107. $this->fixer->configure(['operator' => ':']);
  108. $this->doTest($expected, $input);
  109. }
  110. public static function provideFixWithColonCases(): iterable
  111. {
  112. yield from self::createTestCases([
  113. ['
  114. /**
  115. * @Foo
  116. */'],
  117. ['
  118. /**
  119. * @Foo()
  120. */'],
  121. ['
  122. /**
  123. * @Foo(bar:"baz")
  124. */'],
  125. [
  126. '
  127. /**
  128. * @Foo(bar:"baz")
  129. */',
  130. ],
  131. [
  132. '
  133. /**
  134. * @Foo({bar:"baz"})
  135. */',
  136. '
  137. /**
  138. * @Foo({bar="baz"})
  139. */',
  140. ],
  141. [
  142. '
  143. /**
  144. * @Foo({bar : "baz"})
  145. */',
  146. '
  147. /**
  148. * @Foo({bar = "baz"})
  149. */',
  150. ],
  151. [
  152. '
  153. /**
  154. * @Foo(foo="bar", {bar:"baz"})
  155. */',
  156. '
  157. /**
  158. * @Foo(foo="bar", {bar="baz"})
  159. */',
  160. ],
  161. ['
  162. /**
  163. * See {@link https://help Help} or {@see BarClass} for details.
  164. */'],
  165. ]);
  166. }
  167. /**
  168. * @dataProvider provideFix81Cases
  169. *
  170. * @requires PHP 8.1
  171. */
  172. public function testFix81(string $expected, ?string $input = null): void
  173. {
  174. $this->doTest($expected, $input);
  175. }
  176. public static function provideFix81Cases(): iterable
  177. {
  178. yield [
  179. '<?php class FooClass{
  180. /**
  181. * @Foo({bar = "baz"})
  182. */
  183. private readonly Foo $foo;
  184. }',
  185. '<?php class FooClass{
  186. /**
  187. * @Foo({bar : "baz"})
  188. */
  189. private readonly Foo $foo;
  190. }',
  191. ];
  192. yield [
  193. '<?php class FooClass{
  194. /**
  195. * @Foo({bar = "baz"})
  196. */
  197. readonly private Foo $foo;
  198. }',
  199. '<?php class FooClass{
  200. /**
  201. * @Foo({bar : "baz"})
  202. */
  203. readonly private Foo $foo;
  204. }',
  205. ];
  206. yield [
  207. '<?php class FooClass{
  208. /**
  209. * @Foo({bar = "baz"})
  210. */
  211. readonly Foo $foo;
  212. }',
  213. '<?php class FooClass{
  214. /**
  215. * @Foo({bar : "baz"})
  216. */
  217. readonly Foo $foo;
  218. }',
  219. ];
  220. }
  221. }