~ubuntu-branches/ubuntu/quantal/php5/quantal

« back to all changes in this revision

Viewing changes to ext/spl/tests/observer_002.phpt

  • Committer: Bazaar Package Importer
  • Author(s): Sean Finney
  • Date: 2009-07-01 09:12:10 UTC
  • mto: (0.9.1) (1.1.17 upstream)
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: james.westby@ubuntu.com-20090701091210-go0h6506p62on17r
Tags: upstream-5.3.0
ImportĀ upstreamĀ versionĀ 5.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--TEST--
 
2
SPL: SplObjectStorage
 
3
--FILE--
 
4
<?php
 
5
 
 
6
class MyObjectStorage extends SplObjectStorage
 
7
{
 
8
        function rewind()
 
9
        {
 
10
                echo __METHOD__ . "()\n";
 
11
                parent::rewind();
 
12
        }
 
13
 
 
14
        function valid()
 
15
        {
 
16
                echo __METHOD__ . "(" . (parent::valid() ? 1 : 0) . ")\n";
 
17
                return parent::valid();
 
18
        }
 
19
 
 
20
        function key()
 
21
        {
 
22
                echo __METHOD__ . "(" . parent::key() . ")\n";
 
23
                return parent::key();
 
24
        }
 
25
 
 
26
        function current()
 
27
        {
 
28
                echo __METHOD__ . "(" . parent::current()->getName() . ")\n";
 
29
                return parent::current();
 
30
        }
 
31
 
 
32
        function next()
 
33
        {
 
34
                echo __METHOD__ . "()\n";
 
35
                parent::next();
 
36
        }
 
37
}
 
38
 
 
39
class ObserverImpl implements SplObserver
 
40
{
 
41
        protected $name = '';
 
42
 
 
43
        function __construct($name = 'obj')
 
44
        {
 
45
                $this->name = '$' . $name;
 
46
        }
 
47
 
 
48
        function update(SplSubject $subject)
 
49
        {
 
50
                echo $this->name . '->' . __METHOD__ . '(' . $subject->getName() . ");\n";
 
51
        }
 
52
        
 
53
        function getName()
 
54
        {
 
55
                return $this->name;
 
56
        }
 
57
}
 
58
 
 
59
class SubjectImpl implements SplSubject
 
60
{
 
61
        protected $name = '';
 
62
        protected $observers;
 
63
 
 
64
        function __construct($name = 'sub')
 
65
        {
 
66
                $this->observers = new MyObjectStorage;
 
67
                $this->name = '$' . $name;
 
68
        }
 
69
 
 
70
        function attach(SplObserver $observer)
 
71
        {
 
72
                echo $this->name . '->' . __METHOD__ . '(' . $observer->getName() . ");\n";
 
73
                $this->observers->attach($observer);
 
74
        }
 
75
        
 
76
        function detach(SplObserver $observer)
 
77
        {
 
78
                echo $this->name . '->' . __METHOD__ . '(' . $observer->getName() . ");\n";
 
79
                $this->observers->detach($observer);
 
80
        }
 
81
        
 
82
        function count()
 
83
        {
 
84
                return $this->observers->count();
 
85
        }
 
86
        
 
87
        function notify()
 
88
        {
 
89
                echo $this->name . '->' . __METHOD__ . "();\n";
 
90
                foreach($this->observers as $key => $observer)
 
91
                {
 
92
                        $observer->update($this);
 
93
                }
 
94
        }
 
95
 
 
96
        function getName()
 
97
        {
 
98
                return $this->name;
 
99
        }
 
100
        
 
101
        function contains($obj)
 
102
        {
 
103
                return $this->observers->contains($obj);
 
104
        }
 
105
}
 
106
 
 
107
$sub = new SubjectImpl;
 
108
 
 
109
$ob1 = new ObserverImpl("ob1");
 
110
$ob2 = new ObserverImpl("ob2");
 
111
$ob3 = new ObserverImpl("ob3");
 
112
 
 
113
var_dump($sub->contains($ob1));
 
114
$sub->attach($ob1);
 
115
var_dump($sub->contains($ob1));
 
116
$sub->attach($ob1);
 
117
$sub->attach($ob2);
 
118
$sub->attach($ob3);
 
119
var_dump($sub->count());
 
120
 
 
121
$sub->notify();
 
122
 
 
123
$sub->detach($ob3);
 
124
var_dump($sub->count());
 
125
 
 
126
$sub->notify();
 
127
 
 
128
$sub->detach($ob2);
 
129
$sub->detach($ob1);
 
130
var_dump($sub->count());
 
131
 
 
132
$sub->notify();
 
133
 
 
134
$sub->attach($ob3);
 
135
var_dump($sub->count());
 
136
 
 
137
$sub->notify();
 
138
 
 
139
?>
 
140
===DONE===
 
141
<?php exit(0); ?>
 
142
--EXPECT--
 
143
bool(false)
 
144
$sub->SubjectImpl::attach($ob1);
 
145
bool(true)
 
146
$sub->SubjectImpl::attach($ob1);
 
147
$sub->SubjectImpl::attach($ob2);
 
148
$sub->SubjectImpl::attach($ob3);
 
149
int(3)
 
150
$sub->SubjectImpl::notify();
 
151
MyObjectStorage::rewind()
 
152
MyObjectStorage::valid(1)
 
153
MyObjectStorage::current($ob1)
 
154
MyObjectStorage::key(0)
 
155
$ob1->ObserverImpl::update($sub);
 
156
MyObjectStorage::next()
 
157
MyObjectStorage::valid(1)
 
158
MyObjectStorage::current($ob2)
 
159
MyObjectStorage::key(1)
 
160
$ob2->ObserverImpl::update($sub);
 
161
MyObjectStorage::next()
 
162
MyObjectStorage::valid(1)
 
163
MyObjectStorage::current($ob3)
 
164
MyObjectStorage::key(2)
 
165
$ob3->ObserverImpl::update($sub);
 
166
MyObjectStorage::next()
 
167
MyObjectStorage::valid(0)
 
168
$sub->SubjectImpl::detach($ob3);
 
169
int(2)
 
170
$sub->SubjectImpl::notify();
 
171
MyObjectStorage::rewind()
 
172
MyObjectStorage::valid(1)
 
173
MyObjectStorage::current($ob1)
 
174
MyObjectStorage::key(0)
 
175
$ob1->ObserverImpl::update($sub);
 
176
MyObjectStorage::next()
 
177
MyObjectStorage::valid(1)
 
178
MyObjectStorage::current($ob2)
 
179
MyObjectStorage::key(1)
 
180
$ob2->ObserverImpl::update($sub);
 
181
MyObjectStorage::next()
 
182
MyObjectStorage::valid(0)
 
183
$sub->SubjectImpl::detach($ob2);
 
184
$sub->SubjectImpl::detach($ob1);
 
185
int(0)
 
186
$sub->SubjectImpl::notify();
 
187
MyObjectStorage::rewind()
 
188
MyObjectStorage::valid(0)
 
189
$sub->SubjectImpl::attach($ob3);
 
190
int(1)
 
191
$sub->SubjectImpl::notify();
 
192
MyObjectStorage::rewind()
 
193
MyObjectStorage::valid(1)
 
194
MyObjectStorage::current($ob3)
 
195
MyObjectStorage::key(0)
 
196
$ob3->ObserverImpl::update($sub);
 
197
MyObjectStorage::next()
 
198
MyObjectStorage::valid(0)
 
199
===DONE===