php_unit_attributes.rst 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ============================
  2. Rule ``php_unit_attributes``
  3. ============================
  4. PHPUnit attributes must be used over their respective PHPDoc-based annotations.
  5. Configuration
  6. -------------
  7. ``keep_annotations``
  8. ~~~~~~~~~~~~~~~~~~~~
  9. Whether to keep annotations or not. This may be helpful for projects that
  10. support PHP before version 8 or PHPUnit before version 10.
  11. Allowed types: ``bool``
  12. Default value: ``false``
  13. Examples
  14. --------
  15. Example #1
  16. ~~~~~~~~~~
  17. *Default* configuration.
  18. .. code-block:: diff
  19. --- Original
  20. +++ New
  21. <?php
  22. /**
  23. - * @covers \VendorName\Foo
  24. * @internal
  25. */
  26. +#[\PHPUnit\Framework\Attributes\CoversClass(\VendorName\Foo::class)]
  27. final class FooTest extends TestCase {
  28. /**
  29. * @param int $expected
  30. * @param int $actual
  31. - * @dataProvider giveMeSomeData
  32. - * @requires PHP 8.0
  33. */
  34. + #[\PHPUnit\Framework\Attributes\DataProvider('giveMeSomeData')]
  35. + #[\PHPUnit\Framework\Attributes\RequiresPhp('8.0')]
  36. public function testSomething($expected, $actual) {}
  37. }
  38. Example #2
  39. ~~~~~~~~~~
  40. With configuration: ``['keep_annotations' => true]``.
  41. .. code-block:: diff
  42. --- Original
  43. +++ New
  44. <?php
  45. /**
  46. * @covers \VendorName\Foo
  47. * @internal
  48. */
  49. +#[\PHPUnit\Framework\Attributes\CoversClass(\VendorName\Foo::class)]
  50. final class FooTest extends TestCase {
  51. /**
  52. * @param int $expected
  53. * @param int $actual
  54. * @dataProvider giveMeSomeData
  55. * @requires PHP 8.0
  56. */
  57. + #[\PHPUnit\Framework\Attributes\DataProvider('giveMeSomeData')]
  58. + #[\PHPUnit\Framework\Attributes\RequiresPhp('8.0')]
  59. public function testSomething($expected, $actual) {}
  60. }
  61. References
  62. ----------
  63. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitAttributesFixer <./../../../src/Fixer/PhpUnit/PhpUnitAttributesFixer.php>`_
  64. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitAttributesFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitAttributesFixerTest.php>`_
  65. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.