Fork me on GitHub
a Sensio Labs Product

Exceptional Exception (v3.92.1) edition

Rule php_unit_expectation

Usages of ->setExpectedException* methods MUST be replaced by ->expectException* methods.

Warnings

This rule is RISKY

Risky when PHPUnit classes are overridden or not accessible, or when project has PHPUnit incompatibilities.

This rule is CONFIGURABLE

You can configure this rule using the following option: target.

Configuration

target

Target version of PHPUnit.

Allowed values: '5.2', '5.6', '8.4' and 'newest'

Default value: 'newest'

Examples

Example #1

Default configuration.

--- Original
+++ New
 <?php
 final class MyTest extends \PHPUnit_Framework_TestCase
 {
     public function testFoo()
     {
-        $this->setExpectedException("RuntimeException", "Msg", 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionMessage("Msg");
+        $this->expectExceptionCode(123);
         foo();
     }

     public function testBar()
     {
-        $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionMessageMatches("/Msg.*/");
+        $this->expectExceptionCode(123);
         bar();
     }
 }

Example #2

With configuration: ['target' => '8.4'].

--- Original
+++ New
 <?php
 final class MyTest extends \PHPUnit_Framework_TestCase
 {
     public function testFoo()
     {
-        $this->setExpectedException("RuntimeException", null, 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionCode(123);
         foo();
     }

     public function testBar()
     {
-        $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionMessageMatches("/Msg.*/");
+        $this->expectExceptionCode(123);
         bar();
     }
 }

Example #3

With configuration: ['target' => '5.6'].

--- Original
+++ New
 <?php
 final class MyTest extends \PHPUnit_Framework_TestCase
 {
     public function testFoo()
     {
-        $this->setExpectedException("RuntimeException", null, 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionCode(123);
         foo();
     }

     public function testBar()
     {
-        $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionMessageRegExp("/Msg.*/");
+        $this->expectExceptionCode(123);
         bar();
     }
 }

Example #4

With configuration: ['target' => '5.2'].

--- Original
+++ New
 <?php
 final class MyTest extends \PHPUnit_Framework_TestCase
 {
     public function testFoo()
     {
-        $this->setExpectedException("RuntimeException", "Msg", 123);
+        $this->expectException("RuntimeException");
+        $this->expectExceptionMessage("Msg");
+        $this->expectExceptionCode(123);
         foo();
     }

     public function testBar()
     {
         $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
         bar();
     }
 }

Rule sets

The rule is part of the following rule sets:

References

The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.