Completed
Branch master (9dcfc4)
by Daniel
24:32
created

EnvironmentCheckerTest_CheckNoErrors   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 7
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A check() 0 4 1
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Tests;
4
5
use Phockito;
6
use Psr\Log\LoggerInterface;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Dev\TestOnly;
11
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
12
use SilverStripe\EnvironmentCheck\EnvironmentChecker;
13
use SilverStripe\EnvironmentCheck\EnvironmentCheckSuite;
14
15
/**
16
 * Class EnvironmentCheckerTest
17
 *
18
 * @package environmentcheck
19
 */
20
class EnvironmentCheckerTest extends SapphireTest
21
{
22
    /**
23
     * {@inheritDoc}
24
     * @var bool
25
     */
26
    protected $usesDatabase = true;
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public static function setUpBeforeClass()
32
    {
33
        parent::setUpBeforeClass();
34
35
        Phockito::include_hamcrest();
36
37
        $logger = Injector::inst()->get(LoggerInterface::class);
38
        if ($logger instanceof \Monolog\Logger) {
39
            // It logs to stderr by default - disable
40
            $logger->pushHandler(new \Monolog\Handler\NullHandler);
41
        }
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function setUp()
48
    {
49
        parent::setUp();
50
        Config::nest();
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function tearDown()
57
    {
58
        Config::unnest();
59
        parent::tearDown();
60
    }
61
62
    public function testOnlyLogsWithErrors()
63
    {
64
        Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true);
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true);
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckNoErrors());
67
        $checker = Phockito::spy(
68
            EnvironmentChecker::class,
69
            'test suite',
70
            'test'
71
        );
72
73
        $response = $checker->index();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
        Phockito::verify($checker, 0)->log(\anything(), \anything());
75
        EnvironmentCheckSuite::reset();
76
    }
77
78 View Code Duplication
    public function testLogsWithWarnings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true);
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false);
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings());
83
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors());
84
        $checker = Phockito::spy(
85
            EnvironmentChecker::class,
86
            'test suite',
87
            'test'
88
        );
89
90
        $response = $checker->index();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        Phockito::verify($checker, 1)->log(containsString('warning'), \anything());
92
        Phockito::verify($checker, 0)->log(containsString('error'), \anything());
93
        EnvironmentCheckSuite::reset();
94
    }
95
96 View Code Duplication
    public function testLogsWithErrors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false);
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
        Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true);
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckWarnings());
101
        EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest_CheckErrors());
102
        $checker = Phockito::spy(
103
            EnvironmentChecker::class,
104
            'test suite',
105
            'test'
106
        );
107
108
        $response = $checker->index();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
        Phockito::verify($checker, 0)->log(containsString('warning'), \anything());
110
        Phockito::verify($checker, 1)->log(containsString('error'), \anything());
111
        EnvironmentCheckSuite::reset();
112
    }
113
}
114
115
class EnvironmentCheckerTest_CheckNoErrors implements EnvironmentCheck, TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
116
{
117
    public function check()
118
    {
119
        return [EnvironmentCheck::OK, ''];
120
    }
121
}
122
123
class EnvironmentCheckerTest_CheckWarnings implements EnvironmentCheck, TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
124
{
125
    public function check()
126
    {
127
        return [EnvironmentCheck::WARNING, 'test warning'];
128
    }
129
}
130
131
class EnvironmentCheckerTest_CheckErrors implements EnvironmentCheck, TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
132
{
133
    public function check()
134
    {
135
        return [EnvironmentCheck::ERROR, 'test error'];
136
    }
137
}
138