php_unit_data_provider_name.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ====================================
  2. Rule ``php_unit_data_provider_name``
  3. ====================================
  4. Data provider names must match the name of the test.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Fixer could be risky if one is calling data provider by name as function.
  10. Configuration
  11. -------------
  12. ``prefix``
  13. ~~~~~~~~~~
  14. Prefix that replaces "test".
  15. Allowed types: ``string``
  16. Default value: ``'provide'``
  17. ``suffix``
  18. ~~~~~~~~~~
  19. Suffix to be present at the end.
  20. Allowed types: ``string``
  21. Default value: ``'Cases'``
  22. Examples
  23. --------
  24. Example #1
  25. ~~~~~~~~~~
  26. *Default* configuration.
  27. .. code-block:: diff
  28. --- Original
  29. +++ New
  30. <?php
  31. class FooTest extends TestCase {
  32. /**
  33. - * @dataProvider dataProvider
  34. + * @dataProvider provideSomethingCases
  35. */
  36. public function testSomething($expected, $actual) {}
  37. - public function dataProvider() {}
  38. + public function provideSomethingCases() {}
  39. }
  40. Example #2
  41. ~~~~~~~~~~
  42. With configuration: ``['prefix' => 'data_', 'suffix' => '']``.
  43. .. code-block:: diff
  44. --- Original
  45. +++ New
  46. <?php
  47. class FooTest extends TestCase {
  48. /**
  49. - * @dataProvider dt_prvdr_ftr
  50. + * @dataProvider data_feature
  51. */
  52. public function test_feature($expected, $actual) {}
  53. - public function dt_prvdr_ftr() {}
  54. + public function data_feature() {}
  55. }
  56. Example #3
  57. ~~~~~~~~~~
  58. With configuration: ``['prefix' => 'provides', 'suffix' => 'Data']``.
  59. .. code-block:: diff
  60. --- Original
  61. +++ New
  62. <?php
  63. class FooTest extends TestCase {
  64. /**
  65. * @dataProvider dataProviderUsedInMultipleTests
  66. */
  67. public function testA($expected, $actual) {}
  68. /**
  69. * @dataProvider dataProviderUsedInMultipleTests
  70. */
  71. public function testB($expected, $actual) {}
  72. /**
  73. - * @dataProvider dataProviderUsedInSingleTest
  74. + * @dataProvider providesCData
  75. */
  76. public function testC($expected, $actual) {}
  77. /**
  78. * @dataProvider dataProviderUsedAsFirstInTest
  79. * @dataProvider dataProviderUsedAsSecondInTest
  80. */
  81. public function testD($expected, $actual) {}
  82. public function dataProviderUsedInMultipleTests() {}
  83. - public function dataProviderUsedInSingleTest() {}
  84. + public function providesCData() {}
  85. public function dataProviderUsedAsFirstInTest() {}
  86. public function dataProviderUsedAsSecondInTest() {}
  87. }
  88. Rule sets
  89. ---------
  90. The rule is part of the following rule set:
  91. - `@PhpCsFixer:risky <./../../ruleSets/PhpCsFixerRisky.rst>`_
  92. References
  93. ----------
  94. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitDataProviderNameFixer <./../../../src/Fixer/PhpUnit/PhpUnitDataProviderNameFixer.php>`_
  95. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitDataProviderNameFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitDataProviderNameFixerTest.php>`_
  96. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.