xref: /PHP-7.0/ext/spl/tests/observer_002.phpt (revision 610c7fbe)
1--TEST--
2SPL: SplObjectStorage
3--FILE--
4<?php
5
6class 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
39class 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
59class 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
113var_dump($sub->contains($ob1));
114$sub->attach($ob1);
115var_dump($sub->contains($ob1));
116$sub->attach($ob1);
117$sub->attach($ob2);
118$sub->attach($ob3);
119var_dump($sub->count());
120
121$sub->notify();
122
123$sub->detach($ob3);
124var_dump($sub->count());
125
126$sub->notify();
127
128$sub->detach($ob2);
129$sub->detach($ob1);
130var_dump($sub->count());
131
132$sub->notify();
133
134$sub->attach($ob3);
135var_dump($sub->count());
136
137$sub->notify();
138
139?>
140===DONE===
141<?php exit(0); ?>
142--EXPECT--
143bool(false)
144$sub->SubjectImpl::attach($ob1);
145bool(true)
146$sub->SubjectImpl::attach($ob1);
147$sub->SubjectImpl::attach($ob2);
148$sub->SubjectImpl::attach($ob3);
149int(3)
150$sub->SubjectImpl::notify();
151MyObjectStorage::rewind()
152MyObjectStorage::valid(1)
153MyObjectStorage::current($ob1)
154MyObjectStorage::key(0)
155$ob1->ObserverImpl::update($sub);
156MyObjectStorage::next()
157MyObjectStorage::valid(1)
158MyObjectStorage::current($ob2)
159MyObjectStorage::key(1)
160$ob2->ObserverImpl::update($sub);
161MyObjectStorage::next()
162MyObjectStorage::valid(1)
163MyObjectStorage::current($ob3)
164MyObjectStorage::key(2)
165$ob3->ObserverImpl::update($sub);
166MyObjectStorage::next()
167MyObjectStorage::valid(0)
168$sub->SubjectImpl::detach($ob3);
169int(2)
170$sub->SubjectImpl::notify();
171MyObjectStorage::rewind()
172MyObjectStorage::valid(1)
173MyObjectStorage::current($ob1)
174MyObjectStorage::key(0)
175$ob1->ObserverImpl::update($sub);
176MyObjectStorage::next()
177MyObjectStorage::valid(1)
178MyObjectStorage::current($ob2)
179MyObjectStorage::key(1)
180$ob2->ObserverImpl::update($sub);
181MyObjectStorage::next()
182MyObjectStorage::valid(0)
183$sub->SubjectImpl::detach($ob2);
184$sub->SubjectImpl::detach($ob1);
185int(0)
186$sub->SubjectImpl::notify();
187MyObjectStorage::rewind()
188MyObjectStorage::valid(0)
189$sub->SubjectImpl::attach($ob3);
190int(1)
191$sub->SubjectImpl::notify();
192MyObjectStorage::rewind()
193MyObjectStorage::valid(1)
194MyObjectStorage::current($ob3)
195MyObjectStorage::key(0)
196$ob3->ObserverImpl::update($sub);
197MyObjectStorage::next()
198MyObjectStorage::valid(0)
199===DONE===
200