3 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__
).'/../../../fixtures/config/config.php');
6 require_once(AK_LIB_DIR
.DS
.'AkActiveRecord.php');
7 require_once(AK_LIB_DIR
.DS
.'AkActiveRecord'.DS
.'AkObserver.php');
10 class test_AkActiveRecord_observer
extends AkUnitTest
15 $this->installAndIncludeModels(array(
16 'ObservedAccount'=>'id, balance, created_at, updated_at',
17 'ObservedPerson'=>'id, user_name, first_name, last_name, city, state'));
18 Ak
::import('TestAuditor','ObservedPersonObserver');
21 function Test_of__instantiateDefaultObserver()
23 $Observed = new ObservedPerson();
24 $ObeserversReference =& $Observed->getObservers();
25 $this->assertEqual(strtolower(get_class($ObeserversReference[0])), 'observedpersonobserver');
28 function Test_of_addObserver()
30 $Observed = new ObservedPerson();
33 $Observer =& Ak
::singleton('ObservedPersonObserver', $null);
35 $params = 'ObservedAccount';
36 $Auditor =& Ak
::singleton('TestAuditor',$params);
37 $Auditor->observe(&$Observed);
39 $ObeserversReference =& $Observed->getObservers();
41 $ObeserversReference[0]->message
= 'Hello. I come from the past';
43 $this->assertEqual($ObeserversReference[0]->__singleton_id
, $Observer->__singleton_id
);
44 $this->assertReference($ObeserversReference[1], $Auditor);
48 function Test_of_addObserver2()
50 $ObservedPerson =& new ObservedPerson();
52 $ObeserversReference =& $ObservedPerson->getObservers();
53 $this->assertEqual(strtolower(get_class($ObeserversReference[0])), 'observedpersonobserver');
54 $this->assertEqual($ObeserversReference[0]->message
, 'Hello. I come from the past');
55 $this->assertEqual(strtolower(get_class($ObeserversReference[1])), 'testauditor');
57 $ObservedAccount =& new ObservedAccount();
58 $ObeserversReference =& $ObservedAccount->getObservers();
59 $this->assertEqual(strtolower(get_class($ObeserversReference[0])), 'testauditor');
62 function __Test_of_setObservableState_and_getObservableState()
64 $ObservedAccount1 =& new ObservedAccount();
65 $ObservedAccount1->setObservableState('creating account 1');
67 $ObservedAccount2 =& new ObservedAccount();
68 $ObservedAccount2->setObservableState('creating account 2');
70 $this->assertEqual($ObservedAccount2->getObservableState(), 'creating account 2');
71 $this->assertEqual($ObservedAccount1->getObservableState(), 'creating account 1');
74 function Test_of_notifyObservers()
76 $ObservedPerson =& new ObservedPerson();
77 $ObservedPerson->setObservableState('new person created');
80 $ObservedPerson->notifyObservers();
81 $content = ob_get_contents();
84 $this->assertEqual($content,'new person creatednew person created');
86 $this->assertEqual($ObservedPerson->getObservableState(), '');
90 function Test_of_default_Active_record_observer_triggers()
92 $ObservedPerson =& new ObservedPerson('first_name->','Bermi');
94 $this->assertTrue(empty($ObservedPerson->audited
));
96 $ObservedPerson->save();
98 $this->assertTrue($ObservedPerson->audited
);
100 $content = ob_get_contents();
102 $this->assertEqual($content, "Bermi has been email with account details");
105 foreach ($ObservedPerson->notified_observers
as $k=>$v){
106 $notified[strtolower($k)] = $v;
108 $this->assertEqual($notified, array ( 'beforevalidation' => 1, 'beforevalidationoncreate' => 1, 'aftervalidationoncreate' => 1, 'aftervalidation' => 1, 'beforecreate' => 1, 'beforesave' => 1, 'aftersave' => 1, 'aftercreate' => 1, ));
110 $ObservedPerson->set('last_name','Ferrer');
111 $ObservedPerson->save();
114 foreach ($ObservedPerson->notified_observers
as $k=>$v){
115 $notified[strtolower($k)] = $v;
117 $this->assertEqual($notified, array ( 'beforevalidation' => 2, 'beforevalidationoncreate' => 1, 'aftervalidationoncreate' => 1, 'aftervalidation' => 2, 'beforecreate' => 1, 'beforesave' => 2, 'aftersave' => 2, 'aftercreate' => 1, 'aftervalidationonupdate' => 1, ));
122 function Test_of_beforeSave_trigger()
124 $ObservedPerson =& new ObservedPerson();
126 $ObservedPerson->city
= "Carlet";
127 $ObservedPerson->state
= "Madrid";
130 $this->assertTrue($ObservedPerson->save());
132 $this->assertTrue($ObservedPerson->reload());
133 $this->assertEqual($ObservedPerson->get('state'), "Valencia");
138 ak_test('test_AkActiveRecord_observer', true);