Skip to content

Commit 4b3019f

Browse files
committed
Merge pull request php-telegram-bot#12 from noplanman/harden_tests
Check log file for correct contents.
2 parents 6b63936 + 55c8693 commit 4b3019f

File tree

3 files changed

+71
-62
lines changed

3 files changed

+71
-62
lines changed

tests/TestHelpers.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class TestHelpers
5353
/**
5454
* Set the value of a private/protected property of an object
5555
*
56-
* @param object $object Object that contains the private property
56+
* @param object $object Object that contains the property
5757
* @param string $property Name of the property who's value we want to set
5858
* @param mixed $value The value to set to the property
5959
*/
@@ -65,6 +65,20 @@ public static function setObjectProperty($object, $property, $value)
6565
$ref_property->setValue($object, $value);
6666
}
6767

68+
/**
69+
* Set the value of a private/protected static property of a class
70+
*
71+
* @param string $class Class that contains the static property
72+
* @param string $property Name of the property who's value we want to set
73+
* @param mixed $value The value to set to the property
74+
*/
75+
public static function setStaticProperty($class, $property, $value)
76+
{
77+
$ref_property = new \ReflectionProperty($class, $property);
78+
$ref_property->setAccessible(true);
79+
$ref_property->setValue(null, $value);
80+
}
81+
6882
/**
6983
* Return a simple fake Update object
7084
*

tests/Unit/TelegramLogExternalTest.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/Unit/TelegramLogTest.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
namespace Tests\Unit;
1212

13+
use Longman\TelegramBot\Exception\TelegramLogException;
1314
use Longman\TelegramBot\TelegramLog;
15+
use Monolog\Handler\StreamHandler;
16+
use Monolog\Logger;
17+
use Tests\TestHelpers;
1418

15-
use Longman\TelegramBot\Exception\TelegramLogException;
1619
/**
1720
* @package TelegramTest
1821
* @author Avtandil Kikabidze <[email protected]>
@@ -22,12 +25,34 @@
2225
*/
2326
class TelegramLogTest extends TestCase
2427
{
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+
];
2537

2638
/**
27-
* setUp
28-
*/
39+
* setUp
40+
*/
2941
protected function setUp()
3042
{
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+
}
3156
}
3257

3358
/**
@@ -62,37 +87,59 @@ public function newInstanceWithoutUpdatePath()
6287
*/
6388
public function testErrorStream()
6489
{
65-
$file = '/tmp/errorlog.log';
90+
$file = $this->logfiles['error'];
6691
$this->assertFalse(file_exists($file));
6792
TelegramLog::initErrorLog($file);
6893
TelegramLog::error('my error');
6994
$this->assertTrue(file_exists($file));
70-
unlink($file);
95+
$this->assertContains('bot_log.ERROR: my error', file_get_contents($file));
7196
}
7297

7398
/**
7499
* @test
75100
*/
76101
public function testDebugStream()
77102
{
78-
$file = '/tmp/debuglog.log';
103+
$file = $this->logfiles['debug'];
79104
$this->assertFalse(file_exists($file));
80105
TelegramLog::initDebugLog($file);
81106
TelegramLog::debug('my debug');
82107
$this->assertTrue(file_exists($file));
83-
unlink($file);
108+
$this->assertContains('bot_log.DEBUG: my debug', file_get_contents($file));
84109
}
85110

86111
/**
87112
* @test
88113
*/
89114
public function testUpdateStream()
90115
{
91-
$file = '/tmp/updatelog.log';
116+
$file = $this->logfiles['update'];
92117
$this->assertFalse(file_exists($file));
93118
TelegramLog::initUpdateLog($file);
94119
TelegramLog::update('my update');
95120
$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);
97144
}
98145
}

0 commit comments

Comments
 (0)