|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fwk\Events; |
|
4
|
|
|
|
|
5
|
|
|
if(!class_exists('\Fwk\Events\Dispatcher')) |
|
|
|
|
|
|
6
|
|
|
require_once __DIR__ .'/../src/Dispatcher.php'; |
|
7
|
|
|
|
|
8
|
|
|
if(!class_exists('\Fwk\Events\Event')) |
|
|
|
|
|
|
9
|
|
|
require_once __DIR__ .'/../src/Event.php'; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class MyListenerObj |
|
13
|
|
|
{ |
|
14
|
|
|
public function onTestEvent($event) |
|
15
|
|
|
{ |
|
16
|
|
|
$event->setProcessed(true); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function notAListener() |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
return true; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Test class for EventDispatcher. |
|
27
|
|
|
*/ |
|
28
|
|
|
class DispatcherTest extends \PHPUnit_Framework_TestCase { |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Dispatcher |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $object; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
37
|
|
|
* This method is called before a test is executed. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function setUp() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->object = new Dispatcher; |
|
42
|
|
|
$GLOBALS['testEvent'] = false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testOn() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->object->on('test.event', array($this, 'eventFunction')); |
|
50
|
|
|
$this->object->notify(new Event('test.event')); |
|
51
|
|
|
$this->assertTrue($GLOBALS['testEvent']); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// test function for event callback |
|
55
|
|
|
public function eventFunction($event) |
|
56
|
|
|
{ |
|
57
|
|
|
$GLOBALS['testEvent'] = true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testRemoveListener() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->object->on('test.event', array($this, 'eventFunction')); |
|
65
|
|
|
$this->object->notify(new Event('test.event')); |
|
66
|
|
|
$this->assertTrue($GLOBALS['testEvent']); |
|
67
|
|
|
|
|
68
|
|
|
$GLOBALS['testEvent'] = false; |
|
69
|
|
|
$this->object->removeListener('test.event', array($this, 'eventFunction')); |
|
70
|
|
|
$this->object->notify(new Event('test.event')); |
|
71
|
|
|
$this->assertFalse($GLOBALS['testEvent']); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertInstanceOf('Fwk\Events\Dispatcher', $this->object->removeListener('inexistant.event', array())); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testRemoveAllListeners() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->object->on('test.event', array($this, 'eventFunction')); |
|
81
|
|
|
|
|
82
|
|
|
$this->object->removeAllListeners('test.event'); |
|
83
|
|
|
$this->object->notify(new Event('test.event')); |
|
84
|
|
|
$this->assertFalse($GLOBALS['testEvent']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testNotify() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->assertFalse($GLOBALS['testEvent']); |
|
92
|
|
|
$this->object->on('test.event', array($this, 'eventFunction')); |
|
93
|
|
|
|
|
94
|
|
|
$this->object->notify($event = new Event('test.event')); |
|
95
|
|
|
$this->assertTrue($GLOBALS['testEvent']); |
|
96
|
|
|
$this->assertTrue($event->isProcessed()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testStoppedEvent() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->assertFalse($GLOBALS['testEvent']); |
|
102
|
|
|
$this->object->on('test.event', function(Event $event) { |
|
103
|
|
|
$event->stop(); |
|
104
|
|
|
}); |
|
105
|
|
|
$this->object->on('test.event', array($this, 'eventFunction')); |
|
106
|
|
|
$this->object->notify('test.event'); |
|
107
|
|
|
$this->assertFalse($GLOBALS['testEvent']); // event was stopped |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testListenerObject() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->object->addListener(new MyListenerObj()); |
|
113
|
|
|
$this->object->notify($event = new Event('testEvent')); |
|
114
|
|
|
$this->assertTrue($event->isProcessed()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testInvalidListenerObject() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->setExpectedException('\InvalidArgumentException'); |
|
120
|
|
|
$this->object->addListener(function($event) { return false; }); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|