Annotation of embedaddon/php/ext/spl/tests/observer_001.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: SPL: SplObserver and SplSubject (empty notify)
! 3: --FILE--
! 4: <?php
! 5:
! 6: class ObserverImpl implements SplObserver
! 7: {
! 8: protected $name = '';
! 9:
! 10: function __construct($name = 'obj')
! 11: {
! 12: $this->name = '$' . $name;
! 13: }
! 14:
! 15: function update(SplSubject $subject)
! 16: {
! 17: echo $this->name . '->' . __METHOD__ . '(' . $subject->getName() . ");\n";
! 18: }
! 19:
! 20: function getName()
! 21: {
! 22: return $this->name;
! 23: }
! 24: }
! 25:
! 26: class SubjectImpl implements SplSubject
! 27: {
! 28: protected $name = '';
! 29: protected $observers = array();
! 30:
! 31: function __construct($name = 'sub')
! 32: {
! 33: $this->name = '$' . $name;
! 34: }
! 35:
! 36: function attach(SplObserver $observer)
! 37: {
! 38: echo '$sub->' . __METHOD__ . '(' . $observer->getName() . ");\n";
! 39: if (!in_array($observer, $this->observers))
! 40: {
! 41: $this->observers[] = $observer;
! 42: }
! 43: }
! 44:
! 45: function detach(SplObserver $observer)
! 46: {
! 47: echo '$sub->' . __METHOD__ . '(' . $observer->getName() . ");\n";
! 48: $idx = array_search($observer, $this->observers);
! 49: if ($idx !== false)
! 50: {
! 51: unset($this->observers[$idx]);
! 52: }
! 53: }
! 54:
! 55: function notify()
! 56: {
! 57: echo '$sub->' . __METHOD__ . "();\n";
! 58: foreach($this->observers as $observer)
! 59: {
! 60: $observer->update($this);
! 61: }
! 62: }
! 63:
! 64: function getName()
! 65: {
! 66: return $this->name;
! 67: }
! 68: }
! 69:
! 70: $sub = new SubjectImpl;
! 71:
! 72: $ob1 = new ObserverImpl("ob1");
! 73: $ob2 = new ObserverImpl("ob2");
! 74: $ob3 = new ObserverImpl("ob3");
! 75:
! 76: $sub->attach($ob1);
! 77: $sub->attach($ob1);
! 78: $sub->attach($ob2);
! 79: $sub->attach($ob3);
! 80:
! 81: $sub->notify();
! 82:
! 83: $sub->detach($ob3);
! 84:
! 85: $sub->notify();
! 86:
! 87: $sub->detach($ob2);
! 88: $sub->detach($ob1);
! 89:
! 90: $sub->notify();
! 91:
! 92: $sub->attach($ob3);
! 93:
! 94: $sub->notify();
! 95: ?>
! 96: ===DONE===
! 97: --EXPECT--
! 98: $sub->SubjectImpl::attach($ob1);
! 99: $sub->SubjectImpl::attach($ob1);
! 100: $sub->SubjectImpl::attach($ob2);
! 101: $sub->SubjectImpl::attach($ob3);
! 102: $sub->SubjectImpl::notify();
! 103: $ob1->ObserverImpl::update($sub);
! 104: $ob2->ObserverImpl::update($sub);
! 105: $ob3->ObserverImpl::update($sub);
! 106: $sub->SubjectImpl::detach($ob3);
! 107: $sub->SubjectImpl::notify();
! 108: $ob1->ObserverImpl::update($sub);
! 109: $ob2->ObserverImpl::update($sub);
! 110: $sub->SubjectImpl::detach($ob2);
! 111: $sub->SubjectImpl::detach($ob1);
! 112: $sub->SubjectImpl::notify();
! 113: $sub->SubjectImpl::attach($ob3);
! 114: $sub->SubjectImpl::notify();
! 115: $ob3->ObserverImpl::update($sub);
! 116: ===DONE===
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>