Test Setup Failed
Push — master ( aa9039...94c501 )
by Derek
03:32
created

testLoggingWithExceptionContext()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 11
nc 1
nop 0
1
<?php
2
namespace Subreality\Dilmun\Nabu;
3
4
use Psr\Log\InvalidArgumentException;
5
use Subreality\Dilmun\Nabu\LoggerHandler\File;
6
use Subreality\Dilmun\Nabu\StaticLogger as static_logger;
7
8
class StaticLoggerTest extends \PHPUnit_Framework_TestCase
9
{
10
    public static $full_log_location;
11
12
    /**
13
     * @var File
14
     */
15
    public static $log_file;
16
17 View Code Duplication
    public static function setUpBeforeClass()
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...
18
    {
19
        self::$full_log_location = DILMUN_LOGS_DIR . "StaticLogger_test.log";
20
21
        if(file_exists(self::$full_log_location))
22
        {
23
            unlink(self::$full_log_location);
24
        }
25
26
        self::$log_file = new File(self::$full_log_location);
27
        self::$log_file->initialize();
28
    }
29
30
    protected function assertPreconditions()
31
    {
32
        $this->assertFileExists(self::$full_log_location);
33
    }
34
35
    protected function setUp()
36
    {
37
        if(file_exists(self::$full_log_location))
38
        {
39
            self::$log_file->clearFile();
40
        }
41
    }
42
43
    protected function tearDown()
44
    {
45
    }
46
47
    public static function tearDownAfterClass()
48
    {
49
        if(file_exists(self::$full_log_location))
50
        {
51
            unlink(self::$full_log_location);
52
        }
53
    }
54
55 View Code Duplication
    public function testLogToFileWithFileHandler()
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...
56
    {
57
        static_logger::setHandler(self::$log_file);
58
59
        $returned_message = static_logger::log("debug", "Test successful.");
60
61
        $logged_message = file_get_contents(self::$full_log_location);
62
63
        $this->assertContains($returned_message, $logged_message);
64
    }
65
66
    /**
67
     * @expectedException InvalidArgumentException
68
     */
69
    public function testInvalidLogLevelThrowsException()
70
    {
71
        static_logger::log("not_a_log_level", "This message should never be logged!");
72
    }
73
74
    /**
75
     * @dataProvider validLevelProvider
76
     */
77 View Code Duplication
    public function testLogCallStaticLogsMessagesBasedOnValidLogLevels($level)
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...
78
    {
79
        static_logger::setHandler(self::$log_file);
80
81
        //This generates a PhpStorm error but is valid code
82
        $returned_message = static_logger::{$level}("This is a {$level} log entry.");
83
84
        $logged_message = file_get_contents(self::$full_log_location);
85
86
        $this->assertContains($returned_message, $logged_message);
87
    }
88
89 View Code Duplication
    public function testLoggingWithPsrContextArrayWithString()
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...
90
    {
91
        static_logger::setHandler(self::$log_file);
92
93
        $context_array = array(
94
            "context" =>    "stuff"
95
        );
96
97
        $expected_context_message = "Here is some stuff for ya.";
98
99
        $returned_message = static_logger::log("debug","Here is some {context} for ya.",$context_array);
100
101
        $this->assertContains($expected_context_message, $returned_message);
102
    }
103
104
    /**
105
     * @dataProvider contextArrayProvider
106
     */
107 View Code Duplication
    public function testLoggingWithPsrContextArrayWithUnparsedThings($context_array)
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...
108
    {
109
        static_logger::setHandler(self::$log_file);
110
111
        //Provided contexts that do not adhere to PSR-3 templating, the log method should write the message without
112
        //affecting the {context} section and not throw any exceptions.
113
        $expected_context_message = "Here is some {context} for ya.";
114
115
        $returned_message = static_logger::log("debug","Here is some {context} for ya.",$context_array);
116
117
        $this->assertContains($expected_context_message, $returned_message);
118
    }
119
120
    public function testLoggingWithExceptionContext()
121
    {
122
        static_logger::setHandler(self::$log_file);
123
124
        try{
125
            throw new \Exception("This is an arbitrary exception");
126
        }
127
        catch (\Exception $e)
128
        {
129
            $context_array = array(
130
                "exception" =>  $e
131
            );
132
        }
133
134
        //Not testing {line} because it's too volatile.
135
        $log_message = "The Exception processor provides line, file ({file}), and message ({message})";
136
137
        $expected_context_message = 'The Exception processor provides line, file (/home/derek/PHP/Subreality/Dilmun/tests/Nabu/StaticLoggerTest.php), and message (This is an arbitrary exception)';
138
139
        $returned_message = static_logger::log("debug",$log_message,$context_array);
140
141
        $this->assertContains($expected_context_message, $returned_message);
142
    }
143
144
    /**
145
     * @expectedException \PHPUnit_Framework_Error
146
     */
147 View Code Duplication
    public function testLoggingWithNonArrayContext()
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...
148
    {
149
        static_logger::setHandler(self::$log_file);
150
151
        $context_array = 4;
152
153
        //Provided contexts that do not adhere to PSR-3 templating, the log method should write the message without
154
        //affecting the {context} section and not throw any exceptions.
155
        $expected_context_message = "Here is some {context} for ya.";
156
157
        $returned_message = static_logger::log("debug","Here is some {context} for ya.",$context_array);
0 ignored issues
show
Documentation introduced by
$context_array is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
159
        $this->assertContains($expected_context_message, $returned_message);
160
    }
161
162
    /**
163
     * Provides data for testing valid log levels.
164
     *
165
     * @return array
166
     */
167
    public function validLevelProvider()
168
    {
169
        return array(
170
            array("emergency"),
171
            array("alert"),
172
            array("critical"),
173
            array("error"),
174
            array("warning"),
175
            array("notice"),
176
            array("info"),
177
            array("debug"),
178
        );
179
    }
180
181
    public function contextArrayProvider()
182
    {
183
        return array(
184
            array(
185
                array(
186
                    3,
187
                    2,
188
                    1
189
                )
190
            ),
191
            array(
192
                array(
193
                    self::$log_file
194
                )
195
            ),
196
            array(
197
                array(
198
                    "unparsed string"
199
                )
200
            ),
201
            array(
202
                array(
203
                    "stuff" =>  "context"
204
                )
205
            )
206
        );
207
    }
208
}