Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
// $Id: DispatcherTest.php 42 2009-09-21 10:01:23Z tiefland $
4
 
5
/**
6
 * Unit tests for Event_Dispatcher package.
7
 *
8
 * @author     Bertrand Mansion <bmansion@mamasam.com>
9
 * @package    Event_Dispatcher
10
 * @subpackage Tests
11
 */
12
require_once 'PHPUnit/Framework/TestCase.php';
13
require_once 'Event/Dispatcher.php';
14
 
15
class Notified
16
{
17
    var $notif;
18
 
19
    function notifReceived(&$notif)
20
    {
21
        $this->notif =& $notif;
22
    }
23
 
24
    function description()
25
    {
26
        $notObj =& $this->notif->getNotificationObject();
27
        $name = $this->notif->getNotificationName();
28
        $info = $this->notif->getNotificationInfo();
29
        $desc = $name.':'.implode(':', $info).':'.$notObj->id;
30
        return $desc;
31
    }
32
}
33
 
34
class Dummy
35
{
36
    var $id;
37
    function Dummy($id = 'default')
38
    {
39
        $this->id = $id;
40
    }
41
}
42
 
43
class Notifier
44
{
45
    var $id = 'notifier';
46
    function Notifier($id)
47
    {
48
        $this->id = $id;
49
        $ed =& Event_Dispatcher::getInstance();
50
        $ed->post($this, 'NotifierInstanciated', array('info'));
51
    }
52
}
53
 
54
function notified(&$notif)
55
{
56
    $obj = $notif->getNotificationObject();
57
    $obj->id = $notif->getNotificationName().':'.implode(':', $notif->getNotificationInfo());
58
}
59
 
60
class DispatcherTest extends PHPUnit_Framework_TestCase
61
{
62
 
63
    // Get the default dispatch center
64
    function test1()
65
    {
66
        $nf = new Notified();
67
        $dm = new Dummy();
68
        $ed =& Event_Dispatcher::getInstance();
69
 
70
        // Generic notification, global observer
71
        $ed->addObserver(array(&$nf, 'notifReceived'));
72
        $not =& $ed->post($dm, 'test', array('A', 'B'));
73
        $this->assertEquals('test:A:B:default', $nf->description(), "Error");
74
        $this->assertEquals(1, $not->getNotificationCount(), "Wrong notification count");
75
 
76
        // Object references
77
        $dm->id = 'dummy';
78
        $this->assertEquals('test:A:B:dummy', $nf->description(), "Wrong notification description");
79
 
80
        // Named notifications
81
        $ed->addObserver('notified', 'NotifierInstanciated');
82
        $nt = new Notifier('notifier');
83
        $this->assertEquals('NotifierInstanciated:info', $nt->id, "Wrong notification id");
84
 
85
        // Pending notifications
86
        $not =& $ed->post($nt, 'PendingNotification');
87
        $ed->addObserver(array(&$nf, 'notifReceived'), 'PendingNotification');
88
        $this->assertEquals('PendingNotification::NotifierInstanciated:info', $nf->description(), "Error");
89
        $this->assertEquals(2, $not->getNotificationCount(), "Error");
90
 
91
        // Class filter 1
92
        $ed->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Dummy');
93
        $not =& $ed->post($nt, 'ClassFilterNotification', array('isGlobal'));
94
        $this->assertEquals('ClassFilterNotification:isGlobal:NotifierInstanciated:info', $nf->description(), "Error");
95
        $this->assertEquals(1, $not->getNotificationCount(), "Error");
96
 
97
        // Remove observer
98
        $ed->removeObserver(array(&$nf, 'notifReceived'));
99
        $nt->id = 'reset';
100
        $not =& $ed->post($nt, 'ClassFilterNotification', array('test'));
101
        $this->assertEquals('ClassFilterNotification:isGlobal:reset', $nf->description(), "Error");
102
        $this->assertEquals(0, $not->getNotificationCount(), "Error");
103
 
104
        // Class filter 2
105
        $not =& $ed->post($dm, 'ClassFilterNotification');
106
        $this->assertEquals('ClassFilterNotification::dummy', $nf->description(), "Error");
107
        $this->assertEquals(1, $not->getNotificationCount(), "Error");
108
 
109
        // Re-add the global observer
110
        $ed->addObserver(array(&$nf, 'notifReceived'));
111
        $not =& $ed->post($dm, 'ClassFilterNotification');
112
        $this->assertEquals('ClassFilterNotification::dummy', $nf->description(), "Error");
113
        $this->assertEquals(2, $not->getNotificationCount(), "Error");
114
 
115
    }
116
 
117
    // Tests with 2 dispatchers
118
    function test2()
119
    {
120
        $nf = new Notified();
121
        $dm = new Dummy();
122
 
123
        $ed2 =& Event_Dispatcher::getInstance('another');
124
        $ed1 =& Event_Dispatcher::getInstance();
125
 
126
        $ed2->addObserver(array(&$nf, 'notifReceived'));
127
        $not =& $ed2->post($dm, 'test', array('A', 'B'));
128
        $this->assertEquals('test:A:B:default', $nf->description(), "Error");
129
        $this->assertEquals(1, $not->getNotificationCount(), "Error");
130
 
131
        $not =& $ed1->post($dm, 'test', array('A2', 'B2'));
132
        $this->assertEquals(1, $not->getNotificationCount(), "Error");
133
 
134
        $not =& $ed1->post($dm, 'test', array('A2', 'B2'));
135
        $this->assertEquals(1, $not->getNotificationCount(), "Error");
136
 
137
        $ed2->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Notifier');
138
        $not =& $ed2->post($dm, 'ClassFilterNotification');
139
        $this->assertEquals('ClassFilterNotification::default', $nf->description(), "Error");
140
        $this->assertEquals(1, $not->getNotificationCount(), "Error");
141
 
142
        $ed2->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Dummy');
143
        $not =& $ed2->post($dm, 'ClassFilterNotification');
144
        $this->assertEquals(2, $not->getNotificationCount(), "Error");
145
 
146
    }
147
}
148
?>