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

[8.x] Fixes attempt to log deprecations on mocks #41057

Merged
merged 1 commit into from Feb 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Expand Up @@ -86,7 +86,10 @@ public function handleError($level, $message, $file = '', $line = 0, $context =
*/
public function handleDeprecation($message, $file, $line)
{
if (! class_exists(LogManager::class)) {
if (! class_exists(LogManager::class)
|| ! $this->app->hasBeenBootstrapped()
|| $this->app->runningUnitTests()
) {
return;
}

Expand Down
34 changes: 22 additions & 12 deletions tests/Foundation/Bootstrap/HandleExceptionsTest.php
Expand Up @@ -4,7 +4,7 @@

use ErrorException;
use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\HandleExceptions;
use Illuminate\Log\LogManager;
use Mockery as m;
Expand All @@ -16,11 +16,11 @@ class HandleExceptionsTest extends TestCase
{
protected function setUp(): void
{
$this->container = Container::setInstance(new Container);
$this->app = Application::setInstance(new Application);

$this->config = new Config();

$this->container->singleton('config', function () {
$this->app->singleton('config', function () {
return $this->config;
});

Expand All @@ -31,20 +31,24 @@ protected function setUp(): void

$property->setValue(
$this->handleExceptions,
$this->container
tap(m::mock($this->app), function ($app) {
$app->shouldReceive('runningUnitTests')->andReturn(false);
$app->shouldReceive('hasBeenBootstrapped')->andReturn(true);
})
);
});
}

protected function tearDown(): void
{
Container::setInstance(null);
Application::setInstance(null);
}

public function testPhpDeprecations()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
Expand All @@ -63,7 +67,8 @@ public function testPhpDeprecations()
public function testUserDeprecations()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
Expand All @@ -82,7 +87,8 @@ public function testUserDeprecations()
public function testErrors()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldNotReceive('channel');
$logger->shouldNotReceive('warning');

Expand All @@ -100,7 +106,8 @@ public function testErrors()
public function testEnsuresDeprecationsDriver()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->andReturnSelf();
$logger->shouldReceive('warning');

Expand Down Expand Up @@ -131,7 +138,8 @@ public function testEnsuresDeprecationsDriver()
public function testEnsuresNullDeprecationsDriver()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->andReturnSelf();
$logger->shouldReceive('warning');

Expand All @@ -151,7 +159,8 @@ public function testEnsuresNullDeprecationsDriver()
public function testEnsuresNullLogDriver()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->andReturnSelf();
$logger->shouldReceive('warning');

Expand All @@ -171,7 +180,8 @@ public function testEnsuresNullLogDriver()
public function testDoNotOverrideExistingNullLogDriver()
{
$logger = m::mock(LogManager::class);
$this->container->instance(LogManager::class, $logger);
$this->app->instance(LogManager::class, $logger);

$logger->shouldReceive('channel')->andReturnSelf();
$logger->shouldReceive('warning');

Expand Down