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