php_unit_namespaced.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ============================
  2. Rule ``php_unit_namespaced``
  3. ============================
  4. PHPUnit classes MUST be used in namespaced version, e.g.
  5. ``\PHPUnit\Framework\TestCase`` instead of ``\PHPUnit_Framework_TestCase``.
  6. Description
  7. -----------
  8. PHPUnit v6 has finally fully switched to namespaces.
  9. You could start preparing the upgrade by switching from non-namespaced TestCase
  10. to namespaced one.
  11. Forward compatibility layer (``\PHPUnit\Framework\TestCase`` class) was
  12. backported to PHPUnit v4.8.35 and PHPUnit v5.4.0.
  13. Extended forward compatibility layer (``PHPUnit\Framework\Assert``,
  14. ``PHPUnit\Framework\BaseTestListener``, ``PHPUnit\Framework\TestListener``
  15. classes) was introduced in v5.7.0.
  16. Warning
  17. -------
  18. Using this rule is risky
  19. ~~~~~~~~~~~~~~~~~~~~~~~~
  20. Risky when PHPUnit classes are overridden or not accessible, or when project has
  21. PHPUnit incompatibilities.
  22. Configuration
  23. -------------
  24. ``target``
  25. ~~~~~~~~~~
  26. Target version of PHPUnit.
  27. Allowed values: ``'4.8'``, ``'5.7'``, ``'6.0'``, ``'newest'``
  28. Default value: ``'newest'``
  29. Examples
  30. --------
  31. Example #1
  32. ~~~~~~~~~~
  33. *Default* configuration.
  34. .. code-block:: diff
  35. --- Original
  36. +++ New
  37. <?php
  38. -final class MyTest extends \PHPUnit_Framework_TestCase
  39. +final class MyTest extends \PHPUnit\Framework\TestCase
  40. {
  41. public function testSomething()
  42. {
  43. - PHPUnit_Framework_Assert::assertTrue(true);
  44. + PHPUnit\Framework\Assert::assertTrue(true);
  45. }
  46. }
  47. Example #2
  48. ~~~~~~~~~~
  49. With configuration: ``['target' => '4.8']``.
  50. .. code-block:: diff
  51. --- Original
  52. +++ New
  53. <?php
  54. -final class MyTest extends \PHPUnit_Framework_TestCase
  55. +final class MyTest extends \PHPUnit\Framework\TestCase
  56. {
  57. public function testSomething()
  58. {
  59. PHPUnit_Framework_Assert::assertTrue(true);
  60. }
  61. }
  62. Rule sets
  63. ---------
  64. The rule is part of the following rule sets:
  65. @PHPUnit48Migration:risky
  66. Using the `@PHPUnit48Migration:risky <./../../ruleSets/PHPUnit48MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  67. ``['target' => '4.8']``
  68. @PHPUnit50Migration:risky
  69. Using the `@PHPUnit50Migration:risky <./../../ruleSets/PHPUnit50MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  70. ``['target' => '4.8']``
  71. @PHPUnit52Migration:risky
  72. Using the `@PHPUnit52Migration:risky <./../../ruleSets/PHPUnit52MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  73. ``['target' => '4.8']``
  74. @PHPUnit54Migration:risky
  75. Using the `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  76. ``['target' => '4.8']``
  77. @PHPUnit55Migration:risky
  78. Using the `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  79. ``['target' => '4.8']``
  80. @PHPUnit56Migration:risky
  81. Using the `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  82. ``['target' => '4.8']``
  83. @PHPUnit57Migration:risky
  84. Using the `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  85. ``['target' => '5.7']``
  86. @PHPUnit60Migration:risky
  87. Using the `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  88. ``['target' => '6.0']``
  89. @PHPUnit75Migration:risky
  90. Using the `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  91. ``['target' => '6.0']``
  92. @PHPUnit84Migration:risky
  93. Using the `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  94. ``['target' => '6.0']``
  95. @PHPUnit100Migration:risky
  96. Using the `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ rule set will enable the ``php_unit_namespaced`` rule with the config below:
  97. ``['target' => '6.0']``