1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* test Event Object class |
5
|
|
|
* |
6
|
|
|
* @package BlueEvent |
7
|
|
|
* @subpackage Test |
8
|
|
|
* @author Michał Adamiak <[email protected]> |
9
|
|
|
* @copyright chajr/bluetree |
10
|
|
|
*/ |
11
|
|
|
namespace BlueEventTest; |
12
|
|
|
|
13
|
|
|
use BlueEvent\Event\Base\EventDispatcher; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
class BaseEventTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* store information that even was triggered |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
public static $eventTriggered = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* test return of event object launch count correct value |
27
|
|
|
*/ |
28
|
|
|
public function testEventLaunchCount() |
29
|
|
|
{ |
30
|
|
|
$instance = new EventDispatcher; |
31
|
|
|
$instance->setEventConfiguration([ |
32
|
|
|
'test_event' => [ |
33
|
|
|
'object' => 'BlueEvent\Event\BaseEvent', |
34
|
|
|
'listeners' => [ |
35
|
|
|
'BlueEvent\Test\BaseEventTest::trigger', |
36
|
|
|
] |
37
|
|
|
], |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
/** use static method to avoid launch increment and store in EventDispatcher instance */ |
41
|
|
|
$this->assertEquals(0, \BlueEvent\Event\BaseEvent::getLaunchCount()); |
42
|
|
|
|
43
|
|
|
$instance->triggerEvent('test_event'); |
44
|
|
|
$instance->triggerEvent('test_event'); |
45
|
|
|
|
46
|
|
|
$this->assertEquals(2, \BlueEvent\Event\BaseEvent::getLaunchCount()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* test all methods in base event object |
51
|
|
|
*/ |
52
|
|
|
public function testEventMethods() |
53
|
|
|
{ |
54
|
|
|
$info = []; |
55
|
|
|
|
56
|
|
|
$instance = new EventDispatcher; |
57
|
|
|
$instance->setEventConfiguration([ |
58
|
|
|
'test_event' => [ |
59
|
|
|
'object' => 'BlueEvent\Event\BaseEvent', |
60
|
|
|
'listeners' => [ |
61
|
|
|
function ($event) use (&$info) { |
62
|
|
|
/** @var $event \BlueEvent\Event\BaseEvent */ |
63
|
|
|
$info[] = $event->getEventCode(); |
64
|
|
|
$info[] = $event->getEventParameters(); |
65
|
|
|
$info[] = $event->isPropagationStopped(); |
66
|
|
|
$info[] = $event->getLaunchCount(); |
67
|
|
|
}, |
68
|
|
|
] |
69
|
|
|
], |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$instance->triggerEvent('test_event', [1, 2]); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( |
75
|
|
|
[ |
76
|
|
|
'test_event', |
77
|
|
|
[1, 2], |
78
|
|
|
false, |
79
|
|
|
3, |
80
|
|
|
], |
81
|
|
|
$info |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testExchangeDateBetweenListeners() |
86
|
|
|
{ |
87
|
|
|
$info = false; |
88
|
|
|
|
89
|
|
|
$instance = new EventDispatcher; |
90
|
|
|
$instance->setEventConfiguration([ |
91
|
|
|
'test_event' => [ |
92
|
|
|
'object' => 'BlueEvent\Event\BaseEvent', |
93
|
|
|
'listeners' => [ |
94
|
|
|
function ($event) { |
95
|
|
|
$event->something = true; |
96
|
|
|
}, |
97
|
|
|
function ($event) use (&$info) { |
98
|
|
|
$info = $event->something; |
99
|
|
|
}, |
100
|
|
|
] |
101
|
|
|
], |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$instance->triggerEvent('test_event'); |
105
|
|
|
|
106
|
|
|
$this->assertTrue($info); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* method to test event triggering |
111
|
|
|
*/ |
112
|
|
|
public static function trigger() |
113
|
|
|
{ |
114
|
|
|
self::$eventTriggered++; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|