1 | <?php |
||
2 | |||
3 | namespace SilverLeague\LogViewer\Tests\Handler; |
||
4 | |||
5 | use Psr\Log\LoggerInterface; |
||
6 | use SilverLeague\LogViewer\Handler\DataObjectHandler; |
||
7 | use SilverLeague\LogViewer\Model\LogEntry; |
||
8 | use SilverStripe\Dev\SapphireTest; |
||
9 | use SilverStripe\Core\Config\Config; |
||
10 | use SilverStripe\Core\Injector\Injector; |
||
11 | |||
12 | /** |
||
13 | * @package silverstripe-logviewer |
||
14 | * @author Robbie Averill <[email protected]> |
||
15 | */ |
||
16 | class DataObjectHandlerTest extends SapphireTest |
||
17 | { |
||
18 | /** |
||
19 | * A Logger instance |
||
20 | * @var Monolog\Logger |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
21 | */ |
||
22 | protected $logger; |
||
23 | |||
24 | /** |
||
25 | * The original logger handlers |
||
26 | * @var Monolog\LoggerInterface[] |
||
27 | */ |
||
28 | protected $originalHandlers = []; |
||
29 | |||
30 | /** |
||
31 | * {@inheritDoc} |
||
32 | */ |
||
33 | protected $usesDatabase = true; |
||
34 | |||
35 | /** |
||
36 | * Create a Logger to test with and clear the existing logger handlers |
||
37 | * |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | public function setUp() |
||
41 | { |
||
42 | parent::setUp(); |
||
43 | |||
44 | $this->logger = Injector::inst()->get(LoggerInterface::class); |
||
45 | |||
46 | // Clear the default handlers so we can test precisely |
||
47 | $this->originalHandlers = $this->logger->getHandlers(); |
||
48 | $this->logger->setHandlers([]); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Test that arbitary log levels are all written to the database through the DataObjectHandler |
||
53 | */ |
||
54 | public function testWriteToDefaultLogger() |
||
55 | { |
||
56 | $this->logger->pushHandler(new DataObjectHandler); |
||
57 | $this->logger->addError('Hello world'); |
||
58 | |||
59 | $logEntry = LogEntry::get()->first(); |
||
60 | $this->assertContains('Hello world', $logEntry->Entry); |
||
61 | $this->assertSame('ERROR', $logEntry->Level); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Test that logs are handled at a minimum level, but not lower than it. |
||
66 | */ |
||
67 | public function testDontLogMessagesLowerThanMinimumLever() |
||
68 | { |
||
69 | Config::modify()->set(LogEntry::class, 'minimum_log_level', 300); |
||
70 | LogEntry::get()->removeAll(); |
||
71 | $this->logger->pushHandler(new DataObjectHandler); |
||
72 | |||
73 | $this->logger->addDebug('Debug'); |
||
74 | $this->assertSame(0, LogEntry::get()->count()); |
||
75 | |||
76 | $this->logger->addWarning('Warning'); |
||
77 | $this->assertGreaterThan(0, LogEntry::get()->filter('Level', 'WARNING')->count()); |
||
78 | |||
79 | $this->logger->addAlert('Alert'); |
||
80 | $this->assertGreaterThan(0, LogEntry::get()->filter('Level', 'ALERT')->count()); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Test that the minumum log capture level is returned from configuration |
||
85 | */ |
||
86 | public function testGetMinimumLogLevelFromConfiguration() |
||
87 | { |
||
88 | Config::modify()->set(LogEntry::class, 'minimum_log_level', 123); |
||
89 | $this->assertSame(123, (new DataObjectHandler)->getMinimumLogLevel()); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Restore the original logger handlers |
||
94 | * |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | public function tearDown() |
||
98 | { |
||
99 | $this->logger->setHandlers($this->originalHandlers); |
||
100 | |||
101 | parent::tearDown(); |
||
102 | } |
||
103 | } |
||
104 |