|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Logger; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Logger\Compositum; |
|
6
|
|
|
use org\bovigo\vfs\vfsStream; |
|
7
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
|
8
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
|
9
|
|
|
|
|
10
|
|
|
class CompositumTest extends \PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Compositum |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $object; |
|
16
|
|
|
|
|
17
|
|
|
public function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->object = new Compositum(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function tearDown() |
|
23
|
|
|
{ |
|
24
|
|
|
unset($this->object); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @covers Koch\Logger\Compositum::Log |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testLog() |
|
31
|
|
|
{ |
|
32
|
|
|
// create a virtual error log file |
|
33
|
|
|
vfsStreamWrapper::register(); |
|
34
|
|
|
$this->configFile = vfsStream::url('root/errorlog.txt'); |
|
|
|
|
|
|
35
|
|
|
$this->file = vfsStream::newFile('errorlog.txt', 0777)->withContent('someContent'); |
|
|
|
|
|
|
36
|
|
|
$this->root = new vfsStreamDirectory('root'); |
|
|
|
|
|
|
37
|
|
|
$this->root->addChild($this->file); |
|
38
|
|
|
vfsStreamWrapper::setRoot($this->root); |
|
39
|
|
|
|
|
40
|
|
|
// add file logger and add virtual log file |
|
41
|
|
|
$logger = new \Koch\Logger\Adapter\File(); |
|
42
|
|
|
$logger->setErrorLogFilename($this->configFile); |
|
43
|
|
|
$this->object->addLogger($logger); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
// setup message to log |
|
46
|
|
|
$message = 'TestMessage'; |
|
47
|
|
|
$context = ['Context Info']; |
|
48
|
|
|
$level = 'ERROR'; |
|
49
|
|
|
|
|
50
|
|
|
$this->assertTrue($this->object->log($level, $message, $context)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @covers Koch\Logger\Compositum::addLogger |
|
55
|
|
|
* @covers Koch\Logger\Compositum::removeLogger |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testAddLogger() |
|
58
|
|
|
{ |
|
59
|
|
|
// add |
|
60
|
|
|
$firebug = new \Koch\Logger\Adapter\Firebug(); |
|
61
|
|
|
$this->object->addLogger($firebug); |
|
|
|
|
|
|
62
|
|
|
$this->assertEquals($firebug, $this->object->loggers[0]); |
|
63
|
|
|
|
|
64
|
|
|
// remove |
|
65
|
|
|
$this->object->removeLogger('firebug'); |
|
66
|
|
|
$this->assertEquals([], $this->object->loggers); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: