10
10
11
11
namespace Longman \TelegramBot \Tests \Unit ;
12
12
13
+ use Longman \TelegramBot \Exception \TelegramLogException ;
13
14
use Longman \TelegramBot \TelegramLog ;
14
15
use Monolog \Handler \StreamHandler ;
15
16
use Monolog \Logger ;
@@ -27,17 +28,18 @@ class TelegramLogTest extends TestCase
27
28
* @var array Dummy logfile paths
28
29
*/
29
30
private static $ logfiles = [
30
- 'error ' => '/tmp/php-telegram-bot-errorlog.log ' ,
31
- 'debug ' => '/tmp/php-telegram-bot-debuglog.log ' ,
32
- 'update ' => '/tmp/php-telegram-bot-updatelog.log ' ,
33
- 'external ' => '/tmp/php-telegram-bot-externallog.log ' ,
31
+ 'error ' => '/tmp/php-telegram-bot-error.log ' ,
32
+ 'debug ' => '/tmp/php-telegram-bot-debug.log ' ,
33
+ 'update ' => '/tmp/php-telegram-bot-update.log ' ,
34
+ 'external ' => '/tmp/php-telegram-bot-external.log ' ,
35
+ 'external_update ' => '/tmp/php-telegram-bot-external_update.log ' ,
34
36
];
35
37
36
38
protected function setUp ()
37
39
{
38
40
// Make sure no logger instance is set before each test.
39
- TestHelpers::setStaticProperty (' Longman\TelegramBot\ TelegramLog' , 'logger ' , null );
40
- TestHelpers::setStaticProperty (' Longman\TelegramBot\ TelegramLog' , 'update_logger ' , null );
41
+ TestHelpers::setStaticProperty (TelegramLog::class , 'logger ' , null );
42
+ TestHelpers::setStaticProperty (TelegramLog::class , 'update_logger ' , null );
41
43
}
42
44
43
45
protected function tearDown ()
@@ -48,39 +50,35 @@ protected function tearDown()
48
50
}
49
51
}
50
52
51
- /**
52
- * @expectedException \Longman\TelegramBot\Exception\TelegramLogException
53
- */
54
53
public function testNewInstanceWithoutErrorPath ()
55
54
{
55
+ $ this ->setExpectedException (TelegramLogException::class);
56
56
TelegramLog::initErrorLog ('' );
57
57
}
58
58
59
- /**
60
- * @expectedException \Longman\TelegramBot\Exception\TelegramLogException
61
- */
62
59
public function testNewInstanceWithoutDebugPath ()
63
60
{
61
+ $ this ->setExpectedException (TelegramLogException::class);
64
62
TelegramLog::initDebugLog ('' );
65
63
}
66
64
67
- /**
68
- * @expectedException \Longman\TelegramBot\Exception\TelegramLogException
69
- */
70
65
public function testNewInstanceWithoutUpdatePath ()
71
66
{
67
+ $ this ->setExpectedException (TelegramLogException::class);
72
68
TelegramLog::initUpdateLog ('' );
73
69
}
74
70
75
71
public function testErrorStream ()
76
72
{
77
73
$ file = self ::$ logfiles ['error ' ];
74
+
78
75
$ this ->assertFileNotExists ($ file );
79
76
TelegramLog::initErrorLog ($ file );
80
77
TelegramLog::error ('my error ' );
81
78
TelegramLog::error ('my 50% error ' );
82
79
TelegramLog::error ('my old %s %s error ' , 'custom ' , 'placeholder ' );
83
80
TelegramLog::error ('my new {place} {holder} error ' , ['place ' => 'custom ' , 'holder ' => 'placeholder ' ]);
81
+
84
82
$ this ->assertFileExists ($ file );
85
83
$ error_log = file_get_contents ($ file );
86
84
$ this ->assertContains ('bot_log.ERROR: my error ' , $ error_log );
@@ -92,12 +90,14 @@ public function testErrorStream()
92
90
public function testDebugStream ()
93
91
{
94
92
$ file = self ::$ logfiles ['debug ' ];
93
+
95
94
$ this ->assertFileNotExists ($ file );
96
95
TelegramLog::initDebugLog ($ file );
97
96
TelegramLog::debug ('my debug ' );
98
97
TelegramLog::debug ('my 50% debug ' );
99
98
TelegramLog::debug ('my old %s %s debug ' , 'custom ' , 'placeholder ' );
100
99
TelegramLog::debug ('my new {place} {holder} debug ' , ['place ' => 'custom ' , 'holder ' => 'placeholder ' ]);
100
+
101
101
$ this ->assertFileExists ($ file );
102
102
$ debug_log = file_get_contents ($ file );
103
103
$ this ->assertContains ('bot_log.DEBUG: my debug ' , $ debug_log );
@@ -109,12 +109,14 @@ public function testDebugStream()
109
109
public function testUpdateStream ()
110
110
{
111
111
$ file = self ::$ logfiles ['update ' ];
112
+
112
113
$ this ->assertFileNotExists ($ file );
113
114
TelegramLog::initUpdateLog ($ file );
114
115
TelegramLog::update ('my update ' );
115
116
TelegramLog::update ('my 50% update ' );
116
117
TelegramLog::update ('my old %s %s update ' , 'custom ' , 'placeholder ' );
117
118
TelegramLog::update ('my new {place} {holder} update ' , ['place ' => 'custom ' , 'holder ' => 'placeholder ' ]);
119
+
118
120
$ this ->assertFileExists ($ file );
119
121
$ update_log = file_get_contents ($ file );
120
122
$ this ->assertContains ('my update ' , $ update_log );
@@ -125,14 +127,18 @@ public function testUpdateStream()
125
127
126
128
public function testExternalStream ()
127
129
{
128
- $ file = self ::$ logfiles ['external ' ];
130
+ $ file = self ::$ logfiles ['external ' ];
131
+ $ file_update = self ::$ logfiles ['external_update ' ];
129
132
$ this ->assertFileNotExists ($ file );
133
+ $ this ->assertFileNotExists ($ file_update );
130
134
131
- $ external_monolog = new Logger ('bot_external_log ' );
132
- $ external_monolog ->pushHandler (new StreamHandler ($ file , Logger::ERROR ));
133
- $ external_monolog ->pushHandler (new StreamHandler ($ file , Logger::DEBUG ));
135
+ $ external_logger = new Logger ('bot_external_log ' );
136
+ $ external_logger ->pushHandler (new StreamHandler ($ file , Logger::ERROR ));
137
+ $ external_logger ->pushHandler (new StreamHandler ($ file , Logger::DEBUG ));
138
+ $ external_update_logger = new Logger ('bot_external_update_log ' );
139
+ $ external_update_logger ->pushHandler (new StreamHandler ($ file_update , Logger::INFO ));
134
140
135
- TelegramLog::initialize ($ external_monolog );
141
+ TelegramLog::initialize ($ external_logger , $ external_update_logger );
136
142
TelegramLog::error ('my error ' );
137
143
TelegramLog::error ('my 50% error ' );
138
144
TelegramLog::error ('my old %s %s error ' , 'custom ' , 'placeholder ' );
@@ -141,9 +147,15 @@ public function testExternalStream()
141
147
TelegramLog::debug ('my 50% debug ' );
142
148
TelegramLog::debug ('my old %s %s debug ' , 'custom ' , 'placeholder ' );
143
149
TelegramLog::debug ('my new {place} {holder} debug ' , ['place ' => 'custom ' , 'holder ' => 'placeholder ' ]);
150
+ TelegramLog::update ('my update ' );
151
+ TelegramLog::update ('my 50% update ' );
152
+ TelegramLog::update ('my old %s %s update ' , 'custom ' , 'placeholder ' );
153
+ TelegramLog::update ('my new {place} {holder} update ' , ['place ' => 'custom ' , 'holder ' => 'placeholder ' ]);
144
154
145
155
$ this ->assertFileExists ($ file );
146
- $ file_contents = file_get_contents ($ file );
156
+ $ this ->assertFileExists ($ file_update );
157
+ $ file_contents = file_get_contents ($ file );
158
+ $ file_update_contents = file_get_contents ($ file_update );
147
159
$ this ->assertContains ('bot_external_log.ERROR: my error ' , $ file_contents );
148
160
$ this ->assertContains ('bot_external_log.ERROR: my 50% error ' , $ file_contents );
149
161
$ this ->assertContains ('bot_external_log.ERROR: my old custom placeholder error ' , $ file_contents );
@@ -152,5 +164,9 @@ public function testExternalStream()
152
164
$ this ->assertContains ('bot_external_log.DEBUG: my 50% debug ' , $ file_contents );
153
165
$ this ->assertContains ('bot_external_log.DEBUG: my old custom placeholder debug ' , $ file_contents );
154
166
$ this ->assertContains ('bot_external_log.DEBUG: my new custom placeholder debug ' , $ file_contents );
167
+ $ this ->assertContains ('bot_external_update_log.INFO: my update ' , $ file_update_contents );
168
+ $ this ->assertContains ('bot_external_update_log.INFO: my 50% update ' , $ file_update_contents );
169
+ $ this ->assertContains ('bot_external_update_log.INFO: my old custom placeholder update ' , $ file_update_contents );
170
+ $ this ->assertContains ('bot_external_update_log.INFO: my new custom placeholder update ' , $ file_update_contents );
155
171
}
156
172
}
0 commit comments