Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up ErrorHandler #3616

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Framework/Error/Deprecated.php
Expand Up @@ -14,5 +14,4 @@
*/
final class Deprecated extends Error
{
public static $enabled = true;
}
13 changes: 11 additions & 2 deletions src/Framework/Error/Error.php
Expand Up @@ -16,11 +16,20 @@
*/
class Error extends Exception
{
public function __construct(string $message, int $code, string $file, int $line, \Exception $previous = null)

private $severity;

public function __construct(string $message, int $code, int $severity, string $file, int $line, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);

$this->severity = $severity;
$this->file = $file;
$this->line = $line;
}

public function getSeverity() {
return $this->severity;
}


}
1 change: 0 additions & 1 deletion src/Framework/Error/Notice.php
Expand Up @@ -14,5 +14,4 @@
*/
final class Notice extends Error
{
public static $enabled = true;
}
1 change: 0 additions & 1 deletion src/Framework/Error/Warning.php
Expand Up @@ -14,5 +14,4 @@
*/
final class Warning extends Error
{
public static $enabled = true;
}
21 changes: 21 additions & 0 deletions src/Framework/TestCase.php
Expand Up @@ -32,6 +32,7 @@
use PHPUnit\Framework\MockObject\Stub\ReturnValueMap as ReturnValueMapStub;
use PHPUnit\Runner\BaseTestRunner;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\Util\ErrorHandler;
use PHPUnit\Util\GlobalState;
use PHPUnit\Util\PHP\AbstractPhpProcess;
use PHPUnit\Util\Test as TestUtil;
Expand Down Expand Up @@ -2295,4 +2296,24 @@ private function isCallableTestMethod(string $dependency): bool

return TestUtil::isTestMethod($method);
}

protected function convertErrorsToExceptions(bool $convert = true): void
{
$this->setUseErrorHandler($convert);
}

protected function convertDeprecationsToExceptions(bool $convert = true) : void
{
ErrorHandler::getInstance()->setConvertDeprecated($convert);
}

protected function convertNoticesToExceptions(bool $convert = true) : void
{
ErrorHandler::getInstance()->setConvertNotice($convert);
}

protected function convertWarningsToExceptions(bool $convert = true) : void
{
ErrorHandler::getInstance()->setConvertWarning($convert);
}
}
2 changes: 1 addition & 1 deletion src/Framework/TestResult.php
Expand Up @@ -621,7 +621,7 @@ public function run(Test $test): void

if ($this->convertErrorsToExceptions) {
$oldErrorHandler = \set_error_handler(
[ErrorHandler::class, 'handleError'],
[ErrorHandler::getInstance(), 'handleError'],
\E_ALL | \E_STRICT
);

Expand Down
10 changes: 4 additions & 6 deletions src/TextUI/TestRunner.php
Expand Up @@ -9,9 +9,6 @@
*/
namespace PHPUnit\TextUI;

use PHPUnit\Framework\Error\Deprecated;
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\Error\Warning;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
Expand All @@ -36,6 +33,7 @@
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\Runner\Version;
use PHPUnit\Util\Configuration;
use PHPUnit\Util\ErrorHandler;
use PHPUnit\Util\Filesystem;
use PHPUnit\Util\Log\JUnit;
use PHPUnit\Util\Log\TeamCity;
Expand Down Expand Up @@ -219,15 +217,15 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
}

if (!$arguments['convertDeprecationsToExceptions']) {
Deprecated::$enabled = false;
ErrorHandler::getInstance()->setConvertDeprecated(true);
}

if (!$arguments['convertNoticesToExceptions']) {
Notice::$enabled = false;
ErrorHandler::getInstance()->setConvertNotice(true);
}

if (!$arguments['convertWarningsToExceptions']) {
Warning::$enabled = false;
ErrorHandler::getInstance()->setConvertWarning(true);
}

if ($arguments['stopOnError']) {
Expand Down
112 changes: 89 additions & 23 deletions src/Util/ErrorHandler.php
Expand Up @@ -7,6 +7,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPUnit\Util;

use PHPUnit\Framework\Error\Deprecated;
Expand All @@ -19,29 +20,29 @@
*/
final class ErrorHandler
{
private static $errorStack = [];
private $errorStack = [];

/**
* Returns the error stack.
*/
public static function getErrorStack(): array
protected $convertWarning = true;
protected $convertNotice = true;
protected $convertDeprecated = true;


protected function __construct()
{
return self::$errorStack;

}


/**
* @throws \PHPUnit\Framework\Error\Deprecated
* @throws \PHPUnit\Framework\Error\Error
* @throws \PHPUnit\Framework\Error\Notice
* @throws \PHPUnit\Framework\Error\Warning
*/
public static function handleError(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
public function handleError(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
{
if (!($errorNumber & \error_reporting())) {
return false;
}

self::$errorStack[] = [$errorNumber, $errorString, $errorFile, $errorLine];
$this->errorStack[] = [$errorNumber, $errorString, $errorFile, $errorLine];

$trace = \debug_backtrace();
\array_shift($trace);
Expand All @@ -51,30 +52,29 @@ public static function handleError(int $errorNumber, string $errorString, string
return false;
}
}

$exception = Error::class;
if ($errorNumber === \E_NOTICE || $errorNumber === \E_USER_NOTICE || $errorNumber === \E_STRICT) {
if (!Notice::$enabled) {
if(!$this->convertNotice) {
return false;
}

$exception = Notice::class;
} elseif ($errorNumber === \E_WARNING || $errorNumber === \E_USER_WARNING) {
if (!Warning::$enabled) {
}
if ($errorNumber === \E_WARNING || $errorNumber === \E_USER_WARNING) {
if(!$this->convertWarning) {
return false;
}

$exception = Warning::class;
} elseif ($errorNumber === \E_DEPRECATED || $errorNumber === \E_USER_DEPRECATED) {
if (!Deprecated::$enabled) {

}
if ($errorNumber === \E_DEPRECATED || $errorNumber === \E_USER_DEPRECATED) {
if(!$this->convertDeprecated) {
return false;
}

$exception = Deprecated::class;
} else {
$exception = Error::class;

}

throw new $exception($errorString, $errorNumber, $errorFile, $errorLine);
throw new $exception($errorString, 0, $errorNumber, $errorFile, $errorLine);
}

/**
Expand Down Expand Up @@ -109,4 +109,70 @@ function ($errorNumber, $errorString) use ($severity) {

return $terminator;
}

/**
* @param bool $convertError
*/
public function setConvertError(bool $convertError): void
{
$this->convertError = $convertError;
}

/**
* @return bool
*/
public function getConvertWarning(): bool
{
return $this->convertWarning;
}

/**
* @param bool $convertWarning
*/
public function setConvertWarning(bool $convertWarning): void
{
$this->convertWarning = $convertWarning;
}

/**
* @return bool
*/
public function getConvertNotice(): bool
{
return $this->convertNotice;
}

/**
* @param bool $convertNotice
*/
public function setConvertNotice(bool $convertNotice): void
{
$this->convertNotice = $convertNotice;
}

/**
* @return bool
*/
public function getConvertDeprecated(): bool
{
return $this->convertDeprecated;
}

/**
* @param bool $convertDeprecated
*/
public function setConvertDeprecated(bool $convertDeprecated): void
{
$this->convertDeprecated = $convertDeprecated;
}

private static $instace;

public static function getInstance()
{
if (self::$instace === null) {
self::$instace = new self();
}
return self::$instace;
}
}
2 changes: 1 addition & 1 deletion tests/unit/Framework/TestFailureTest.php
Expand Up @@ -76,7 +76,7 @@ public function testExceptionToStringForExpectationFailedExceptionWithComparison

public function testExceptionToStringForFrameworkError(): void
{
$exception = new Error('message', 0, 'file', 1);
$exception = new Error('message', 0, E_WARNING,'file', 1);

$this->assertEquals("message\n", TestFailure::exceptionToString($exception));
}
Expand Down