Add LoggerState to disble user-configured logging during PHPUnit tests

This commit is contained in:
Robbie Averill 2019-02-22 11:43:01 +07:00
parent 580214cc30
commit 7ab55a4948
2 changed files with 43 additions and 0 deletions

View File

@ -11,6 +11,7 @@ SilverStripe\Core\Injector\Injector:
fixtures: '%$SilverStripe\Dev\State\FixtureTestState'
requirements: '%$SilverStripe\View\Dev\RequirementsTestState'
ssviewer: '%$SilverStripe\View\Dev\SSViewerTestState'
logstate: '%$SilverStripe\Dev\State\LoggerState'
---
Name: kerneltest
Before: '*'

View File

@ -0,0 +1,42 @@
<?php
namespace SilverStripe\Dev\State;
use Monolog\Handler\NullHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
/**
* Disables any user configured loggers by pushing a NullHandler during PHPUnit tests.
*
* This is designed specifically for Monolog. If using another PSR-3 compatible logging package, this will
* not do anything.
*/
class LoggerState implements TestState
{
public function setUp(SapphireTest $test)
{
/** @var Logger $userLogger */
$userLogger = Injector::inst()->get(LoggerInterface::class);
if ($userLogger && $userLogger instanceof Logger) {
$userLogger->setHandlers([new NullHandler()]);
}
}
public function tearDown(SapphireTest $test)
{
// noop
}
public function setUpOnce($class)
{
// noop
}
public function tearDownOnce($class)
{
// noop
}
}