FinalClassFixerTest.php 8.0 KB

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