mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Add LoggerState to disble user-configured logging during PHPUnit tests
This commit is contained in:
parent
580214cc30
commit
7ab55a4948
@ -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: '*'
|
||||
|
42
src/Dev/State/LoggerState.php
Normal file
42
src/Dev/State/LoggerState.php
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user