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