Skip to content

Commit

Permalink
Override Illuminate\Foundation\Bootstrap\HandleExceptions (#92)
Browse files Browse the repository at this point in the history
* Override `Illuminate\Foundation\Bootstrap\HandleExceptions`

Framework PR: laravel/framework#45299

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone authored Dec 14, 2022
1 parent dda9c32 commit 51866b3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@
<php>
<server name="APP_ENV" value="testing"/>
<server name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<server name="LOG_DEPRECATIONS_CHANNEL" value="stderr"/>
<server name="SEND_DEPRECATED_NOTICES_TO_RAY" value="(true)"/>
</php>
</phpunit>
51 changes: 51 additions & 0 deletions src/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Orchestra\Testbench\Bootstrap;

use ErrorException;
use Exception;
use Illuminate\Log\LogManager;

final class HandleExceptions extends \Illuminate\Foundation\Bootstrap\HandleExceptions
{
/**
* Reports a deprecation to the "deprecations" logger.
*
* @param string $message
* @param string $file
* @param int $line
* @param int $level
* @return void
*/
public function handleDeprecationError($message, $file, $line, $level = E_DEPRECATED)
{
if (! class_exists(LogManager::class)
|| ! static::$app->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
));
}
});
}
}
8 changes: 7 additions & 1 deletion src/Concerns/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions tests/PhpDeprecationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Orchestra\Testbench\Tests;

use Illuminate\Support\Facades\Log;
use Orchestra\Testbench\TestCase;

class PhpDeprecationsTest extends TestCase
{
/** @test */
public function handle_php81_deprecations()
{
Log::shouldReceive('channel')
->once()->with('deprecations')
->andReturnSelf()
->shouldReceive('warning')
->once()
->withArgs(function ($message) {
return strpos($message, 'zzz in') !== false;
});

trigger_error('zzz', E_USER_DEPRECATED);
}
}

0 comments on commit 51866b3

Please sign in to comment.