FinalClassFixerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\ClassNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ClassNotation\FinalClassFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\FinalClassFixer>
  22. */
  23. final class FinalClassFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield ['<?php /** @Entity */ class MyEntity {}'];
  38. yield ['<?php use Doctrine\ORM\Mapping as ORM; /** @ORM\Entity */ class MyEntity {}'];
  39. yield ['<?php use Doctrine\ORM\Mapping as ORM; /** @ORM\Entity(repositoryClass="MyRepository") */ class MyEntity {}'];
  40. yield ['<?php use Doctrine\ORM\Mapping; /** @Mapping\Entity */ class MyEntity {}'];
  41. yield ['<?php use Doctrine\ORM; /** @ORM\Mapping\Entity */ class MyEntity {}'];
  42. yield ['<?php /** @Document */ class MyDocument {}'];
  43. yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; /** @ODM\Document */ class MyEntity {}'];
  44. yield ['<?php /** @entity */ class MyEntity {}'];
  45. yield ['<?php use Doctrine\ORM\Mapping as ORM; /** @orm\entity */ class MyEntity {}'];
  46. yield ['<?php abstract class MyAbstract {}'];
  47. yield ['<?php trait MyTrait {}'];
  48. yield ['<?php interface MyInterface {}'];
  49. yield ['<?php echo Exception::class;'];
  50. yield [
  51. '<?php final class MyClass {}',
  52. '<?php class MyClass {}',
  53. ];
  54. yield [
  55. '<?php final class MyClass extends MyAbstract {}',
  56. '<?php class MyClass extends MyAbstract {}',
  57. ];
  58. yield [
  59. '<?php final class MyClass implements MyInterface {}',
  60. '<?php class MyClass implements MyInterface {}',
  61. ];
  62. yield [
  63. '<?php /** @codeCoverageIgnore */ final class MyEntity {}',
  64. '<?php /** @codeCoverageIgnore */ class MyEntity {}',
  65. ];
  66. yield [
  67. '<?php final class A {} abstract class B {} final class C {}',
  68. '<?php class A {} abstract class B {} class C {}',
  69. ];
  70. yield [
  71. '<?php /** @internal Map my app to an @Entity */ final class MyMapper {}',
  72. '<?php /** @internal Map my app to an @Entity */ class MyMapper {}',
  73. ];
  74. yield ['<?php $anonymClass = new class {};'];
  75. }
  76. /**
  77. * @dataProvider provideFix80Cases
  78. *
  79. * @requires PHP 8.0
  80. */
  81. public function testFix80(string $expected, ?string $input = null): void
  82. {
  83. $this->doTest($expected, $input);
  84. }
  85. /**
  86. * @return iterable<array{0: string, 1?: string}>
  87. */
  88. public static function provideFix80Cases(): iterable
  89. {
  90. yield ['<?php #[Entity] class MyEntity {}'];
  91. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class MyEntity {}'];
  92. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass:"MyRepository")] class MyEntity {}'];
  93. yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] class MyEntity {}'];
  94. yield ['<?php #[Document] class MyDocument {}'];
  95. yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] class MyEntity {}'];
  96. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\entity] class MyEntity {}'];
  97. yield ['<?php #[IgnoredAttribute] #[Entity] class MyEntity {}'];
  98. yield ['<?php #[IgnoredAttribute("Some-Value"), Entity] class MyEntity {}'];
  99. // Test with comments in between attributes and class
  100. yield ['<?php #[Entity] /* some comment */ class MyEntity {}'];
  101. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] /* some comment */ class MyEntity {}'];
  102. yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] /* some comment */ class MyEntity {}'];
  103. yield ['<?php #[Document] /* some comment */ class MyDocument {}'];
  104. yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] /* some comment */ class MyEntity {}'];
  105. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute] #[Entity] /* some comment */ class MyEntity {}'];
  106. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute("Some-Value"), Entity] /* some comment */ class MyEntity {}'];
  107. // Test with comments before the class
  108. yield ['<?php /* some comment */ #[Entity] class MyEntity {}'];
  109. yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class MyEntity {}'];
  110. yield ['<?php /* some comment */ use Doctrine\ORM; #[ORM\Mapping\Entity] class MyEntity {}'];
  111. yield ['<?php /* some comment */ #[Document] class MyDocument {}'];
  112. yield ['<?php /* some comment */ use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] class MyEntity {}'];
  113. yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[ORM\entity] class MyEntity {}'];
  114. yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute] #[Entity] class MyEntity {}'];
  115. yield ['<?php /* some comment */ use Doctrine\ORM\Mapping as ORM; #[IgnoredAttribute, Entity] class MyEntity {}'];
  116. // Multiline tests
  117. yield [
  118. <<<'EOF'
  119. <?php
  120. use Doctrine\ORM;
  121. #[IgnoredAttribute("Some-Value"), IgnoredAttribute("Another-Value")]
  122. #[ORM\Mapping\Entity]
  123. /**
  124. * multi
  125. * line
  126. */
  127. class MyEntity {}
  128. EOF,
  129. ];
  130. yield [
  131. <<<'EOF'
  132. <?php
  133. use Doctrine\ORM;
  134. #[
  135. IgnoredAttribute("Some-Value"),
  136. IgnoredAttribute("Another-Value"),#
  137. ORM\Mapping\Entity,
  138. ]
  139. /**
  140. * multi
  141. * line
  142. */
  143. class MyEntity {}
  144. EOF,
  145. ];
  146. }
  147. /**
  148. * @dataProvider provideFix82Cases
  149. *
  150. * @requires PHP 8.2
  151. */
  152. public function testFix82(string $expected, ?string $input = null): void
  153. {
  154. $this->doTest($expected, $input);
  155. }
  156. /**
  157. * @return iterable<array{0: string, 1?: string}>
  158. */
  159. public static function provideFix82Cases(): iterable
  160. {
  161. yield ['<?php #[Entity] readonly class MyEntity {}'];
  162. yield ['<?php use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] readonly class MyEntity {}'];
  163. yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] readonly class MyEntity {}'];
  164. yield ['<?php #[Document] readonly class MyDocument {}'];
  165. yield ['<?php use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; #[ODM\Document] readonly class MyEntity {}'];
  166. yield ['<?php use Doctrine\ORM; #[ORM\Mapping\Entity] readonly /* ... */ class MyEntity {}'];
  167. yield [
  168. <<<'EOF'
  169. <?php
  170. use Doctrine\ORM;
  171. #[ORM\Mapping\Entity]
  172. readonly
  173. /**
  174. * multi
  175. * line
  176. */
  177. class MyEntity {}
  178. EOF,
  179. ];
  180. }
  181. }