Source of file DataObjectHandlerTest.php
Size: 2,861 Bytes - Last Modified: 2021-12-23T10:24:51+00:00
/var/www/docs.ssmods.com/process/src/tests/Handler/DataObjectHandlerTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 | <?php namespace SilverLeague\LogViewer\Tests\Handler; use Psr\Log\LoggerInterface; use SilverLeague\LogViewer\Handler\DataObjectHandler; use SilverLeague\LogViewer\Model\LogEntry; use SilverStripe\Dev\SapphireTest; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; /** * @package silverstripe-logviewer * @author Robbie Averill <robbie@averill.co.nz> */ class DataObjectHandlerTest extends SapphireTest { /** * A Logger instance * @var Monolog\Logger */ protected $logger; /** * The original logger handlers * @var Monolog\LoggerInterface[] */ protected $originalHandlers = []; /** * {@inheritDoc} */ protected $usesDatabase = true; /** * Create a Logger to test with and clear the existing logger handlers * * {@inheritDoc} */ public function setUp() { parent::setUp(); $this->logger = Injector::inst()->get(LoggerInterface::class); // Clear the default handlers so we can test precisely $this->originalHandlers = $this->logger->getHandlers(); $this->logger->setHandlers([]); } /** * Test that arbitary log levels are all written to the database through the DataObjectHandler */ public function testWriteToDefaultLogger() { $this->logger->pushHandler(new DataObjectHandler); $this->logger->addError('Hello world'); $logEntry = LogEntry::get()->first(); $this->assertContains('Hello world', $logEntry->Entry); $this->assertSame('ERROR', $logEntry->Level); } /** * Test that logs are handled at a minimum level, but not lower than it. */ public function testDontLogMessagesLowerThanMinimumLever() { Config::modify()->set(LogEntry::class, 'minimum_log_level', 300); LogEntry::get()->removeAll(); $this->logger->pushHandler(new DataObjectHandler); $this->logger->addDebug('Debug'); $this->assertSame(0, LogEntry::get()->count()); $this->logger->addWarning('Warning'); $this->assertGreaterThan(0, LogEntry::get()->filter('Level', 'WARNING')->count()); $this->logger->addAlert('Alert'); $this->assertGreaterThan(0, LogEntry::get()->filter('Level', 'ALERT')->count()); } /** * Test that the minumum log capture level is returned from configuration */ public function testGetMinimumLogLevelFromConfiguration() { Config::modify()->set(LogEntry::class, 'minimum_log_level', 123); $this->assertSame(123, (new DataObjectHandler)->getMinimumLogLevel()); } /** * Restore the original logger handlers * * {@inheritDoc} */ public function tearDown() { $this->logger->setHandlers($this->originalHandlers); parent::tearDown(); } } |