php_unit_expectation.rst 4.6 KB

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