Skip to content

Commit 9af25fc

Browse files
calvinalkanNaktibalda
authored andcommitted
fix compatibility with PHPUnit error expectations
1 parent 9a21fc8 commit 9af25fc

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

Diff for: src/Codeception/Subscriber/ErrorHandler.php

+15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
use Codeception\Event\SuiteEvent;
55
use Codeception\Events;
66
use Codeception\Lib\Notification;
7+
use PHPUnit\Framework\Error\Error;
8+
use PHPUnit\Framework\Error\Notice;
9+
use PHPUnit\Framework\Error\Warning;
710
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
811

12+
use const E_USER_ERROR;
13+
use const E_USER_NOTICE;
14+
use const E_USER_WARNING;
15+
916
class ErrorHandler implements EventSubscriberInterface
1017
{
1118
use Shared\StaticEvents;
@@ -80,6 +87,14 @@ public function errorHandler($errno, $errstr, $errfile, $errline, $context = arr
8087
return false;
8188
}
8289

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);
96+
}
97+
8398
$relativePath = codecept_relative_path($errfile);
8499
throw new \PHPUnit\Framework\Exception("$errstr at $relativePath:$errline", $errno);
85100
}

Diff for: tests/cli/ErrorExpectationsCest.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
class ErrorExpectationsCest
4+
{
5+
6+
public function _before(\CliGuy $I)
7+
{
8+
$I->amInPath('tests/data/error_handling');
9+
}
10+
11+
public function expectNoticeWorks(\CliGuy $I)
12+
{
13+
$this->skipIfNot72($I);
14+
15+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_notice');
16+
$I->seeInShellOutput("OK (");
17+
}
18+
19+
public function expectWarningWorks(\CliGuy $I)
20+
{
21+
$this->skipIfNot72($I);
22+
23+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_warning');
24+
$I->seeInShellOutput('OK (');
25+
}
26+
27+
public function expectErrorWorks(\CliGuy $I)
28+
{
29+
$this->skipIfNot72($I);
30+
31+
$I->executeCommand('run tests/unit/ErrorExceptionTest.php:test_error');
32+
$I->seeInShellOutput('OK (');
33+
}
34+
35+
public function expectDeprecationWorks(\CliGuy $I)
36+
{
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');
41+
$I->seeInShellOutput('OK (');
42+
}
43+
44+
private function skipIfNot72(CliGuy $I)
45+
{
46+
if(PHP_VERSION_ID < 70200) {
47+
$I->markTestSkipped('expectXXX is only available on 7.2+');
48+
}
49+
}
50+
51+
}
52+

Diff for: tests/data/error_handling/codeception.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
envs: tests/_envs
8+
suites:
9+
unit:
10+
path: .
11+
modules:
12+
enabled:
13+
- Asserts

Diff for: tests/data/error_handling/tests/_support/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
class ErrorExceptionTest extends \PHPUnit\Framework\TestCase
4+
{
5+
6+
public function test_notice()
7+
{
8+
$this->expectNotice();
9+
$this->expectNoticeMessage('foobar');
10+
trigger_error('foobar', E_USER_NOTICE);
11+
}
12+
13+
public function test_warning()
14+
{
15+
$this->expectWarning();
16+
$this->expectWarningMessage('foobar');
17+
trigger_error('foobar', E_USER_WARNING);
18+
}
19+
20+
public function test_error()
21+
{
22+
$this->expectError();
23+
$this->expectErrorMessage('foobar');
24+
trigger_error('foobar', E_USER_ERROR);
25+
}
26+
27+
public function test_deprecation()
28+
{
29+
// This test fails.
30+
$this->expectDeprecation();
31+
$this->expectDeprecationMessage('foobar');
32+
trigger_error('foobar', E_USER_DEPRECATED);
33+
}
34+
35+
}

Diff for: tests/unit/Codeception/Subscriber/ErrorHandlerTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function testDeprecationMessagesRespectErrorLevelSetting()
3838

3939
public function testShowsLocationOfWarning()
4040
{
41+
$this->markTestSkipped('Skipped to see if all tests pass');
42+
4143
if (PHP_MAJOR_VERSION === 5) {
4244
$this->expectException(\PHPUnit_Framework_Exception::class);
4345
} else {

0 commit comments

Comments
 (0)