Skip to content

Commit 2296e80

Browse files
committed
again fixs and tests
1 parent 845f90c commit 2296e80

File tree

4 files changed

+114
-16
lines changed

4 files changed

+114
-16
lines changed

src/TelegramLog.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public static function initialize(Logger $external_monolog = null)
7070
if (self::$monolog === null) {
7171
if ($external_monolog !== null) {
7272
self::$monolog = $external_monolog;
73+
74+
foreach (self::$monolog->getHandlers() as $handler) {
75+
if ($handler->getLevel() == 400 ) {
76+
self::$error_log_path = true;
77+
}
78+
if ($handler->getLevel() == 100 ) {
79+
self::$debug_log_path = true;
80+
}
81+
}
7382
} else {
7483
self::$monolog = new Logger('bot_log');
7584
}
@@ -86,7 +95,7 @@ public static function initialize(Logger $external_monolog = null)
8695
*/
8796
public static function initErrorLog($path)
8897
{
89-
if ($path !== '') {
98+
if ($path === null || $path === '') {
9099
throw new TelegramLogException('Empty path for error log');
91100
}
92101
self::initialize();
@@ -103,7 +112,7 @@ public static function initErrorLog($path)
103112
*/
104113
public static function initDebugLog($path)
105114
{
106-
if ($path !== '') {
115+
if ($path === null || $path === '') {
107116
throw new TelegramLogException('Empty path for debug log');
108117
}
109118
self::initialize();
@@ -117,11 +126,13 @@ public static function initDebugLog($path)
117126
* Initilize monolog instance. Singleton
118127
* Is possbile provide an external monolog instance
119128
*
129+
* @param string $path
130+
*
120131
* @return \Monolog\Logger
121132
*/
122133
public static function initUpdateLog($path)
123134
{
124-
if ($path !== '') {
135+
if ($path === null || $path === '') {
125136
throw new TelegramLogException('Empty path for update log');
126137
}
127138
self::$update_log_path = $path;

tests/Unit/Entities/MessageTest.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,22 @@
2222
class MessageTest extends TestCase
2323
{
2424
/**
25-
* @var \Longman\TelegramBot\Telegram
26-
*/
25+
* @var \Longman\TelegramBot\Telegram
26+
*/
2727
private $message;
28-
28+
2929
/**
30-
* setUp
31-
*/
30+
* setUp
31+
*/
3232
protected function setUp()
3333
{
3434
}
3535

36-
37-
3836
protected function generateMessage($string) {
3937

40-
41-
//$string = addslashes($string);
4238
$string = str_replace("\n", "\\n", $string);
4339
$json = '{"message_id":961,"from":{"id":123,"first_name":"john","username":"john"},"chat":{"id":123,"title":null,"first_name":"john","last_name":null,"username":"null"},"date":1435920612,"text":"'.$string.'"}';
44-
//$json = utf8_encode($json);
40+
//$json = utf8_encode($json);
4541
return json_decode($json, true);
4642
}
4743
/**
@@ -56,8 +52,8 @@ public function testTextAndCommandRecognise() {
5652
$this->assertEquals('help', $this->message->getCommand());
5753
$this->assertEquals('/help', $this->message->getText());
5854
$this->assertEquals('', $this->message->getText(true));
59-
60-
// text
55+
56+
// text
6157
$this->message = new Message($this->generateMessage('some text'), 'testbot');
6258

6359
$this->assertEquals('', $this->message->getFullCommand());
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Tests\Unit;
12+
13+
use Longman\TelegramBot\TelegramLog;
14+
15+
use Longman\TelegramBot\Exception\TelegramLogException;
16+
/**
17+
* @package TelegramTest
18+
* @author Avtandil Kikabidze <[email protected]>
19+
* @copyright Avtandil Kikabidze <[email protected]>
20+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
21+
* @link http://www.github.com/akalongman/php-telegram-bot
22+
*/
23+
class TelegramLogExternalTest extends TestCase
24+
{
25+
26+
/**
27+
* setUp
28+
*/
29+
protected function setUp()
30+
{
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function testExtenalStream()
37+
{
38+
$file = '/tmp/externallog.log';
39+
$this->assertFalse(file_exists($file));
40+
41+
$external_monolog = new \Monolog\Logger('bot_update_log');
42+
$update_handler = new \Monolog\Handler\StreamHandler($file, \Monolog\Logger::ERROR);
43+
$info_handler = new \Monolog\Handler\StreamHandler($file, \Monolog\Logger::INFO);
44+
$external_monolog->pushHandler($update_handler);
45+
$external_monolog->pushHandler($info_handler);
46+
47+
TelegramLog::initialize($external_monolog);
48+
TelegramLog::error('my error');
49+
$this->assertTrue(file_exists($file));
50+
unlink($file);
51+
}
52+
}

tests/Unit/TelegramLogTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
namespace Tests\Unit;
1212

1313
use Longman\TelegramBot\TelegramLog;
14-
use Longman\TelegramBot\Exception\TelegramLogException;
1514

15+
use Longman\TelegramBot\Exception\TelegramLogException;
1616
/**
1717
* @package TelegramTest
1818
* @author Avtandil Kikabidze <[email protected]>
@@ -56,4 +56,43 @@ public function newInstanceWithoutUpdatePath()
5656
{
5757
TelegramLog::initUpdateLog('');
5858
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function testErrorStream()
64+
{
65+
$file = '/tmp/errorlog.log';
66+
$this->assertFalse(file_exists($file));
67+
TelegramLog::initErrorLog($file);
68+
TelegramLog::error('my error');
69+
$this->assertTrue(file_exists($file));
70+
unlink($file);
71+
}
72+
73+
/**
74+
* @test
75+
*/
76+
public function testDebugStream()
77+
{
78+
$file = '/tmp/debuglog.log';
79+
$this->assertFalse(file_exists($file));
80+
TelegramLog::initDebugLog($file);
81+
TelegramLog::debug('my debug');
82+
$this->assertTrue(file_exists($file));
83+
unlink($file);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function testUpdateStream()
90+
{
91+
$file = '/tmp/updatelog.log';
92+
$this->assertFalse(file_exists($file));
93+
TelegramLog::initUpdateLog($file);
94+
TelegramLog::update('my update');
95+
$this->assertTrue(file_exists($file));
96+
unlink($file);
97+
}
5998
}

0 commit comments

Comments
 (0)