php_unit_expectation.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. =============================
  2. Rule ``php_unit_expectation``
  3. =============================
  4. Usages of ``->setExpectedException*`` methods MUST be replaced by
  5. ``->expectException*`` methods.
  6. .. warning:: Using this rule is risky.
  7. Risky when PHPUnit classes are overridden or not accessible, or when project
  8. has PHPUnit incompatibilities.
  9. Configuration
  10. -------------
  11. ``target``
  12. ~~~~~~~~~~
  13. Target version of PHPUnit.
  14. Allowed values: ``'5.2'``, ``'5.6'``, ``'8.4'``, ``'newest'``
  15. Default value: ``'newest'``
  16. Examples
  17. --------
  18. Example #1
  19. ~~~~~~~~~~
  20. *Default* configuration.
  21. .. code-block:: diff
  22. --- Original
  23. +++ New
  24. <?php
  25. final class MyTest extends \PHPUnit_Framework_TestCase
  26. {
  27. public function testFoo()
  28. {
  29. - $this->setExpectedException("RuntimeException", "Msg", 123);
  30. + $this->expectException("RuntimeException");
  31. + $this->expectExceptionMessage("Msg");
  32. + $this->expectExceptionCode(123);
  33. foo();
  34. }
  35. public function testBar()
  36. {
  37. - $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  38. + $this->expectException("RuntimeException");
  39. + $this->expectExceptionMessageMatches("/Msg.*/");
  40. + $this->expectExceptionCode(123);
  41. bar();
  42. }
  43. }
  44. Example #2
  45. ~~~~~~~~~~
  46. With configuration: ``['target' => '8.4']``.
  47. .. code-block:: diff
  48. --- Original
  49. +++ New
  50. <?php
  51. final class MyTest extends \PHPUnit_Framework_TestCase
  52. {
  53. public function testFoo()
  54. {
  55. - $this->setExpectedException("RuntimeException", null, 123);
  56. + $this->expectException("RuntimeException");
  57. + $this->expectExceptionCode(123);
  58. foo();
  59. }
  60. public function testBar()
  61. {
  62. - $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  63. + $this->expectException("RuntimeException");
  64. + $this->expectExceptionMessageMatches("/Msg.*/");
  65. + $this->expectExceptionCode(123);
  66. bar();
  67. }
  68. }
  69. Example #3
  70. ~~~~~~~~~~
  71. With configuration: ``['target' => '5.6']``.
  72. .. code-block:: diff
  73. --- Original
  74. +++ New
  75. <?php
  76. final class MyTest extends \PHPUnit_Framework_TestCase
  77. {
  78. public function testFoo()
  79. {
  80. - $this->setExpectedException("RuntimeException", null, 123);
  81. + $this->expectException("RuntimeException");
  82. + $this->expectExceptionCode(123);
  83. foo();
  84. }
  85. public function testBar()
  86. {
  87. - $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  88. + $this->expectException("RuntimeException");
  89. + $this->expectExceptionMessageRegExp("/Msg.*/");
  90. + $this->expectExceptionCode(123);
  91. bar();
  92. }
  93. }
  94. Example #4
  95. ~~~~~~~~~~
  96. With configuration: ``['target' => '5.2']``.
  97. .. code-block:: diff
  98. --- Original
  99. +++ New
  100. <?php
  101. final class MyTest extends \PHPUnit_Framework_TestCase
  102. {
  103. public function testFoo()
  104. {
  105. - $this->setExpectedException("RuntimeException", "Msg", 123);
  106. + $this->expectException("RuntimeException");
  107. + $this->expectExceptionMessage("Msg");
  108. + $this->expectExceptionCode(123);
  109. foo();
  110. }
  111. public function testBar()
  112. {
  113. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  114. bar();
  115. }
  116. }
  117. Rule sets
  118. ---------
  119. The rule is part of the following rule sets:
  120. @PHPUnit52Migration:risky
  121. Using the `@PHPUnit52Migration:risky <./../../ruleSets/PHPUnit52MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  122. ``['target' => '5.2']``
  123. @PHPUnit54Migration:risky
  124. Using the `@PHPUnit54Migration:risky <./../../ruleSets/PHPUnit54MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  125. ``['target' => '5.2']``
  126. @PHPUnit55Migration:risky
  127. Using the `@PHPUnit55Migration:risky <./../../ruleSets/PHPUnit55MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  128. ``['target' => '5.2']``
  129. @PHPUnit56Migration:risky
  130. Using the `@PHPUnit56Migration:risky <./../../ruleSets/PHPUnit56MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  131. ``['target' => '5.6']``
  132. @PHPUnit57Migration:risky
  133. Using the `@PHPUnit57Migration:risky <./../../ruleSets/PHPUnit57MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  134. ``['target' => '5.6']``
  135. @PHPUnit60Migration:risky
  136. Using the `@PHPUnit60Migration:risky <./../../ruleSets/PHPUnit60MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  137. ``['target' => '5.6']``
  138. @PHPUnit75Migration:risky
  139. Using the `@PHPUnit75Migration:risky <./../../ruleSets/PHPUnit75MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  140. ``['target' => '5.6']``
  141. @PHPUnit84Migration:risky
  142. Using the `@PHPUnit84Migration:risky <./../../ruleSets/PHPUnit84MigrationRisky.rst>`_ rule set will enable the ``php_unit_expectation`` rule with the config below:
  143. ``['target' => '8.4']``