Commit 55c86937 authored by Armando Lüscher's avatar Armando Lüscher

Check log file for correct contents.

Add test helper to modify static properties.
Move external log file test into main log test class.
parent 6b63936d
......@@ -53,7 +53,7 @@ class TestHelpers
/**
* Set the value of a private/protected property of an object
*
* @param object $object Object that contains the private property
* @param object $object Object that contains the property
* @param string $property Name of the property who's value we want to set
* @param mixed $value The value to set to the property
*/
......@@ -65,6 +65,20 @@ class TestHelpers
$ref_property->setValue($object, $value);
}
/**
* Set the value of a private/protected static property of a class
*
* @param string $class Class that contains the static property
* @param string $property Name of the property who's value we want to set
* @param mixed $value The value to set to the property
*/
public static function setStaticProperty($class, $property, $value)
{
$ref_property = new \ReflectionProperty($class, $property);
$ref_property->setAccessible(true);
$ref_property->setValue(null, $value);
}
/**
* Return a simple fake Update object
*
......
<?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);
}
}
......@@ -10,9 +10,12 @@
namespace Tests\Unit;
use Longman\TelegramBot\Exception\TelegramLogException;
use Longman\TelegramBot\TelegramLog;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Tests\TestHelpers;
use Longman\TelegramBot\Exception\TelegramLogException;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
......@@ -22,12 +25,34 @@ use Longman\TelegramBot\Exception\TelegramLogException;
*/
class TelegramLogTest extends TestCase
{
/**
* Logfile paths
*/
private $logfiles = [
'error' => '/tmp/errorlog.log',
'debug' => '/tmp/debuglog.log',
'update' => '/tmp/updatelog.log',
'external' => '/tmp/externallog.log',
];
/**
* setUp
*/
* setUp
*/
protected function setUp()
{
// Make sure no monolog instance is set before each test.
TestHelpers::setStaticProperty('Longman\TelegramBot\TelegramLog', 'monolog', null);
}
/**
* tearDown
*/
protected function tearDown()
{
// Make sure no logfiles exist.
foreach ($this->logfiles as $file) {
file_exists($file) && unlink($file);
}
}
/**
......@@ -62,12 +87,12 @@ class TelegramLogTest extends TestCase
*/
public function testErrorStream()
{
$file = '/tmp/errorlog.log';
$file = $this->logfiles['error'];
$this->assertFalse(file_exists($file));
TelegramLog::initErrorLog($file);
TelegramLog::error('my error');
$this->assertTrue(file_exists($file));
unlink($file);
$this->assertContains('bot_log.ERROR: my error', file_get_contents($file));
}
/**
......@@ -75,12 +100,12 @@ class TelegramLogTest extends TestCase
*/
public function testDebugStream()
{
$file = '/tmp/debuglog.log';
$file = $this->logfiles['debug'];
$this->assertFalse(file_exists($file));
TelegramLog::initDebugLog($file);
TelegramLog::debug('my debug');
$this->assertTrue(file_exists($file));
unlink($file);
$this->assertContains('bot_log.DEBUG: my debug', file_get_contents($file));
}
/**
......@@ -88,11 +113,33 @@ class TelegramLogTest extends TestCase
*/
public function testUpdateStream()
{
$file = '/tmp/updatelog.log';
$file = $this->logfiles['update'];
$this->assertFalse(file_exists($file));
TelegramLog::initUpdateLog($file);
TelegramLog::update('my update');
$this->assertTrue(file_exists($file));
unlink($file);
$this->assertContains('my update', file_get_contents($file));
}
/**
* @test
*/
public function testExternalStream()
{
$file = $this->logfiles['external'];
$this->assertFalse(file_exists($file));
$external_monolog = new Logger('bot_update_log');
$external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR));
$external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG));
TelegramLog::initialize($external_monolog);
TelegramLog::error('my error');
TelegramLog::debug('my debug');
$this->assertTrue(file_exists($file));
$file_contents = file_get_contents($file);
$this->assertContains('bot_update_log.ERROR: my error', $file_contents);
$this->assertContains('bot_update_log.DEBUG: my debug', $file_contents);
}
}
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