EnvironmentCheckerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 44
c 2
b 1
f 0
dl 0
loc 77
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testOnlyLogsWithErrors() 0 17 1
A testLogsWithWarnings() 0 23 1
A testLogsWithErrors() 0 23 1
A tearDown() 0 4 1
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Tests;
4
5
use Monolog\Logger;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\EnvironmentCheck\EnvironmentChecker;
12
use SilverStripe\EnvironmentCheck\EnvironmentCheckSuite;
13
14
/**
15
 * Class EnvironmentCheckerTest
16
 *
17
 * @package environmentcheck
18
 */
19
class EnvironmentCheckerTest extends SapphireTest
20
{
21
    protected $usesDatabase = true;
22
23
    protected function tearDown()
24
    {
25
        EnvironmentCheckSuite::reset();
26
        parent::tearDown();
27
    }
28
29
    public function testOnlyLogsWithErrors()
30
    {
31
        Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true);
32
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true);
33
34
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckNoErrors());
35
36
        $logger = $this->getMockBuilder(Logger::class)
37
            ->disableOriginalConstructor()
38
            ->setMethods(['log'])
39
            ->getMock();
40
41
        $logger->expects($this->never())->method('log');
42
43
        Injector::inst()->registerService($logger, LoggerInterface::class);
44
45
        (new EnvironmentChecker('test suite', 'test'))->index();
46
    }
47
48
    public function testLogsWithWarnings()
49
    {
50
        Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true);
51
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false);
52
53
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings());
54
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors());
55
56
        $logger = $this->getMockBuilder(Logger::class)
57
            ->disableOriginalConstructor()
58
            ->setMethods(['log'])
59
            ->getMock();
60
61
        $logger->expects($this->once())
62
            ->method('log')
63
            ->withConsecutive(
64
                $this->equalTo(LogLevel::WARNING),
65
                $this->anything()
66
            );
67
68
        Injector::inst()->registerService($logger, LoggerInterface::class);
69
70
        (new EnvironmentChecker('test suite', 'test'))->index();
71
    }
72
73
    public function testLogsWithErrors()
74
    {
75
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false);
76
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true);
77
78
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings());
79
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors());
80
81
        $logger = $this->getMockBuilder(Logger::class)
82
            ->disableOriginalConstructor()
83
            ->setMethods(['log'])
84
            ->getMock();
85
86
        $logger->expects($this->once())
87
            ->method('log')
88
            ->withConsecutive(
89
                [$this->equalTo(LogLevel::ALERT), $this->anything()],
90
                [$this->equalTo(LogLevel::WARNING), $this->anything()]
91
            );
92
93
        Injector::inst()->registerService($logger, LoggerInterface::class);
94
95
        (new EnvironmentChecker('test suite', 'test'))->index();
96
    }
97
}
98