Skip to content

Commit

Permalink
Fixes mocking facades when deprecations happen (#41057)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Feb 16, 2022
1 parent ff9ca06 commit 96bb4d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
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

0 comments on commit 96bb4d4

Please sign in to comment.