Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
9df0118b
Commit
9df0118b
authored
Jul 15, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit tested new Logger functions.
parent
121c77ff
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
6 deletions
+78
-6
logger.php
logger.php
+1
-1
tests/test_logger.php
tests/test_logger.php
+77
-5
No files found.
logger.php
View file @
9df0118b
...
...
@@ -42,7 +42,7 @@ class Logger extends Base {
function
set_dump_format
(
$format
)
{
if
(
!
in_array
(
$format
,
self
::
$allowed_dump_formats
)
)
throw
new
InvalidArgumentException
(
sprintf
(
''
,
$format
));
throw
new
\
InvalidArgumentException
(
sprintf
(
''
,
$format
));
$this
->
dump_format
=
$format
;
}
...
...
tests/test_logger.php
View file @
9df0118b
...
...
@@ -5,6 +5,7 @@ use WebBasics\Logger;
define
(
'NAME'
,
'Testlogger'
);
define
(
'FORMAT'
,
'%(level): %(message)'
);
define
(
'LOGDIR'
,
'build/logs/'
);
define
(
'LOGFILE'
,
'build/temp.log'
);
class
LoggerTest
extends
PHPUnit_Extensions_OutputTestCase
{
...
...
@@ -12,12 +13,38 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
$this
->
logger
=
new
Logger
();
$this
->
logger
->
set_property
(
'name'
,
NAME
);
$this
->
logger
->
set_format
(
FORMAT
);
is_dir
(
'build'
)
||
mkdir
(
'build'
);
}
function
assert_dumps
(
$expected
)
{
$this
->
assertEquals
(
$this
->
logger
->
dumps
(),
$expected
);
}
function
test_set_directory
()
{
$this
->
logger
->
set_directory
(
'logs'
);
$this
->
assertAttributeEquals
(
'logs/'
,
'log_directory'
,
$this
->
logger
);
$this
->
logger
->
set_directory
(
'logs/'
);
$this
->
assertAttributeEquals
(
'logs/'
,
'log_directory'
,
$this
->
logger
);
}
function
test_set_format
()
{
$this
->
logger
->
set_format
(
'foo'
);
$this
->
assertAttributeEquals
(
'foo'
,
'format'
,
$this
->
logger
);
}
function
test_set_dump_format_success
()
{
$this
->
logger
->
set_dump_format
(
'html'
);
$this
->
assertAttributeEquals
(
'html'
,
'dump_format'
,
$this
->
logger
);
}
/**
* @expectedException InvalidArgumentException
*/
function
test_set_dump_format_failure
()
{
$this
->
logger
->
set_dump_format
(
'foo'
);
}
function
test_get_format
()
{
$this
->
assertEquals
(
$this
->
logger
->
get_format
(),
FORMAT
);
}
...
...
@@ -99,15 +126,20 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
/**
* @depends test_process_level
*/
function
test_dump
()
{
function
test_dump
_plain
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
expectOutputString
(
'WARNING: test message'
);
$this
->
logger
->
dump
();
}
function
test_handle_exception
()
{
$this
->
logger
->
handle_exception
(
new
Exception
(
'test message'
));
$this
->
assertNotEquals
(
$this
->
logger
->
dumps
(),
''
);
/**
* @depends test_process_level
*/
function
test_dump_html
()
{
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
set_dump_format
(
'html'
);
$this
->
expectOutputString
(
'<strong>Log:</strong><br /><pre>WARNING: test message</pre>'
);
$this
->
logger
->
dump
();
}
function
test_save
()
{
...
...
@@ -117,7 +149,47 @@ class LoggerTest extends PHPUnit_Extensions_OutputTestCase {
$this
->
logger
->
warning
(
'another test message'
);
$this
->
logger
->
save
(
LOGFILE
);
$this
->
assertStringEqualsFile
(
LOGFILE
,
"WARNING: test message
\n
WARNING: another test message"
);
file_exists
(
LOGFILE
)
&&
unlink
(
LOGFILE
);
unlink
(
LOGFILE
);
}
function
find_logfile
()
{
$files
=
scandir
(
LOGDIR
);
return
$files
[
2
];
}
/**
* @depends test_save
*/
function
test_dump_file_regular
()
{
$this
->
logger
->
set_directory
(
LOGDIR
);
$this
->
logger
->
set_dump_format
(
'file'
);
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
dump
();
$filename
=
$this
->
find_logfile
();
$this
->
assertStringEqualsFile
(
LOGDIR
.
$filename
,
'WARNING: test message'
);
unlink
(
LOGDIR
.
$filename
);
$this
->
assertRegExp
(
'/^log_\d{2}-\d{2}-\d{4}_\d{2}-\d{2}-\d{2}.log$/'
,
$filename
);
}
/**
* @depends test_dump_file_regular
*/
function
test_dump_file_prefix
()
{
$this
->
logger
->
set_directory
(
LOGDIR
);
$this
->
logger
->
set_dump_format
(
'file'
);
$this
->
logger
->
warning
(
'test message'
);
$this
->
logger
->
dump
(
'error'
);
$filename
=
$this
->
find_logfile
();
$this
->
assertStringEqualsFile
(
LOGDIR
.
$filename
,
'WARNING: test message'
);
unlink
(
LOGDIR
.
$filename
);
$this
->
assertRegExp
(
'/^error_\d{2}-\d{2}-\d{4}_\d{2}-\d{2}-\d{2}.log$/'
,
$filename
);
}
function
test_handle_exception
()
{
$this
->
logger
->
handle_exception
(
new
Exception
(
'test message'
));
$this
->
assertNotEquals
(
$this
->
logger
->
dumps
(),
''
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment