Skip to content

Commit 96bb4d4

Browse files
authored
Fixes mocking facades when deprecations happen (#41057)
1 parent ff9ca06 commit 96bb4d4

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public function handleError($level, $message, $file = '', $line = 0, $context =
8686
*/
8787
public function handleDeprecation($message, $file, $line)
8888
{
89-
if (! class_exists(LogManager::class)) {
89+
if (! class_exists(LogManager::class)
90+
|| ! $this->app->hasBeenBootstrapped()
91+
|| $this->app->runningUnitTests()
92+
) {
9093
return;
9194
}
9295

tests/Foundation/Bootstrap/HandleExceptionsTest.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use ErrorException;
66
use Illuminate\Config\Repository as Config;
7-
use Illuminate\Container\Container;
7+
use Illuminate\Foundation\Application;
88
use Illuminate\Foundation\Bootstrap\HandleExceptions;
99
use Illuminate\Log\LogManager;
1010
use Mockery as m;
@@ -16,11 +16,11 @@ class HandleExceptionsTest extends TestCase
1616
{
1717
protected function setUp(): void
1818
{
19-
$this->container = Container::setInstance(new Container);
19+
$this->app = Application::setInstance(new Application);
2020

2121
$this->config = new Config();
2222

23-
$this->container->singleton('config', function () {
23+
$this->app->singleton('config', function () {
2424
return $this->config;
2525
});
2626

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

3232
$property->setValue(
3333
$this->handleExceptions,
34-
$this->container
34+
tap(m::mock($this->app), function ($app) {
35+
$app->shouldReceive('runningUnitTests')->andReturn(false);
36+
$app->shouldReceive('hasBeenBootstrapped')->andReturn(true);
37+
})
3538
);
3639
});
3740
}
3841

3942
protected function tearDown(): void
4043
{
41-
Container::setInstance(null);
44+
Application::setInstance(null);
4245
}
4346

4447
public function testPhpDeprecations()
4548
{
4649
$logger = m::mock(LogManager::class);
47-
$this->container->instance(LogManager::class, $logger);
50+
$this->app->instance(LogManager::class, $logger);
51+
4852
$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
4953
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
5054
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
@@ -63,7 +67,8 @@ public function testPhpDeprecations()
6367
public function testUserDeprecations()
6468
{
6569
$logger = m::mock(LogManager::class);
66-
$this->container->instance(LogManager::class, $logger);
70+
$this->app->instance(LogManager::class, $logger);
71+
6772
$logger->shouldReceive('channel')->with('deprecations')->andReturnSelf();
6873
$logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s',
6974
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated',
@@ -82,7 +87,8 @@ public function testUserDeprecations()
8287
public function testErrors()
8388
{
8489
$logger = m::mock(LogManager::class);
85-
$this->container->instance(LogManager::class, $logger);
90+
$this->app->instance(LogManager::class, $logger);
91+
8692
$logger->shouldNotReceive('channel');
8793
$logger->shouldNotReceive('warning');
8894

@@ -100,7 +106,8 @@ public function testErrors()
100106
public function testEnsuresDeprecationsDriver()
101107
{
102108
$logger = m::mock(LogManager::class);
103-
$this->container->instance(LogManager::class, $logger);
109+
$this->app->instance(LogManager::class, $logger);
110+
104111
$logger->shouldReceive('channel')->andReturnSelf();
105112
$logger->shouldReceive('warning');
106113

@@ -131,7 +138,8 @@ public function testEnsuresDeprecationsDriver()
131138
public function testEnsuresNullDeprecationsDriver()
132139
{
133140
$logger = m::mock(LogManager::class);
134-
$this->container->instance(LogManager::class, $logger);
141+
$this->app->instance(LogManager::class, $logger);
142+
135143
$logger->shouldReceive('channel')->andReturnSelf();
136144
$logger->shouldReceive('warning');
137145

@@ -151,7 +159,8 @@ public function testEnsuresNullDeprecationsDriver()
151159
public function testEnsuresNullLogDriver()
152160
{
153161
$logger = m::mock(LogManager::class);
154-
$this->container->instance(LogManager::class, $logger);
162+
$this->app->instance(LogManager::class, $logger);
163+
155164
$logger->shouldReceive('channel')->andReturnSelf();
156165
$logger->shouldReceive('warning');
157166

@@ -171,7 +180,8 @@ public function testEnsuresNullLogDriver()
171180
public function testDoNotOverrideExistingNullLogDriver()
172181
{
173182
$logger = m::mock(LogManager::class);
174-
$this->container->instance(LogManager::class, $logger);
183+
$this->app->instance(LogManager::class, $logger);
184+
175185
$logger->shouldReceive('channel')->andReturnSelf();
176186
$logger->shouldReceive('warning');
177187

0 commit comments

Comments
 (0)