Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Tracer/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function setCliOutput(OutputInterface $output): void
}

/**
* Turns a reported PHP error into an exception. A deprecation only warns about a future PHP version,
* often from code that is not ours, so it is logged and the script goes on.
* @throws ErrorException
*/
public function handleError(int $severity, string $message, string $file, int $line): bool
Expand All @@ -71,6 +73,14 @@ public function handleError(int $severity, string $message, string $file, int $l
return false;
}

if ($severity === E_DEPRECATED || $severity === E_USER_DEPRECATED) {
if ($this->logger !== null) {
$this->logger->warning($message . ' in ' . $file . ':' . $line);
}

return true;
}

throw new ErrorException($message, 0, $severity, $file, $line);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Database/Adapters/Idiorm/IdiormDbalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Quantum\Tests\Unit\Database\Adapters\Idiorm;

use Quantum\Database\Adapters\Idiorm\IdiormDbal;
use Quantum\Tracer\ErrorHandler;
use Quantum\App\Enums\AppType;

class IdiormDbalTest extends IdiormDbalTestCase
Expand Down Expand Up @@ -51,6 +52,29 @@ public function testIdiormLogsNoQueriesWhenDebugIsTheTextFalseFromEnv(): void
$this->assertFalse($this->getBaseConfig()['logging']);
}

public function testIdiormRunsAQueryWithANullParameterWhileDeprecationsAreReported(): void
{
IdiormDbal::execute('CREATE TABLE nullable_values (id INTEGER PRIMARY KEY, name TEXT)');

(new ErrorHandler())->setup(logger());

$oldLevel = error_reporting(E_ALL);

try {
IdiormDbal::execute('INSERT INTO nullable_values (name) VALUES (?)', [null]);

$this->assertSame([['name' => null]], IdiormDbal::query('SELECT name FROM nullable_values'));

$warnings = array_column(logger()->getAdapter()->getMessages(), 'warning');

$this->assertNotEmpty(array_filter($warnings, fn ($message) => str_contains((string) $message, 'PDO::quote()')));
} finally {
error_reporting($oldLevel);
restore_error_handler();
restore_exception_handler();
}
}

/**
* @return array<string, mixed>
*/
Expand Down
80 changes: 80 additions & 0 deletions tests/Unit/Tracer/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,55 @@ public function testHandleErrorReturnsFalseForSuppressedErrors(): void
}
}

/**
* @dataProvider deprecationSeverityProvider
*/
public function testHandleErrorLogsADeprecationInsteadOfThrowing(int $severity): void
{
$logged = $this->captureWarnings();

$oldLevel = error_reporting(E_ALL);

try {
$result = $this->errorHandler->handleError($severity, 'Feature is deprecated', '/app/file.php', 12);

$this->assertTrue($result);
$this->assertSame(['Feature is deprecated in /app/file.php:12'], $logged());
} finally {
error_reporting($oldLevel);
}
}

/**
* @dataProvider deprecationSeverityProvider
*/
public function testHandleErrorIgnoresADeprecationWhenThereIsNoLogger(int $severity): void
{
$this->setPrivateProperty($this->errorHandler, 'logger', null);

$oldLevel = error_reporting(E_ALL);

try {
$this->assertTrue($this->errorHandler->handleError($severity, 'Feature is deprecated', __FILE__, __LINE__));
} finally {
error_reporting($oldLevel);
}
}

public function testHandleErrorLeavesAnUnreportedDeprecationToPhp(): void
{
$logged = $this->captureWarnings();

$oldLevel = error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);

try {
$this->assertFalse($this->errorHandler->handleError(E_DEPRECATED, 'Feature is deprecated', __FILE__, __LINE__));
$this->assertSame([], $logged());
} finally {
error_reporting($oldLevel);
}
}

public function testHandleExceptionCliWritesExceptionMessage(): void
{
$this->useAppType(AppType::CONSOLE);
Expand Down Expand Up @@ -218,6 +267,17 @@ public function testLogErrorFallsBackToErrorMethodForUnknownLevel(): void
$this->assertTrue(true);
}

/**
* @return array<string, array{0: int}>
*/
public function deprecationSeverityProvider(): array
{
return [
'deprecated' => [E_DEPRECATED],
'user-deprecated' => [E_USER_DEPRECATED],
];
}

/**
* @dataProvider errorTypeProvider
*/
Expand All @@ -242,4 +302,24 @@ public function errorTypeProvider(): array
];
}

/**
* Gives the handler a logger that records its warnings, and returns a function that lists them
* @return callable(): array<int, string>
*/
private function captureWarnings(): callable
{
$logged = [];

$logger = Mockery::mock(Logger::class);
$logger->shouldReceive('warning')->andReturnUsing(function (string $message) use (&$logged): void {
$logged[] = $message;
});

$this->setPrivateProperty($this->errorHandler, 'logger', $logger);

return function () use (&$logged): array {
return $logged;
};
}

}
Loading