Completed
Pull Request — master (#7)
by Robbie
02:07
created

testGetMinimumLogLevelFromConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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