Skip to content

Commit 92a3ea2

Browse files
committed
added failing test for #33
1 parent 7044374 commit 92a3ea2

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

src/Base.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ public function setLogLevel($level)
236236
}
237237
}
238238

239+
/**
240+
* Check if a message with the given level should be logged
241+
*
242+
* @param string $level
243+
* @return bool
244+
*/
245+
public function isLogLevelEnabled($level)
246+
{
247+
if (!isset($this->loglevel[$level])) $this->fatal('Unknown log level');
248+
return $this->loglevel[$level]['enabled'];
249+
}
250+
239251
/**
240252
* Exits the program on a fatal error
241253
*
@@ -283,7 +295,7 @@ protected function logMessage($level, $message, array $context = array())
283295
if (!isset($this->loglevel[$level])) $level = 'error';
284296

285297
$info = $this->loglevel[$level];
286-
if (!$info['enabled']) return; // no logging for this level
298+
if (!$this->isLogLevelEnabled($level)) return; // no logging for this level
287299

288300
$message = $this->interpolate($message, $context);
289301

tests/LogLevelTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace splitbrain\phpcli\tests;
4+
5+
6+
use splitbrain\phpcli\CLI;
7+
use splitbrain\phpcli\Options;
8+
9+
class LogLevel extends CLI
10+
{
11+
protected function setup(Options $options)
12+
{
13+
}
14+
15+
protected function main(Options $options)
16+
{
17+
}
18+
}
19+
20+
class LogLevelTest extends \PHPUnit\Framework\TestCase
21+
{
22+
23+
24+
public function provideLevels()
25+
{
26+
return array(
27+
array(
28+
'debug',
29+
array('debug', 'info', 'notice', 'success', 'warning', 'error', 'critical', 'alert', 'emergency'),
30+
array(),
31+
),
32+
array(
33+
'info',
34+
array('info', 'notice', 'success', 'warning', 'error', 'critical', 'alert', 'emergency'),
35+
array('debug'),
36+
),
37+
array(
38+
'notice',
39+
array('notice', 'success', 'warning', 'error', 'critical', 'alert', 'emergency'),
40+
array('debug', 'info'),
41+
),
42+
array(
43+
'success',
44+
array('success', 'warning', 'error', 'critical', 'alert', 'emergency'),
45+
array('debug', 'info', 'notice'),
46+
),
47+
array(
48+
'warning',
49+
array('warning', 'error', 'critical', 'alert', 'emergency'),
50+
array('debug', 'info', 'notice', 'success'),
51+
),
52+
array(
53+
'error',
54+
array('error', 'critical', 'alert', 'emergency'),
55+
array('debug', 'info', 'notice', 'success', 'warning'),
56+
),
57+
array(
58+
'critical',
59+
array('critical', 'alert', 'emergency'),
60+
array('debug', 'info', 'notice', 'success', 'warning', 'error'),
61+
),
62+
array(
63+
'alert',
64+
array('alert', 'emergency'),
65+
array('debug', 'info', 'notice', 'success', 'warning', 'error', 'critical'),
66+
),
67+
array(
68+
'emergency',
69+
array('emergency'),
70+
array('debug', 'info', 'notice', 'success', 'warning', 'error', 'critical', 'alert'),
71+
),
72+
);
73+
}
74+
75+
/**
76+
* @dataProvider provideLevels
77+
*/
78+
public function testLevels($level, $enabled, $disabled)
79+
{
80+
$cli = new LogLevel();
81+
$cli->setLogLevel($level);
82+
foreach ($enabled as $e) {
83+
$this->assertTrue($cli->isLogLevelEnabled($e), "$e is not enabled but should be");
84+
}
85+
foreach ($disabled as $d) {
86+
$this->assertFalse($cli->isLogLevelEnabled($d), "$d is enabled but should not be");
87+
}
88+
}
89+
90+
91+
}

0 commit comments

Comments
 (0)