Fixing #138
[akelos.git] / test / unit / lib / AkActiveRecord / _AkActiveRecord_callbacks.php
blobe098bc3209ba9e19eab3f10e479439b5b9714c69
1 <?php
2 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
3 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
5 require_once(AK_LIB_DIR.DS.'AkActiveRecord.php');
6 require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkObserver.php');
8 /* Create Mocks */
9 $callbacks_during_save = array('beforeCreate','beforeValidation','beforeValidationOnUpdate','beforeValidationOnCreate','beforeSave','beforeUpdate','beforeDestroy',
10 'afterCreate','afterValidation','afterValidationOnUpdate','afterValidationOnCreate','afterSave','afterUpdate','afterDestroy');
12 function createClass ($classname, $parent, $functions, $function_body){
13 $class_code = "class $classname extends $parent {";
14 if (!AK_PHP5 && $parent=='ActiveRecord') $class_code .= __fix_for_PHP4($classname);
15 foreach ($functions as $function){
16 $class_code .= "function $function(){".$function_body."}";
18 $class_code .= "}";
19 eval($class_code);
21 function __fix_for_PHP4($model_name)
23 $table_name = AkInflector::tableize($model_name);
24 return "function $model_name()
26 \$this->setModelName('$model_name');
27 \$attributes = (array)func_get_args();
28 \$this->setTableName('$table_name');
29 \$this->init(\$attributes);
30 }";
35 createClass('TestCallback','ActiveRecord',$callbacks_during_save,'$this->__called[]=__FUNCTION__;return true;');
36 createClass('TestObserver','AkObserver',$callbacks_during_save,'$this->__called[]=__FUNCTION__;return true;');
38 /* Test */
39 class TestCase_AkActiveRecord_callbacks extends AkUnitTest
41 function test_start()
43 $this->installAndIncludeModels(array('TestCallback'=>'id,name'));
44 if (!isset($this->Observer)) $this->Observer =& new TestObserver(&$this->TestCallback);
47 function tearDown()
49 $this->Observer->__called = array();
52 function test_implementation_of_the_singleton_pattern()
54 $ObservedModel =& new TestCallback();
55 $observers =& $ObservedModel->getObservers();
56 $this->assertReference($this->Observer,$observers[0]);
59 function test_callbacks_on_create()
61 $expected = array ('beforeSave','beforeCreate','afterCreate','afterSave');
62 if (!AK_PHP5) $expected = array_map('strtolower',$expected);
64 $CreateTest =& new TestCallback(array('name'=>'A Name'));
65 $CreateTest->save(false);
67 $this->assertEqual($CreateTest->__called,$expected);
68 $this->assertEqual($this->Observer->__called,$expected);
71 function test_callbacks_on_create_with_validation()
73 $expected = array ('beforeSave','beforeValidation','afterValidation','beforeValidationOnCreate','afterValidationOnCreate','beforeCreate','afterCreate','afterSave');
74 if (!AK_PHP5) $expected = array_map('strtolower',$expected);
76 $CreateTest =& new TestCallback(array('name'=>'Another Name'));
77 $CreateTest->save(true);
79 $this->assertEqual($CreateTest->__called,$expected);
80 $this->assertEqual($this->Observer->__called,$expected);
83 function test_callbacks_on_update()
85 $expected = array ('beforeSave','beforeUpdate','afterUpdate','afterSave');
86 if (!AK_PHP5) $expected = array_map('strtolower',$expected);
88 $UpdateTest =& $this->TestCallback->find('first',array('name'=>'A Name'));
89 $UpdateTest->save(false);
91 $this->assertEqual($UpdateTest->__called,$expected);
92 $this->assertEqual($this->Observer->__called,$expected);
95 function test_callbacks_on_update_with_validation()
97 $expected = array ('beforeSave','beforeValidation','afterValidation','beforeValidationOnUpdate','afterValidationOnUpdate','beforeUpdate','afterUpdate','afterSave');
98 if (!AK_PHP5) $expected = array_map('strtolower',$expected);
100 $UpdateTest =& $this->TestCallback->find('first',array('name'=>'Another Name'));
101 $UpdateTest->save(true);
103 $this->assertEqual($UpdateTest->__called,$expected);
104 $this->assertEqual($this->Observer->__called,$expected);
107 function test_callbacks_on_destroy()
109 $expected = array('beforeDestroy','afterDestroy');
110 if (!AK_PHP5) $expected = array_map('strtolower',$expected);
112 $DestroyTest =& $this->TestCallback->find('first',array('name'=>'Another Name'));
113 $DestroyTest->destroy();
115 $this->assertEqual($DestroyTest->__called,$expected);
116 $this->assertEqual($this->Observer->__called,$expected);
120 ak_test('TestCase_AkActiveRecord_callbacks', true);