Commit 2296e807 authored by MBoretto's avatar MBoretto

again fixs and tests

parent 845f90cf
......@@ -70,6 +70,15 @@ class TelegramLog
if (self::$monolog === null) {
if ($external_monolog !== null) {
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 {
self::$monolog = new Logger('bot_log');
}
......@@ -86,7 +95,7 @@ class TelegramLog
*/
public static function initErrorLog($path)
{
if ($path !== '') {
if ($path === null || $path === '') {
throw new TelegramLogException('Empty path for error log');
}
self::initialize();
......@@ -103,7 +112,7 @@ class TelegramLog
*/
public static function initDebugLog($path)
{
if ($path !== '') {
if ($path === null || $path === '') {
throw new TelegramLogException('Empty path for debug log');
}
self::initialize();
......@@ -117,11 +126,13 @@ class TelegramLog
* Initilize monolog instance. Singleton
* Is possbile provide an external monolog instance
*
* @param string $path
*
* @return \Monolog\Logger
*/
public static function initUpdateLog($path)
{
if ($path !== '') {
if ($path === null || $path === '') {
throw new TelegramLogException('Empty path for update log');
}
self::$update_log_path = $path;
......
......@@ -22,26 +22,22 @@ use \Longman\TelegramBot\Entities\Message;
class MessageTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
* @var \Longman\TelegramBot\Telegram
*/
private $message;
/**
* setUp
*/
* setUp
*/
protected function setUp()
{
}
protected function generateMessage($string) {
//$string = addslashes($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 = utf8_encode($json);
//$json = utf8_encode($json);
return json_decode($json, true);
}
/**
......@@ -56,8 +52,8 @@ class MessageTest extends TestCase
$this->assertEquals('help', $this->message->getCommand());
$this->assertEquals('/help', $this->message->getText());
$this->assertEquals('', $this->message->getText(true));
// text
// text
$this->message = new Message($this->generateMessage('some text'), 'testbot');
$this->assertEquals('', $this->message->getFullCommand());
......
<?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 @@
namespace Tests\Unit;
use Longman\TelegramBot\TelegramLog;
use Longman\TelegramBot\Exception\TelegramLogException;
use Longman\TelegramBot\Exception\TelegramLogException;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
......@@ -56,4 +56,43 @@ class TelegramLogTest extends TestCase
{
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