Usages of ->setExpectedException*
methods MUST be replaced by
->expectException*
methods.
Risky when PHPUnit classes are overridden or not accessible, or when project has PHPUnit incompatibilities.
Target version of PHPUnit.
Allowed values: '5.2'
, '5.6'
, '8.4'
and 'newest'
Default value: 'newest'
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();
}
}
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();
}
}
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();
}
}
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();
}
}
The rule is part of the following rule sets:
@PHPUnit52Migration:risky with config:
['target' => '5.2']
@PHPUnit54Migration:risky with config:
['target' => '5.2']
@PHPUnit55Migration:risky with config:
['target' => '5.2']
@PHPUnit56Migration:risky with config:
['target' => '5.6']
@PHPUnit57Migration:risky with config:
['target' => '5.6']
@PHPUnit60Migration:risky with config:
['target' => '5.6']
@PHPUnit75Migration:risky with config:
['target' => '5.6']
@PHPUnit84Migration:risky with config:
['target' => '8.4']
@PHPUnit100Migration:risky with config:
['target' => '8.4']