Skip to content

Commit 8a365d2

Browse files
committed
Finished support for PHPUnit error expectations
1 parent 9af25fc commit 8a365d2

File tree

6 files changed

+60
-42
lines changed

6 files changed

+60
-42
lines changed

src/Codeception/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class Configuration
133133
'config' => [],
134134
],
135135
'error_level' => 'E_ALL & ~E_STRICT & ~E_DEPRECATED',
136+
'convert_deprecations_to_exceptions' => false
136137
];
137138

138139
protected static $params;

src/Codeception/Subscriber/ErrorHandler.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
use Codeception\Event\SuiteEvent;
55
use Codeception\Events;
66
use Codeception\Lib\Notification;
7+
use PHPUnit\Framework\Error\Deprecated;
78
use PHPUnit\Framework\Error\Error;
89
use PHPUnit\Framework\Error\Notice;
910
use PHPUnit\Framework\Error\Warning;
1011
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1112

12-
use const E_USER_ERROR;
13-
use const E_USER_NOTICE;
14-
use const E_USER_WARNING;
15-
1613
class ErrorHandler implements EventSubscriberInterface
1714
{
1815
use Shared\StaticEvents;
@@ -42,6 +39,8 @@ class ErrorHandler implements EventSubscriberInterface
4239
*/
4340
private $errorLevel;
4441

42+
private $convertDeprecationsToExceptions = false;
43+
4544
public function __construct()
4645
{
4746
$this->errorLevel = E_ALL & ~E_STRICT & ~E_DEPRECATED;
@@ -60,6 +59,10 @@ public function handle(SuiteEvent $e)
6059
}
6160
error_reporting($this->errorLevel);
6261

62+
if ($settings['convert_deprecations_to_exceptions']) {
63+
$this->convertDeprecationsToExceptions = true;
64+
}
65+
6366
if ($this->initialized) {
6467
return;
6568
}
@@ -73,7 +76,7 @@ public function handle(SuiteEvent $e)
7376

7477
public function errorHandler($errno, $errstr, $errfile, $errline, $context = array())
7578
{
76-
if (E_USER_DEPRECATED === $errno) {
79+
if ((E_USER_DEPRECATED === $errno || E_DEPRECATED === $errno) && !$this->convertDeprecationsToExceptions) {
7780
$this->handleDeprecationError($errno, $errstr, $errfile, $errline, $context);
7881
return;
7982
}
@@ -87,12 +90,23 @@ public function errorHandler($errno, $errstr, $errfile, $errline, $context = arr
8790
return false;
8891
}
8992

90-
if($errno === E_USER_NOTICE) {
91-
throw new Notice($errstr, $errno, $errfile, $errline);
92-
}elseif ($errno === E_USER_WARNING) {
93-
throw new Warning($errstr, $errno, $errfile, $errline);
94-
}elseif ($errno === E_USER_ERROR) {
95-
throw new Error($errstr, $errno, $errfile, $errline);
93+
if (version_compare(\PHPUnit\Runner\Version::id(), '8.4.0', '>=')) {
94+
switch ($errno) {
95+
case E_DEPRECATED:
96+
case E_USER_DEPRECATED:
97+
// Renamed to Deprecation in PHPUnit 10
98+
throw new Deprecated($errstr, $errno, $errfile, $errline);
99+
case E_NOTICE:
100+
case E_STRICT:
101+
case E_USER_NOTICE:
102+
throw new Notice($errstr, $errno, $errfile, $errline);
103+
case E_WARNING:
104+
case E_USER_WARNING:
105+
throw new Warning($errstr, $errno, $errfile, $errline);
106+
case E_USER_ERROR:
107+
default:
108+
throw new Error($errstr, $errno, $errfile, $errline);
109+
}
96110
}
97111

98112
$relativePath = codecept_relative_path($errfile);

tests/cli/ErrorExpectationsCest.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,40 @@ public function _before(\CliGuy $I)
1010

1111
public function expectNoticeWorks(\CliGuy $I)
1212
{
13-
$this->skipIfNot72($I);
13+
$this->skipIfOldPhpUnit($I);
1414

15-
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_notice');
15+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:testNotice');
1616
$I->seeInShellOutput("OK (");
1717
}
1818

1919
public function expectWarningWorks(\CliGuy $I)
2020
{
21-
$this->skipIfNot72($I);
21+
$this->skipIfOldPhpUnit($I);
2222

23-
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_warning');
23+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:testWarning');
2424
$I->seeInShellOutput('OK (');
2525
}
2626

2727
public function expectErrorWorks(\CliGuy $I)
2828
{
29-
$this->skipIfNot72($I);
29+
$this->skipIfOldPhpUnit($I);
3030

31-
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_error');
31+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:testError');
3232
$I->seeInShellOutput('OK (');
3333
}
3434

3535
public function expectDeprecationWorks(\CliGuy $I)
3636
{
37-
$this->skipIfNot72($I);
38-
$I->markTestSkipped('This test is just to reproduce that is doesnt work. It will fail because nothing has been implemented');
39-
40-
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_deprecation');
37+
$this->skipIfOldPhpUnit($I);
38+
39+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:testDeprecation');
4140
$I->seeInShellOutput('OK (');
4241
}
4342

44-
private function skipIfNot72(CliGuy $I)
43+
private function skipIfOldPhpUnit(CliGuy $I)
4544
{
46-
if(PHP_VERSION_ID < 70200) {
47-
$I->markTestSkipped('expectXXX is only available on 7.2+');
45+
if (version_compare(\PHPUnit\Runner\Version::id(), '8.4.0', '<')) {
46+
$I->markTestSkipped('expectXXX is only available on PHPUnit 8.4+');
4847
}
4948
}
50-
5149
}
52-

tests/data/error_handling/codeception.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ suites:
1010
path: .
1111
modules:
1212
enabled:
13-
- Asserts
13+
- Asserts
14+
settings:
15+
convert_deprecations_to_exceptions: true

tests/data/error_handling/tests/unit/ErrorExceptionTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@
33
class ErrorExceptionTest extends \PHPUnit\Framework\TestCase
44
{
55

6-
public function test_notice()
6+
public function testNotice()
77
{
88
$this->expectNotice();
99
$this->expectNoticeMessage('foobar');
1010
trigger_error('foobar', E_USER_NOTICE);
1111
}
12-
13-
public function test_warning()
12+
13+
public function testWarning()
1414
{
1515
$this->expectWarning();
1616
$this->expectWarningMessage('foobar');
1717
trigger_error('foobar', E_USER_WARNING);
1818
}
19-
20-
public function test_error()
19+
20+
public function testError()
2121
{
2222
$this->expectError();
2323
$this->expectErrorMessage('foobar');
2424
trigger_error('foobar', E_USER_ERROR);
2525
}
26-
27-
public function test_deprecation()
26+
27+
public function testDeprecation()
2828
{
2929
// This test fails.
3030
$this->expectDeprecation();
3131
$this->expectDeprecationMessage('foobar');
3232
trigger_error('foobar', E_USER_DEPRECATED);
3333
}
34-
35-
}
34+
}

tests/unit/Codeception/Subscriber/ErrorHandlerTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@ public function testDeprecationMessagesRespectErrorLevelSetting()
3838

3939
public function testShowsLocationOfWarning()
4040
{
41-
$this->markTestSkipped('Skipped to see if all tests pass');
42-
43-
if (PHP_MAJOR_VERSION === 5) {
41+
if (version_compare(\PHPUnit\Runner\Version::id(), '8.4.0', '>=')) {
42+
$this->expectWarning();
43+
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '>=')) {
44+
$this->expectException(\PHPUnit\Framework\Exception::class);
45+
} else {
4446
$this->expectException(\PHPUnit_Framework_Exception::class);
47+
}
48+
49+
if (version_compare(\PHPUnit\Runner\Version::id(), '8.4.0', '>=')) {
50+
$this->expectWarningMessage('Undefined variable: file');
4551
} else {
46-
$this->expectException(\PHPUnit\Framework\Exception::class);
52+
$SEP = DIRECTORY_SEPARATOR;
53+
$this->expectExceptionMessage($expectedMessage = "Undefined variable: file at tests{$SEP}unit{$SEP}Codeception{$SEP}Subscriber{$SEP}ErrorHandlerTest.php:55");
4754
}
48-
$SEP = DIRECTORY_SEPARATOR;
49-
$this->expectExceptionMessage("Undefined variable: file at tests{$SEP}unit{$SEP}Codeception{$SEP}Subscriber{$SEP}ErrorHandlerTest.php:48");
5055
trigger_error('Undefined variable: file', E_USER_WARNING);
5156
}
5257
}

0 commit comments

Comments
 (0)