|
10 | 10 |
|
11 | 11 | namespace Tests\Unit;
|
12 | 12 |
|
| 13 | +use Longman\TelegramBot\Exception\TelegramLogException; |
13 | 14 | use Longman\TelegramBot\TelegramLog;
|
| 15 | +use Monolog\Handler\StreamHandler; |
| 16 | +use Monolog\Logger; |
| 17 | +use Tests\TestHelpers; |
14 | 18 |
|
15 |
| -use Longman\TelegramBot\Exception\TelegramLogException; |
16 | 19 | /**
|
17 | 20 | * @package TelegramTest
|
18 | 21 | * @author Avtandil Kikabidze <[email protected]>
|
|
22 | 25 | */
|
23 | 26 | class TelegramLogTest extends TestCase
|
24 | 27 | {
|
| 28 | + /** |
| 29 | + * Logfile paths |
| 30 | + */ |
| 31 | + private $logfiles = [ |
| 32 | + 'error' => '/tmp/errorlog.log', |
| 33 | + 'debug' => '/tmp/debuglog.log', |
| 34 | + 'update' => '/tmp/updatelog.log', |
| 35 | + 'external' => '/tmp/externallog.log', |
| 36 | + ]; |
25 | 37 |
|
26 | 38 | /**
|
27 |
| - * setUp |
28 |
| - */ |
| 39 | + * setUp |
| 40 | + */ |
29 | 41 | protected function setUp()
|
30 | 42 | {
|
| 43 | + // Make sure no monolog instance is set before each test. |
| 44 | + TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'monolog', null); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * tearDown |
| 49 | + */ |
| 50 | + protected function tearDown() |
| 51 | + { |
| 52 | + // Make sure no logfiles exist. |
| 53 | + foreach ($this->logfiles as $file) { |
| 54 | + file_exists($file) && unlink($file); |
| 55 | + } |
31 | 56 | }
|
32 | 57 |
|
33 | 58 | /**
|
@@ -62,37 +87,59 @@ public function newInstanceWithoutUpdatePath()
|
62 | 87 | */
|
63 | 88 | public function testErrorStream()
|
64 | 89 | {
|
65 |
| - $file = '/tmp/errorlog.log'; |
| 90 | + $file = $this->logfiles['error']; |
66 | 91 | $this->assertFalse(file_exists($file));
|
67 | 92 | TelegramLog::initErrorLog($file);
|
68 | 93 | TelegramLog::error('my error');
|
69 | 94 | $this->assertTrue(file_exists($file));
|
70 |
| - unlink($file); |
| 95 | + $this->assertContains('bot_log.ERROR: my error', file_get_contents($file)); |
71 | 96 | }
|
72 | 97 |
|
73 | 98 | /**
|
74 | 99 | * @test
|
75 | 100 | */
|
76 | 101 | public function testDebugStream()
|
77 | 102 | {
|
78 |
| - $file = '/tmp/debuglog.log'; |
| 103 | + $file = $this->logfiles['debug']; |
79 | 104 | $this->assertFalse(file_exists($file));
|
80 | 105 | TelegramLog::initDebugLog($file);
|
81 | 106 | TelegramLog::debug('my debug');
|
82 | 107 | $this->assertTrue(file_exists($file));
|
83 |
| - unlink($file); |
| 108 | + $this->assertContains('bot_log.DEBUG: my debug', file_get_contents($file)); |
84 | 109 | }
|
85 | 110 |
|
86 | 111 | /**
|
87 | 112 | * @test
|
88 | 113 | */
|
89 | 114 | public function testUpdateStream()
|
90 | 115 | {
|
91 |
| - $file = '/tmp/updatelog.log'; |
| 116 | + $file = $this->logfiles['update']; |
92 | 117 | $this->assertFalse(file_exists($file));
|
93 | 118 | TelegramLog::initUpdateLog($file);
|
94 | 119 | TelegramLog::update('my update');
|
95 | 120 | $this->assertTrue(file_exists($file));
|
96 |
| - unlink($file); |
| 121 | + $this->assertContains('my update', file_get_contents($file)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @test |
| 126 | + */ |
| 127 | + public function testExternalStream() |
| 128 | + { |
| 129 | + $file = $this->logfiles['external']; |
| 130 | + $this->assertFalse(file_exists($file)); |
| 131 | + |
| 132 | + $external_monolog = new Logger('bot_update_log'); |
| 133 | + $external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR)); |
| 134 | + $external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG)); |
| 135 | + |
| 136 | + TelegramLog::initialize($external_monolog); |
| 137 | + TelegramLog::error('my error'); |
| 138 | + TelegramLog::debug('my debug'); |
| 139 | + |
| 140 | + $this->assertTrue(file_exists($file)); |
| 141 | + $file_contents = file_get_contents($file); |
| 142 | + $this->assertContains('bot_update_log.ERROR: my error', $file_contents); |
| 143 | + $this->assertContains('bot_update_log.DEBUG: my debug', $file_contents); |
97 | 144 | }
|
98 | 145 | }
|
0 commit comments