-
-
Notifications
You must be signed in to change notification settings - Fork 959
Expand file tree
/
Copy pathTelegramLogTest.php
More file actions
116 lines (97 loc) · 4.02 KB
/
Copy pathTelegramLogTest.php
File metadata and controls
116 lines (97 loc) · 4.02 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
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Tests\Unit;
use Longman\TelegramBot\TelegramLog;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link https://github.com/php-telegram-bot/core
*/
class TelegramLogTest extends TestCase
{
/**
* @var array Dummy logfile paths
*/
private static $logfiles = [
'debug' => '/tmp/php-telegram-bot-debug.log',
'error' => '/tmp/php-telegram-bot-error.log',
'update' => '/tmp/php-telegram-bot-update.log',
];
protected function setUp(): void
{
TelegramLog::initialize(
new Logger('bot_log', [
(new StreamHandler(self::$logfiles['debug'], Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
(new StreamHandler(self::$logfiles['error'], Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
]),
new Logger('bot_log_updates', [
(new StreamHandler(self::$logfiles['update'], Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)),
])
);
}
protected function tearDown(): void
{
// Make sure no logger instance is set after each test.
TestHelpers::setStaticProperty(TelegramLog::class, 'logger', null);
TestHelpers::setStaticProperty(TelegramLog::class, 'update_logger', null);
// Make sure no logfiles exist.
foreach (self::$logfiles as $file) {
file_exists($file) && unlink($file);
}
}
public function testNullLogger(): void
{
TelegramLog::initialize(null, null);
TelegramLog::debug('my debug log');
TelegramLog::error('my error log');
TelegramLog::update('my update log');
foreach (self::$logfiles as $file) {
self::assertFileDoesNotExist($file);
}
}
public function testDebugStream(): void
{
$file = self::$logfiles['debug'];
self::assertFileDoesNotExist($file);
TelegramLog::debug('my debug log');
TelegramLog::debug('my {place} {holder} debug log', ['place' => 'custom', 'holder' => 'placeholder']);
self::assertFileExists($file);
$debug_log = file_get_contents($file);
self::assertStringContainsString('bot_log.DEBUG: my debug log', $debug_log);
self::assertStringContainsString('bot_log.DEBUG: my custom placeholder debug log', $debug_log);
}
public function testErrorStream(): void
{
$file = self::$logfiles['error'];
self::assertFileDoesNotExist($file);
TelegramLog::error('my error log');
TelegramLog::error('my {place} {holder} error log', ['place' => 'custom', 'holder' => 'placeholder']);
self::assertFileExists($file);
$error_log = file_get_contents($file);
self::assertStringContainsString('bot_log.ERROR: my error log', $error_log);
self::assertStringContainsString('bot_log.ERROR: my custom placeholder error log', $error_log);
}
public function testUpdateStream(): void
{
$file = self::$logfiles['update'];
self::assertFileDoesNotExist($file);
TelegramLog::update('my update log');
TelegramLog::update('my {place} {holder} update log', ['place' => 'custom', 'holder' => 'placeholder']);
self::assertFileExists($file);
$update_log = file_get_contents($file);
self::assertStringContainsString('my update log', $update_log);
self::assertStringContainsString('my custom placeholder update log', $update_log);
}
}