|
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
|
|
|
public static function setUpBeforeClass() |
|
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
|
|
|
public function testLogToFileWithFileHandler() |
|
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
|
|
|
public function testLogCallStaticLogsMessagesBasedOnValidLogLevels($level) |
|
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
|
|
|
public function testLoggingWithPsrContextArrayWithString() |
|
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
|
|
|
public function testLoggingWithPsrContextArrayWithUnparsedThings($context_array) |
|
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
|
|
|
public function testLoggingWithNonArrayContext() |
|
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); |
|
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
|
|
|
} |