Skip to content

Commit

Permalink
allow tests to utilise the null logger (#38785)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Sep 13, 2021
1 parent 510651a commit c42c356
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -450,7 +450,7 @@ protected function configurationFor($name)
/**
* Get the default log driver name.
*
* @return string
* @return string|null
*/
public function getDefaultDriver()
{
Expand Down Expand Up @@ -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()) {

This comment has been minimized.

Copy link
@oanhnn

oanhnn Oct 25, 2021

see #39347

$driver = $driver ?? 'null';
}

return $driver;
}

/**
* System is unusable.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Log/LogManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Monolog\Logger as Monolog;
use Orchestra\Testbench\TestCase;
use ReflectionProperty;
use RuntimeException;

class LogManagerTest extends TestCase
{
Expand Down Expand Up @@ -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'];
Expand Down

0 comments on commit c42c356

Please sign in to comment.