-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathLoggerTest.php
More file actions
182 lines (166 loc) · 5.03 KB
/
Copy pathLoggerTest.php
File metadata and controls
182 lines (166 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Test\Unit\Logger;
use Cloudinary\Test\Unit\TestHelpers\TestLogger;
use Cloudinary\Test\Unit\UnitTestCase;
use Exception;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
/**
* Class LoggerTest
*/
final class LoggerTest extends UnitTestCase
{
/**
* Checks that a different configurations create different handlers
*
* @dataProvider dataProvider
*
* @param array $config
* @param int $expectedCount
* @param array $expectContainClasses
*/
public function testHandlersConfiguration(
array $config,
$expectedCount,
array $expectContainClasses
) {
$testLogger = new TestLogger(['logging' => $config]);
$testLogger->generateLog();
self::assertCount($expectedCount, $testLogger->getHandlers());
if (!empty($expectContainClasses)) {
foreach ($expectContainClasses as $expectedClass) {
self::assertContainsInstancesOf($expectedClass, $testLogger->getHandlers());
}
}
}
/**
* Data provider with different configurations
*
* @return array
*/
public static function dataProvider()
{
return [
[
'config' => [
'file' => [
'my_debug_file' => [
'path' => 'logs/cloudinary.log',
],
'file_for_critical_logs' => [
'path' => 'logs/cloudinary_critical.log',
'level' => 'critical'
],
],
'error_log' => [
'level' => 'ERROR'
]
],
'expectedCount' => 3,
'expectContainClasses' => [
ErrorLogHandler::class,
StreamHandler::class
],
],
[
'config' => [
'error_log' => [
'level' => 'ERROR'
]
],
'expectedCount' => 1,
'expectContainClasses' => [
ErrorLogHandler::class,
],
],
[
'config' => [
'error_log' => true,
],
'expectedCount' => 1,
'expectContainClasses' => [
ErrorLogHandler::class,
],
],
[
'config' => [
'error_log' => false,
],
'expectedCount' => 1,
'expectContainClasses' => [
NullHandler::class,
],
],
[
'config' => [],
'expectedCount' => 1,
'expectContainClasses' => [
ErrorLogHandler::class,
],
],
[
'config' => ['error_log' => []],
'expectedCount' => 1,
'expectContainClasses' => [
ErrorLogHandler::class,
],
],
[
'config' => ['enabled' => false,],
'expectedCount' => 0,
'expectContainClasses' => [],
],
];
}
public function testTriggerErrorOnLoggerWrite()
{
$errorsCaught = [];
$errorHandler = static function ($errNo, $errString) use (&$errorsCaught) {
$errorsCaught[] = ['no' => $errNo, 'message' => $errString];
return true;
};
set_error_handler($errorHandler);
$logger = new TestLogger(
[
'logging' => [
'file' => [
'some-unreachable-path' => [
'path' => 'http://some-unreachable-website',
'level' => 'INFO',
],
],
],
]
);
/**
* Generating logging activity twice to check that error triggered once
*/
$exceptionsCaught = 0;
try {
$logger->generateLog();
} catch (Exception $e) {
$exceptionsCaught++;
}
// Checking that we got an error triggered
self::assertCount(1, $errorsCaught);
self::assertEquals(1, $exceptionsCaught);
try {
$logger->generateLog();
} catch (Exception $e) {
$exceptionsCaught++;
}
// Checking that exception's and error's count is still the same
self::assertCount(1, $errorsCaught);
self::assertEquals(1, $exceptionsCaught);
restore_error_handler();
}
}