Commit 2296e807 authored by MBoretto's avatar MBoretto

again fixs and tests

parent 845f90cf
...@@ -70,6 +70,15 @@ class TelegramLog ...@@ -70,6 +70,15 @@ class TelegramLog
if (self::$monolog === null) { if (self::$monolog === null) {
if ($external_monolog !== null) { if ($external_monolog !== null) {
self::$monolog = $external_monolog; self::$monolog = $external_monolog;
foreach (self::$monolog->getHandlers() as $handler) {
if ($handler->getLevel() == 400 ) {
self::$error_log_path = true;
}
if ($handler->getLevel() == 100 ) {
self::$debug_log_path = true;
}
}
} else { } else {
self::$monolog = new Logger('bot_log'); self::$monolog = new Logger('bot_log');
} }
...@@ -86,7 +95,7 @@ class TelegramLog ...@@ -86,7 +95,7 @@ class TelegramLog
*/ */
public static function initErrorLog($path) public static function initErrorLog($path)
{ {
if ($path !== '') { if ($path === null || $path === '') {
throw new TelegramLogException('Empty path for error log'); throw new TelegramLogException('Empty path for error log');
} }
self::initialize(); self::initialize();
...@@ -103,7 +112,7 @@ class TelegramLog ...@@ -103,7 +112,7 @@ class TelegramLog
*/ */
public static function initDebugLog($path) public static function initDebugLog($path)
{ {
if ($path !== '') { if ($path === null || $path === '') {
throw new TelegramLogException('Empty path for debug log'); throw new TelegramLogException('Empty path for debug log');
} }
self::initialize(); self::initialize();
...@@ -117,11 +126,13 @@ class TelegramLog ...@@ -117,11 +126,13 @@ class TelegramLog
* Initilize monolog instance. Singleton * Initilize monolog instance. Singleton
* Is possbile provide an external monolog instance * Is possbile provide an external monolog instance
* *
* @param string $path
*
* @return \Monolog\Logger * @return \Monolog\Logger
*/ */
public static function initUpdateLog($path) public static function initUpdateLog($path)
{ {
if ($path !== '') { if ($path === null || $path === '') {
throw new TelegramLogException('Empty path for update log'); throw new TelegramLogException('Empty path for update log');
} }
self::$update_log_path = $path; self::$update_log_path = $path;
......
...@@ -33,12 +33,8 @@ class MessageTest extends TestCase ...@@ -33,12 +33,8 @@ class MessageTest extends TestCase
{ {
} }
protected function generateMessage($string) { protected function generateMessage($string) {
//$string = addslashes($string);
$string = str_replace("\n", "\\n", $string); $string = str_replace("\n", "\\n", $string);
$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.'"}'; $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.'"}';
//$json = utf8_encode($json); //$json = utf8_encode($json);
......
<?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 Tests\Unit;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Exception\TelegramLogException;
/**
* @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 http://www.github.com/akalongman/php-telegram-bot
*/
class TelegramLogExternalTest extends TestCase
{
/**
* setUp
*/
protected function setUp()
{
}
/**
* @test
*/
public function testExtenalStream()
{
$file = '/tmp/externallog.log';
$this->assertFalse(file_exists($file));
$external_monolog = new \Monolog\Logger('bot_update_log');
$update_handler = new \Monolog\Handler\StreamHandler($file, \Monolog\Logger::ERROR);
$info_handler = new \Monolog\Handler\StreamHandler($file, \Monolog\Logger::INFO);
$external_monolog->pushHandler($update_handler);
$external_monolog->pushHandler($info_handler);
TelegramLog::initialize($external_monolog);
TelegramLog::error('my error');
$this->assertTrue(file_exists($file));
unlink($file);
}
}
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
namespace Tests\Unit; namespace Tests\Unit;
use Longman\TelegramBot\TelegramLog; use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Exception\TelegramLogException;
use Longman\TelegramBot\Exception\TelegramLogException;
/** /**
* @package TelegramTest * @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com> * @author Avtandil Kikabidze <akalongman@gmail.com>
...@@ -56,4 +56,43 @@ class TelegramLogTest extends TestCase ...@@ -56,4 +56,43 @@ class TelegramLogTest extends TestCase
{ {
TelegramLog::initUpdateLog(''); TelegramLog::initUpdateLog('');
} }
/**
* @test
*/
public function testErrorStream()
{
$file = '/tmp/errorlog.log';
$this->assertFalse(file_exists($file));
TelegramLog::initErrorLog($file);
TelegramLog::error('my error');
$this->assertTrue(file_exists($file));
unlink($file);
}
/**
* @test
*/
public function testDebugStream()
{
$file = '/tmp/debuglog.log';
$this->assertFalse(file_exists($file));
TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug');
$this->assertTrue(file_exists($file));
unlink($file);
}
/**
* @test
*/
public function testUpdateStream()
{
$file = '/tmp/updatelog.log';
$this->assertFalse(file_exists($file));
TelegramLog::initUpdateLog($file);
TelegramLog::update('my update');
$this->assertTrue(file_exists($file));
unlink($file);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment