Skip to content

Commit 05512a4

Browse files
authored
Merge pull request Codeception#6094 from Codeception/4.1-issue6090
Show origin of warning in error message
2 parents 3579d06 + 49254ca commit 05512a4

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Codeception/Subscriber/ErrorHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public function errorHandler($errno, $errstr, $errfile, $errline, $context = arr
8080
return false;
8181
}
8282

83-
throw new \PHPUnit\Framework\Exception($errstr, $errno);
83+
$relativePath = codecept_relative_path($errfile);
84+
throw new \PHPUnit\Framework\Exception("$errstr at $relativePath:$errline", $errno);
8485
}
8586

8687
public function shutdownHandler()

tests/unit/Codeception/Subscriber/ErrorHandlerTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@
55
use Codeception\Subscriber\ErrorHandler;
66
use Codeception\Suite;
77

8-
class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
8+
class ErrorHandlerTest extends \Codeception\PHPUnit\TestCase
99
{
10+
private $originalErrorLevel;
11+
12+
public function _setUp()
13+
{
14+
$this->originalErrorLevel = error_reporting();
15+
}
16+
17+
public function _tearDown()
18+
{
19+
// Deprecation message test changes error_level
20+
error_reporting($this->originalErrorLevel);
21+
}
1022

1123
public function testDeprecationMessagesRespectErrorLevelSetting()
1224
{
@@ -23,4 +35,16 @@ public function testDeprecationMessagesRespectErrorLevelSetting()
2335

2436
$this->assertEquals([], Notification::all(), 'Deprecation message was added to notifications');
2537
}
38+
39+
public function testShowsLocationOfWarning()
40+
{
41+
if (PHP_MAJOR_VERSION === 5) {
42+
$this->expectException(\PHPUnit_Framework_Exception::class);
43+
} else {
44+
$this->expectException(\PHPUnit\Framework\Exception::class);
45+
}
46+
$SEP = DIRECTORY_SEPARATOR;
47+
$this->expectExceptionMessage("Undefined variable: file at tests{$SEP}unit{$SEP}Codeception{$SEP}Subscriber{$SEP}ErrorHandlerTest.php:48");
48+
trigger_error('Undefined variable: file', E_USER_WARNING);
49+
}
2650
}

0 commit comments

Comments
 (0)