1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Test; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use BlueEvent\Event\Base\EventDispatcher; |
7
|
|
|
use BlueRegister\Events\Event; |
8
|
|
|
use BlueRegister\Events\RegisterEvent; |
9
|
|
|
use BlueRegister\Log; |
10
|
|
|
use SimpleLog\Log as SimpleLog; |
11
|
|
|
|
12
|
|
|
class EventTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* name of test event log file |
16
|
|
|
*/ |
17
|
|
|
const REGISTER_LOG_NAME = '/debug.log'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @expectedException \LogicException |
21
|
|
|
*/ |
22
|
|
|
public function testIncorrectEventObject() |
23
|
|
|
{ |
24
|
|
|
$logPath = __DIR__ . '/log'; |
25
|
|
|
$this->clearLog($logPath); |
26
|
|
|
|
27
|
|
|
new Event( |
28
|
|
|
[ |
29
|
|
|
'event_object' => TestClass\SimpleClass::class, |
30
|
|
|
'event_config' => [], |
31
|
|
|
], |
32
|
|
|
$this->createLogObject() |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @expectedException \LogicException |
38
|
|
|
* @expectedExceptionMessage Class don't exists: SomeClass |
39
|
|
|
*/ |
40
|
|
|
public function testIncorrectEventObjectFromNoneExistingClass() |
41
|
|
|
{ |
42
|
|
|
$logPath = __DIR__ . '/log'; |
43
|
|
|
$this->clearLog($logPath); |
44
|
|
|
|
45
|
|
|
new Event( |
46
|
|
|
[ |
47
|
|
|
'event_object' => 'SomeClass', |
48
|
|
|
'event_config' => [], |
49
|
|
|
], |
50
|
|
|
$this->createLogObject() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \LogicException |
56
|
|
|
* @expectedExceptionMessage Cannot create Event instance: Test\TestClass\SimpleClass |
57
|
|
|
*/ |
58
|
|
|
public function testCreateEventInstanceFromIncorrectObject() |
59
|
|
|
{ |
60
|
|
|
$logPath = __DIR__ . '/log'; |
61
|
|
|
$this->clearLog($logPath); |
62
|
|
|
|
63
|
|
|
new Event( |
64
|
|
|
[ |
65
|
|
|
'event_object' => new \Test\TestClass\SimpleClass, |
66
|
|
|
'event_config' => [], |
67
|
|
|
], |
68
|
|
|
$this->createLogObject() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCreateEventObject() |
73
|
|
|
{ |
74
|
|
|
$logPath = __DIR__ . '/log'; |
75
|
|
|
$this->clearLog($logPath); |
76
|
|
|
$log = $this->createLogObject(); |
77
|
|
|
|
78
|
|
|
$event1 = new Event( |
79
|
|
|
[ |
80
|
|
|
'event_object' => new EventDispatcher(), |
81
|
|
|
'event_config' => [], |
82
|
|
|
], |
83
|
|
|
$log |
84
|
|
|
); |
85
|
|
|
$event2 = new Event( |
86
|
|
|
[ |
87
|
|
|
'event_object' => EventDispatcher::class, |
88
|
|
|
'event_config' => [], |
89
|
|
|
], |
90
|
|
|
$log |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf(Event::class, $event1); |
94
|
|
|
$this->assertInstanceOf(Event::class, $event2); |
95
|
|
|
$this->clearLog($logPath); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testCallEvent() |
99
|
|
|
{ |
100
|
|
|
$logPath = __DIR__ . '/log'; |
101
|
|
|
$testData = []; |
102
|
|
|
$eventConfig = [ |
103
|
|
|
'register_before_create' => [ |
104
|
|
|
'object' => RegisterEvent::class, |
105
|
|
|
'listeners' => [ |
106
|
|
|
function ($event) use (&$testData) { |
107
|
|
|
/** @var $event \BlueRegister\Events\RegisterEvent */ |
108
|
|
|
$testData['register_before_create'] = $event->getEventParameters(); |
109
|
|
|
} |
110
|
|
|
] |
111
|
|
|
] |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
$log = $this->createLogObject(); |
115
|
|
|
|
116
|
|
|
$event = new Event( |
117
|
|
|
[ |
118
|
|
|
'event_object' => new EventDispatcher(['events' => $eventConfig]), |
119
|
|
|
'event_config' => [], |
120
|
|
|
], |
121
|
|
|
$log |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->assertEmpty($testData); |
125
|
|
|
$event->callEvent('register_before_create', []); |
126
|
|
|
|
127
|
|
|
$this->assertNotEmpty($testData); |
128
|
|
|
$this->assertInstanceOf(EventDispatcher::class, $testData['register_before_create'][0]['event_object']); |
129
|
|
|
|
130
|
|
|
$this->clearLog($logPath); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testCallEventWithLog() |
134
|
|
|
{ |
135
|
|
|
$logPath = __DIR__ . '/log'; |
136
|
|
|
$eventConfig = [ |
137
|
|
|
'register_before_create' => [ |
138
|
|
|
'object' => RegisterEvent::class, |
139
|
|
|
'listeners' => [] |
140
|
|
|
] |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
$this->clearLog($logPath); |
144
|
|
|
|
145
|
|
|
$log = $this->createLogObject(); |
146
|
|
|
|
147
|
|
|
$event = new Event( |
148
|
|
|
[ |
149
|
|
|
'event_object' => new EventDispatcher(['events' => $eventConfig]), |
150
|
|
|
'event_config' => [], |
151
|
|
|
], |
152
|
|
|
$log |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$this->assertFileNotExists($logPath . self::REGISTER_LOG_NAME); |
156
|
|
|
|
157
|
|
|
$event->callEvent('register_before_create', []); |
158
|
|
|
|
159
|
|
|
$this->assertFileExists($logPath . self::REGISTER_LOG_NAME); |
160
|
|
|
$this->clearLog($logPath); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function clearLog($logPath) |
164
|
|
|
{ |
165
|
|
|
$logFile = $logPath . self::REGISTER_LOG_NAME; |
166
|
|
|
|
167
|
|
|
if (file_exists($logFile)) { |
168
|
|
|
unlink($logFile); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return Log |
174
|
|
|
*/ |
175
|
|
|
protected function createLogObject() |
176
|
|
|
{ |
177
|
|
|
$logPath = __DIR__ . '/log'; |
178
|
|
|
|
179
|
|
|
$simpleLog = new SimpleLog; |
180
|
|
|
$simpleLog->setOption('log_path', $logPath) |
181
|
|
|
->setOption('level', 'debug'); |
182
|
|
|
|
183
|
|
|
return new Log($simpleLog); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|