php_unit_no_expectation_annotation.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ===========================================
  2. Rule ``php_unit_no_expectation_annotation``
  3. ===========================================
  4. Usages of ``@expectedException*`` annotations MUST be replaced by
  5. ``->setExpectedException*`` methods.
  6. Warning
  7. -------
  8. Using this rule is risky
  9. ~~~~~~~~~~~~~~~~~~~~~~~~
  10. Risky when PHPUnit classes are overridden or not accessible, or when project has
  11. PHPUnit incompatibilities.
  12. Configuration
  13. -------------
  14. ``target``
  15. ~~~~~~~~~~
  16. Target version of PHPUnit.
  17. Allowed values: ``'3.2'``, ``'4.3'`` and ``'newest'``
  18. Default value: ``'newest'``
  19. ``use_class_const``
  20. ~~~~~~~~~~~~~~~~~~~
  21. Use ::class notation.
  22. Allowed types: ``bool``
  23. Default value: ``true``
  24. Examples
  25. --------
  26. Example #1
  27. ~~~~~~~~~~
  28. *Default* configuration.
  29. .. code-block:: diff
  30. --- Original
  31. +++ New
  32. <?php
  33. final class MyTest extends \PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. - * @expectedException FooException
  37. - * @expectedExceptionMessageRegExp /foo.*$/
  38. - * @expectedExceptionCode 123
  39. */
  40. function testAaa()
  41. {
  42. + $this->setExpectedExceptionRegExp(\FooException::class, '/foo.*$/', 123);
  43. +
  44. aaa();
  45. }
  46. }
  47. Example #2
  48. ~~~~~~~~~~
  49. With configuration: ``['target' => '3.2']``.
  50. .. code-block:: diff
  51. --- Original
  52. +++ New
  53. <?php
  54. final class MyTest extends \PHPUnit_Framework_TestCase
  55. {
  56. /**
  57. - * @expectedException FooException
  58. - * @expectedExceptionCode 123
  59. */
  60. function testBbb()
  61. {
  62. + $this->setExpectedException(\FooException::class, null, 123);
  63. +
  64. bbb();
  65. }
  66. /**
  67. * @expectedException FooException
  68. * @expectedExceptionMessageRegExp /foo.*$/
  69. */
  70. function testCcc()
  71. {
  72. ccc();
  73. }
  74. }
  75. Rule sets
  76. ---------
  77. The rule is part of the following rule sets:
  78. - `@PHPUnit32Migration:risky <./../../ruleSets/PHPUnit32MigrationRisky.rst>`_ with config:
  79. ``['target' => '3.2']``
  80. - `@PHPUnit35Migration:risky <./../../ruleSets/PHPUnit35MigrationRisky.rst>`_ with config:
  81. ``['target' => '3.2']``
  82. - `@PHPUnit43Migration:risky <./../../ruleSets/PHPUnit43MigrationRisky.rst>`_ with config:
  83. ``['target' => '4.3']``
  84. - `@PHPUnit48Migration:risky <./../../ruleSets/PHPUnit48MigrationRisky.rst>`_ with config:
  85. ``['target' => '4.3']``
  86. - `@PHPUnit50Migration:risky <./../../ruleSets/PHPUnit50MigrationRisky.rst>`_ with config:
  87. ``['target' => '4.3']``
  88. - `@PHPUnit52Migration:risky <./../../ruleSets/PHPUnit52MigrationRisky.rst>`_ with config:
  89. ``['target' => '4.3']``
  90. - `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ with config:
  91. ``['target' => '4.3']``
  92. - `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ with config:
  93. ``['target' => '4.3']``
  94. - `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ with config:
  95. ``['target' => '4.3']``
  96. - `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ with config:
  97. ``['target' => '4.3']``
  98. - `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ with config:
  99. ``['target' => '4.3']``
  100. - `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ with config:
  101. ``['target' => '4.3']``
  102. - `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ with config:
  103. ``['target' => '4.3']``
  104. - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config:
  105. ``['target' => '4.3']``