1
|
|
|
<?php |
2
|
|
|
namespace Graze\Monolog; |
3
|
|
|
|
4
|
|
|
use Monolog\TestCase; |
5
|
|
|
|
6
|
|
|
class EventTest extends TestCase |
7
|
|
|
{ |
8
|
|
|
public function setupMockHandler() |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
$handler = $this->getMock('Monolog\Handler\HandlerInterface'); |
11
|
|
|
$handler->expects($this->once()) |
12
|
|
|
->method('handle'); |
13
|
|
|
return $handler; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function setupMockHandlerWithValidationCallback($callback) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$handler = $this->setupMockHandler(); |
19
|
|
|
$handler->expects($this->once()) |
20
|
|
|
->method('handle') |
21
|
|
|
->with($this->callback($callback)); |
22
|
|
|
|
23
|
|
|
return $handler; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testConstruct() |
27
|
|
|
{ |
28
|
|
|
$this->assertInstanceOf('Graze\Monolog\Event', new Event()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testIdentifier() |
32
|
|
|
{ |
33
|
|
|
$callback = function($data) { |
|
|
|
|
34
|
|
|
return 'foo' === $data['eventIdentifier']; |
35
|
|
|
}; |
36
|
|
|
$handler = $this->setupMockHandlerWithValidationCallback($callback); |
37
|
|
|
|
38
|
|
|
$event = new Event(array($handler)); |
|
|
|
|
39
|
|
|
$event->setIdentifier('foo'); |
40
|
|
|
$event->publish(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testData() |
44
|
|
|
{ |
45
|
|
|
$callback = function($data) { |
|
|
|
|
46
|
|
|
return 5 === $data['data']['foo']; |
47
|
|
|
}; |
48
|
|
|
$handler = $this->setupMockHandlerWithValidationCallback($callback); |
49
|
|
|
|
50
|
|
|
$event = new Event(array($handler)); |
|
|
|
|
51
|
|
|
$event->setData('foo',5); |
52
|
|
|
$event->publish(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testMetadata() |
56
|
|
|
{ |
57
|
|
|
$callback = function($data) { |
|
|
|
|
58
|
|
|
return array() === $data['metadata']['bar']; |
|
|
|
|
59
|
|
|
}; |
60
|
|
|
$handler = $this->setupMockHandlerWithValidationCallback($callback); |
61
|
|
|
|
62
|
|
|
$event = new Event(array($handler)); |
|
|
|
|
63
|
|
|
$event->setMetadata('bar',array()); |
|
|
|
|
64
|
|
|
$event->publish(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testPublish() |
68
|
|
|
{ |
69
|
|
|
$handler = $this->setupMockHandler(); |
70
|
|
|
|
71
|
|
|
$event = new Event(array($handler)); |
|
|
|
|
72
|
|
|
$this->assertTrue($event->publish()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testPublishFalseNoHandler() |
76
|
|
|
{ |
77
|
|
|
$event = new Event(); |
78
|
|
|
$this->assertFalse($event->publish()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testMultipleHandlers() |
82
|
|
|
{ |
83
|
|
|
$handlers = array( |
|
|
|
|
84
|
|
|
$this->setupMockHandler(), |
85
|
|
|
$this->setupMockHandler(), |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$event = new Event($handlers); |
89
|
|
|
$this->assertTrue($event->publish()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|