php_unit_data_provider_static.rst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ======================================
  2. Rule ``php_unit_data_provider_static``
  3. ======================================
  4. Data providers must be static.
  5. Warning
  6. -------
  7. Using this rule is risky
  8. ~~~~~~~~~~~~~~~~~~~~~~~~
  9. Fixer could be risky if one is calling data provider function dynamically.
  10. Configuration
  11. -------------
  12. ``force``
  13. ~~~~~~~~~
  14. Whether to make the data providers static even if they have a dynamic class call
  15. (may introduce fatal error "using $this when not in object context", and you may
  16. have to adjust the code manually by converting dynamic calls to static ones).
  17. Allowed types: ``bool``
  18. Default value: ``false``
  19. Examples
  20. --------
  21. Example #1
  22. ~~~~~~~~~~
  23. *Default* configuration.
  24. .. code-block:: diff
  25. --- Original
  26. +++ New
  27. <?php
  28. class FooTest extends TestCase {
  29. /**
  30. * @dataProvider provideSomethingCases
  31. */
  32. public function testSomething($expected, $actual) {}
  33. - public function provideSomethingCases() {}
  34. + public static function provideSomethingCases() {}
  35. }
  36. Example #2
  37. ~~~~~~~~~~
  38. With configuration: ``['force' => true]``.
  39. .. code-block:: diff
  40. --- Original
  41. +++ New
  42. <?php
  43. class FooTest extends TestCase {
  44. /**
  45. * @dataProvider provideSomethingCases1
  46. * @dataProvider provideSomethingCases2
  47. */
  48. public function testSomething($expected, $actual) {}
  49. - public function provideSomethingCases1() { $this->getData1(); }
  50. - public function provideSomethingCases2() { self::getData2(); }
  51. + public static function provideSomethingCases1() { $this->getData1(); }
  52. + public static function provideSomethingCases2() { self::getData2(); }
  53. }
  54. Example #3
  55. ~~~~~~~~~~
  56. With configuration: ``['force' => false]``.
  57. .. code-block:: diff
  58. --- Original
  59. +++ New
  60. <?php
  61. class FooTest extends TestCase {
  62. /**
  63. * @dataProvider provideSomething1Cases
  64. * @dataProvider provideSomething2Cases
  65. */
  66. public function testSomething($expected, $actual) {}
  67. public function provideSomething1Cases() { $this->getData1(); }
  68. - public function provideSomething2Cases() { self::getData2(); }
  69. + public static function provideSomething2Cases() { self::getData2(); }
  70. }
  71. Rule sets
  72. ---------
  73. The rule is part of the following rule set:
  74. - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config:
  75. ``['force' => true]``