From e7e08701bd865c21bc8445c0f2d3477dd434d233 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 16 Feb 2022 16:11:18 +0000 Subject: [PATCH] Fixes mocking facades when deprecations happen --- .../Foundation/Bootstrap/HandleExceptions.php | 5 ++- .../Bootstrap/HandleExceptionsTest.php | 34 ++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php b/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php index df059ab5b528..b631619043fe 100644 --- a/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php +++ b/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php @@ -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; } diff --git a/tests/Foundation/Bootstrap/HandleExceptionsTest.php b/tests/Foundation/Bootstrap/HandleExceptionsTest.php index 6bb1ee150bda..102827613c18 100644 --- a/tests/Foundation/Bootstrap/HandleExceptionsTest.php +++ b/tests/Foundation/Bootstrap/HandleExceptionsTest.php @@ -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; @@ -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; }); @@ -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', @@ -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', @@ -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'); @@ -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'); @@ -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'); @@ -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'); @@ -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');