diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index 6e04f572b547..a3f7f0afdeab 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -95,7 +95,7 @@ public function channel($channel = null) */ public function driver($driver = null) { - return $this->get($driver ?? $this->getDefaultDriver()); + return $this->get($this->parseDriver($driver)); } /** @@ -450,7 +450,7 @@ protected function configurationFor($name) /** * Get the default log driver name. * - * @return string + * @return string|null */ public function getDefaultDriver() { @@ -490,13 +490,30 @@ public function extend($driver, Closure $callback) */ public function forgetChannel($driver = null) { - $driver = $driver ?? $this->getDefaultDriver(); + $driver = $this->parseDriver($driver); if (isset($this->channels[$driver])) { unset($this->channels[$driver]); } } + /** + * Parse the driver name. + * + * @param string|null $driver + * @return string|null + */ + protected function parseDriver($driver) + { + $driver = $driver ?? $this->getDefaultDriver(); + + if ($this->app->runningUnitTests()) { + $driver = $driver ?? 'null'; + } + + return $driver; + } + /** * System is unusable. * diff --git a/tests/Log/LogManagerTest.php b/tests/Log/LogManagerTest.php index 65cd162d76ad..4dfd9fb51e32 100755 --- a/tests/Log/LogManagerTest.php +++ b/tests/Log/LogManagerTest.php @@ -15,6 +15,7 @@ use Monolog\Logger as Monolog; use Orchestra\Testbench\TestCase; use ReflectionProperty; +use RuntimeException; class LogManagerTest extends TestCase { @@ -203,6 +204,41 @@ public function testLogManagerCreatesMonologHandlerWithProperFormatter() } } + public function testItUtilisesTheNullDriverDuringTestsWhenNullDriverUsed() + { + $config = $this->app->make('config'); + $config->set('logging.default', null); + $config->set('logging.channels.null', [ + 'driver' => 'monolog', + 'handler' => NullHandler::class, + ]); + $manager = new class($this->app) extends LogManager + { + protected function createEmergencyLogger() + { + throw new RuntimeException('Emergency logger was created.'); + } + }; + + // In tests, this should not need to create the emergency logger... + $manager->info('message'); + + // we should also be able to forget the null channel... + $this->assertCount(1, $manager->getChannels()); + $manager->forgetChannel(); + $this->assertCount(0, $manager->getChannels()); + + // However in production we want it to fallback to the emergency logger... + $this->app['env'] = 'production'; + try { + $manager->info('message'); + + $this->fail('Emergency logger was not created as expected.'); + } catch (RuntimeException $exception) { + $this->assertSame('Emergency logger was created.', $exception->getMessage()); + } + } + public function testLogManagerCreateSingleDriverWithConfiguredFormatter() { $config = $this->app['config'];