php_unit_namespaced.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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'`` and ``'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 <./../../ruleSets/PHPUnit48MigrationRisky.rst>`_ with config:
  66. ``['target' => '4.8']``
  67. - `@PHPUnit50Migration:risky <./../../ruleSets/PHPUnit50MigrationRisky.rst>`_ with config:
  68. ``['target' => '4.8']``
  69. - `@PHPUnit52Migration:risky <./../../ruleSets/PHPUnit52MigrationRisky.rst>`_ with config:
  70. ``['target' => '4.8']``
  71. - `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ with config:
  72. ``['target' => '4.8']``
  73. - `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ with config:
  74. ``['target' => '4.8']``
  75. - `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ with config:
  76. ``['target' => '4.8']``
  77. - `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ with config:
  78. ``['target' => '5.7']``
  79. - `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ with config:
  80. ``['target' => '6.0']``
  81. - `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ with config:
  82. ``['target' => '6.0']``
  83. - `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ with config:
  84. ``['target' => '6.0']``
  85. - `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config:
  86. ``['target' => '6.0']``
  87. - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config:
  88. ``['target' => '6.0']``
  89. References
  90. ----------
  91. - Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitNamespacedFixer <./../../../src/Fixer/PhpUnit/PhpUnitNamespacedFixer.php>`_
  92. - Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitNamespacedFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitNamespacedFixerTest.php>`_
  93. The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.