KumbiaPHP beta2-dev
Framework PHP en español
active_record.php
Ir a la documentación de este archivo.
00001 <?php
00025         $config = Config::read('databases');
00026 
00027         $db = db::raw_connect();
00028 
00029         $db->debug = true;
00030         $total_time = 0;
00031 
00032         $test = true;
00033         $test_name = "CREAR Y BORRAR UNA TABLA";
00034         $init_time = $start_benchmark = microtime(true);
00035         try {
00036                 $value1 = $db->drop_table("kumbia_test");
00037                 if(!$value1){
00038                         throw new DbException("No se pudo crear la tabla de prueba (1)");
00039                 }
00040                 $value2 = $db->create_table("kumbia_test", array(
00041                         "id" => array(
00042                                 "type" => db::TYPE_INTEGER,
00043                                 "not_null" => true,
00044                                 "primary" => true,
00045                                 "auto" => true
00046                         ),
00047                         "texto" => array(
00048                                 "type" => db::TYPE_VARCHAR,
00049                                 "not_null" => true,
00050                                 "size" => 40
00051                         ),
00052                         "fecha" => array(
00053                                 "type" => db::TYPE_DATE,
00054                         ),
00055                         "email" => array(
00056                                 "type" => db::TYPE_VARCHAR,
00057                                 "size" => 70
00058                         ),
00059                         "numero" => array(
00060                                 "type" => db::TYPE_INTEGER,
00061                         )
00062                 ));
00063                 if($value2===false){
00064                         throw new DbException("No se pudo crear la tabla de prueba (2)");
00065                 }
00066                 if(!$db->table_exists("kumbia_test")){
00067                         throw new DbException("No se pudo comprobar la existencia de la tabla de prueba (3)");
00068                 }
00069 
00070                 //Crear modelo dinamicamente
00071                 eval("class KumbiaTest extends ActiveRecord {
00072 
00073                         function __construct(){
00074                                 \$this->validates_numericality_of('numero');
00075                                 \$this->validates_presence_of('numero');
00076                                 \$this->validates_email_in('email');
00077                                 \$this->validates_date_in('fecha');
00078                                 \$this->validates_uniqueness_of('texto');
00079                         }
00080 
00081                 } ");
00082                 unset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']]["kumbia_test"]);
00083                 $model = new KumbiaTest();
00084                 if(!is_subclass_of($model, "ActiveRecord")){
00085                         throw new DbException("No se pudo crear el modelo de prueba (3)");
00086                 }
00087 
00088         }
00089         catch(Exception $e){
00090                 $test = false;
00091                 print "<div style='background:#FFBBBB;border:1px solid red'>";
00092                 print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
00093                 print "</div>";
00094         }
00095         if($test){
00096                 $end_benckmark = microtime(true) - $start_benchmark;
00097                 print "<div style='background:#CCFF99;border:1px solid green'>";
00098                 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00099                 print "</div>";
00100         }
00101 
00102 
00103         $test = true;
00104         $test_name = "INSERTAR DATOS DE PRUEBA EN EL MODELO";
00105         $init_time = $start_benchmark = microtime(true);
00106         try {
00107                 $model->debug = true;
00108                 for($i=1;$i<=20;$i++){
00109                         $model->texto = "Texto ".$i;
00110                         $model->fecha = "2007-02-".sprintf("%02d", rand(1, 10));
00111                         $model->email = "kumbia@com";
00112                         $model->numero = rand(0, 5);
00113                         $model->create();
00114                 }
00115         }
00116         catch(Exception $e){
00117                 $test = false;
00118                 print "<div style='background:#FFBBBB;border:1px solid red'>";
00119                 print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
00120                 print "</div>";
00121         }
00122         if($test){
00123                 $end_benckmark = microtime(true) - $start_benchmark;
00124                 print "<div style='background:#CCFF99;border:1px solid green'>";
00125                 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00126                 print "</div>";
00127         }
00128 
00129         $test = true;
00130         $test_name = "ACTUALIZAR DATOS DE PRUEBA EN EL MODELO";
00131         $start_benchmark = microtime(true);
00132         try {
00133                 for($i=1;$i<=20;$i+=5){
00134                         $model = $model->find($i);
00135                         if($model){
00136                                 $model->numero = "100";
00137                                 $model->update();
00138                         } else {
00139                                 throw new DbException("No Devolvio el objeto para id = $i");
00140                         }
00141                 }
00142                 $model->update_all("email = 'hello@com'");
00143                 $model->update_all("texto = 'otro texto'", "id <= 10");
00144         }
00145         catch(Exception $e){
00146                 $test = false;
00147                 print "<div style='background:#FFBBBB;border:1px solid red'>";
00148                 print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
00149                 print "</div>";
00150         }
00151         if($test){
00152                 $end_benckmark = microtime(true) - $start_benchmark;
00153                 print "<div style='background:#CCFF99;border:1px solid green'>";
00154                 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00155                 print "</div>";
00156         }
00157 
00158 
00159         $test = true;
00160         $test_name = "CONSULTAR DATOS DE PRUEBA EN EL MODELO";
00161         $start_benchmark = microtime(true);
00162         try {
00163                 $model = new KumbiaTest();
00164                 $model->debug = true;
00165                 $model->find();
00166                 if($model->count!=20){
00167                         throw new DbException("No devolvio el numero correcto de registros en la tabla (1)");
00168                 }
00169                 $model->find_first(11);
00170                 if($model->numero!=100){
00171                         throw new DbException("No devolvio el registro correcto para id = 11 (2)");
00172                 }
00173                 $otro_model = $model->find_first(11);
00174                 if($otro_model->numero!=100){
00175                         throw new DbException("No devolvio el registro correcto para id = 11 (3)");
00176                 }
00177                 $model->find("numero = 100");
00178                 if($model->count!=4){
00179                         throw new DbException("No devolvio el numero correcto de registros en la tabla (4)");
00180                 }
00181                 $results = $model->find("numero = 100", "order: id desc");
00182                 if($results[0]->id!=16){
00183                         throw new DbException("No devolvio el registro correcto al ordenar (5)");
00184                 }
00185                 if(count($results)!=4){
00186                         throw new DbException("No devolvio el numero de registros correcto al ordenar (6)");
00187                 }
00188                 $results = $model->find("conditions: numero = 100", "limit: 1", "order: id asc");
00189                 if(count($results)!=1){
00190                         throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento (7)");
00191                 }
00192                 if($results[0]->id!=1){
00193                         throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento {$results[0]->id} (8)");
00194                 }
00195                 $min = $model->minimum("id", "conditions: numero = 100");
00196                 if($min!=1){
00197                         throw new DbException("No devolvio el minimum correcto (9)");
00198                 }
00199                 $max = $model->maximum("id", "conditions: numero = 100");
00200                 if($max!=16){
00201                         throw new DbException("No devolvio el maximum correcto (10)");
00202                 }
00203                 $sum = $model->sum("id", "conditions: numero = 100");
00204                 if($sum!=34){
00205                         throw new DbException("No devolvio el sum correcto (11)");
00206                 }
00207                 $avg = $model->average("id", "conditions: numero = 100");
00208                 if($avg!=8.5){
00209                         throw new DbException("No devolvio el avg correcto (12)");
00210                 }
00211                 $model->find_first("numero = 100");
00212                 if($model->id!=1){
00213                         throw new DbException("find_first con condicion fallo (13)");
00214                 }
00215                 $model->find_first(15);
00216                 if($model->id!=15){
00217                         throw new DbException("find_first a llave primaria (14)");
00218                 }
00219                 $model2 = $model->find_first("id > 10");
00220                 if($model2->id!=11){
00221                         throw new DbException("find_first a condicion (15)");
00222                 }
00223                 if($model->count()!=20){
00224                         throw new DbException("count sin parametros (16)");
00225                 }
00226                 if($model->count("numero = 100")!=4){
00227                         throw new DbException("count con parametros (17)");
00228                 }
00229                 if(count($model->distinct("id", "conditions: numero = 100"))!=4){
00230                         throw new DbException("fallo distinct (18)");
00231                 }
00232                 $rows = $model->find_all_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 14 ORDER BY 1");
00233                 if($rows[0]->id!=12){
00234                         throw new DbException("fallo find_all_by_sql (19)");
00235                 }
00236                 $row = $model->find_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 13 ORDER BY 1");
00237                 if($row->id!=12){
00238                         throw new DbException("fallo find_by_sql (20)");
00239                 }
00240                 if(count($model->find_all_by_numero(100))!=4){
00241                         throw new DbException("fallo find_all_by_numero (21)");
00242                 }
00243                 $model->find_by_id(16);
00244                 if($model->id!=16){
00245                         throw new DbException("fallo find_by_id (22)");
00246                 }
00247                 $num = $model->count_by_numero(100);
00248                 if($model->id!=16){
00249                         throw new DbException("fallo find_by_id (22)");
00250                 }
00251         }
00252         catch(Exception $e){
00253                 $test = false;
00254                 print "<div style='background:#FFBBBB;border:1px solid red'>";
00255                 print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
00256                 print "</div>";
00257                 return;
00258         }
00259         if($test){
00260                 $end_benckmark = microtime(true) - $start_benchmark;
00261                 print "<div style='background:#CCFF99;border:1px solid green'>";
00262                 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00263                 print "</div>";
00264         }
00265 
00266         $test = true;
00267         $test_name = "ELIMINAR REGISTROS DE PRUEBA EN EL MODELO";
00268         $start_benchmark = microtime(true);
00269         try {
00270                 $model->delete(18);
00271                 $model->delete_all("id < 10");
00272                 $model->delete_all();
00273         }
00274         catch(Exception $e){
00275                 $test = false;
00276                 print "<div style='background:#FFBBBB;border:1px solid red'>";
00277                 print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
00278                 print "</div>";
00279         }
00280         if($test){
00281                 $end_benckmark = microtime(true) - $start_benchmark;
00282                 print "<div style='background:#CCFF99;border:1px solid green'>";
00283                 print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
00284                 print "</div>";
00285         }
00286 
00287 
00288         print "<div style='background:#CCFF99;border:1px solid green'>";
00289         print "<strong>Tiempo total de los Test ".(microtime(true) - $init_time)."</strong>";
00290         print "</div>";
00291 
00292 ?>
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones