diff --git a/phpunit.xml b/phpunit.xml index 78c2802f7..266028ce0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -34,5 +34,7 @@ + + diff --git a/src/Bootstrap/HandleExceptions.php b/src/Bootstrap/HandleExceptions.php new file mode 100644 index 000000000..0d11f455b --- /dev/null +++ b/src/Bootstrap/HandleExceptions.php @@ -0,0 +1,51 @@ +hasBeenBootstrapped() + ) { + return; + } + + try { + $logger = static::$app->make(LogManager::class); + } catch (Exception $e) { + return; + } + + $this->ensureDeprecationLoggerIsConfigured(); + + /** @phpstan-ignore-next-line */ + $options = static::$app['config']->get('logging.deprecations') ?? []; + + with($logger->channel('deprecations'), function ($log) use ($message, $file, $line, $level, $options) { + $error = new ErrorException($message, 0, $level, $file, $line); + + if ($options['trace'] ?? false) { + $log->warning((string) $error); + } else { + $log->warning(sprintf('%s in %s on line %s', + $message, $file, $line + )); + } + }); + } +} diff --git a/src/Concerns/CreatesApplication.php b/src/Concerns/CreatesApplication.php index 6786fc85b..362b101f9 100644 --- a/src/Concerns/CreatesApplication.php +++ b/src/Concerns/CreatesApplication.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\RateLimiter; use Orchestra\Testbench\Foundation\PackageManifest; +use PHPUnit\Framework\TestCase as PHPUnitTestCase; /** * @property bool|null $enablesPackageDiscoveries @@ -319,7 +320,12 @@ protected function resolveApplicationExceptionHandler($app) */ protected function resolveApplicationBootstrappers($app) { - $app->make('Illuminate\Foundation\Bootstrap\HandleExceptions')->bootstrap($app); + $app->make( + $this instanceof PHPUnitTestCase + ? 'Orchestra\Testbench\Bootstrap\HandleExceptions' + : 'Illuminate\Foundation\Bootstrap\HandleExceptions' + )->bootstrap($app); + $app->make('Illuminate\Foundation\Bootstrap\RegisterFacades')->bootstrap($app); $app->make('Illuminate\Foundation\Bootstrap\SetRequestForConsole')->bootstrap($app); $app->make('Illuminate\Foundation\Bootstrap\RegisterProviders')->bootstrap($app); diff --git a/tests/PhpDeprecationsTest.php b/tests/PhpDeprecationsTest.php new file mode 100644 index 000000000..c1c13f019 --- /dev/null +++ b/tests/PhpDeprecationsTest.php @@ -0,0 +1,24 @@ +once()->with('deprecations') + ->andReturnSelf() + ->shouldReceive('warning') + ->once() + ->withArgs(function ($message) { + return strpos($message, 'zzz in') !== false; + }); + + trigger_error('zzz', E_USER_DEPRECATED); + } +}