KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
active_record.php
Ir a la documentación de este archivo.
1 <?php
25  $config = Config::read('databases');
26 
27  $db = db::raw_connect();
28 
29  $db->debug = true;
31 
32  $test = true;
33  $test_name = "CREAR Y BORRAR UNA TABLA";
34  $init_time = $start_benchmark = microtime(true);
35  try {
36  $value1 = $db->drop_table("kumbia_test");
37  if(!$value1){
38  throw new DbException("No se pudo crear la tabla de prueba (1)");
39  }
40  $value2 = $db->create_table("kumbia_test", array(
41  "id" => array(
42  "type" => db::TYPE_INTEGER,
43  "not_null" => true,
44  "primary" => true,
45  "auto" => true
46  ),
47  "texto" => array(
48  "type" => db::TYPE_VARCHAR,
49  "not_null" => true,
50  "size" => 40
51  ),
52  "fecha" => array(
53  "type" => db::TYPE_DATE,
54  ),
55  "email" => array(
56  "type" => db::TYPE_VARCHAR,
57  "size" => 70
58  ),
59  "numero" => array(
60  "type" => db::TYPE_INTEGER,
61  )
62  ));
63  if($value2===false){
64  throw new DbException("No se pudo crear la tabla de prueba (2)");
65  }
66  if(!$db->table_exists("kumbia_test")){
67  throw new DbException("No se pudo comprobar la existencia de la tabla de prueba (3)");
68  }
69 
70  //Crear modelo dinamicamente
71  eval("class KumbiaTest extends ActiveRecord {
72 
73  function __construct(){
74  \$this->validates_numericality_of('numero');
75  \$this->validates_presence_of('numero');
76  \$this->validates_email_in('email');
77  \$this->validates_date_in('fecha');
78  \$this->validates_uniqueness_of('texto');
79  }
80 
81  } ");
82  unset($_SESSION['KUMBIA_META_DATA'][$_SESSION['KUMBIA_PATH']]["kumbia_test"]);
83  $model = new KumbiaTest();
84  if(!is_subclass_of($model, "ActiveRecord")){
85  throw new DbException("No se pudo crear el modelo de prueba (3)");
86  }
87 
88  }
89  catch(Exception $e){
90  $test = false;
91  print "<div style='background:#FFBBBB;border:1px solid red'>";
92  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
93  print "</div>";
94  }
95  if($test){
96  $end_benckmark = microtime(true) - $start_benchmark;
97  print "<div style='background:#CCFF99;border:1px solid green'>";
98  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
99  print "</div>";
100  }
101 
102 
103  $test = true;
104  $test_name = "INSERTAR DATOS DE PRUEBA EN EL MODELO";
105  $init_time = $start_benchmark = microtime(true);
106  try {
107  $model->debug = true;
108  for($i=1;$i<=20;$i++){
109  $model->texto = "Texto ".$i;
110  $model->fecha = "2007-02-".sprintf("%02d", rand(1, 10));
111  $model->email = "kumbia@com";
112  $model->numero = rand(0, 5);
113  $model->create();
114  }
115  }
116  catch(Exception $e){
117  $test = false;
118  print "<div style='background:#FFBBBB;border:1px solid red'>";
119  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
120  print "</div>";
121  }
122  if($test){
123  $end_benckmark = microtime(true) - $start_benchmark;
124  print "<div style='background:#CCFF99;border:1px solid green'>";
125  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
126  print "</div>";
127  }
128 
129  $test = true;
130  $test_name = "ACTUALIZAR DATOS DE PRUEBA EN EL MODELO";
131  $start_benchmark = microtime(true);
132  try {
133  for($i=1;$i<=20;$i+=5){
134  $model = $model->find($i);
135  if($model){
136  $model->numero = "100";
137  $model->update();
138  } else {
139  throw new DbException("No Devolvio el objeto para id = $i");
140  }
141  }
142  $model->update_all("email = 'hello@com'");
143  $model->update_all("texto = 'otro texto'", "id <= 10");
144  }
145  catch(Exception $e){
146  $test = false;
147  print "<div style='background:#FFBBBB;border:1px solid red'>";
148  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
149  print "</div>";
150  }
151  if($test){
152  $end_benckmark = microtime(true) - $start_benchmark;
153  print "<div style='background:#CCFF99;border:1px solid green'>";
154  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
155  print "</div>";
156  }
157 
158 
159  $test = true;
160  $test_name = "CONSULTAR DATOS DE PRUEBA EN EL MODELO";
161  $start_benchmark = microtime(true);
162  try {
163  $model = new KumbiaTest();
164  $model->debug = true;
165  $model->find();
166  if($model->count!=20){
167  throw new DbException("No devolvio el numero correcto de registros en la tabla (1)");
168  }
169  $model->find_first(11);
170  if($model->numero!=100){
171  throw new DbException("No devolvio el registro correcto para id = 11 (2)");
172  }
173  $otro_model = $model->find_first(11);
174  if($otro_model->numero!=100){
175  throw new DbException("No devolvio el registro correcto para id = 11 (3)");
176  }
177  $model->find("numero = 100");
178  if($model->count!=4){
179  throw new DbException("No devolvio el numero correcto de registros en la tabla (4)");
180  }
181  $results = $model->find("numero = 100", "order: id desc");
182  if($results[0]->id!=16){
183  throw new DbException("No devolvio el registro correcto al ordenar (5)");
184  }
185  if(count($results)!=4){
186  throw new DbException("No devolvio el numero de registros correcto al ordenar (6)");
187  }
188  $results = $model->find("conditions: numero = 100", "limit: 1", "order: id asc");
189  if(count($results)!=1){
190  throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento (7)");
191  }
192  if($results[0]->id!=1){
193  throw new DbException("No devolvio el registro correcto cuando se uso limit y ordenamiento {$results[0]->id} (8)");
194  }
195  $min = $model->minimum("id", "conditions: numero = 100");
196  if($min!=1){
197  throw new DbException("No devolvio el minimum correcto (9)");
198  }
199  $max = $model->maximum("id", "conditions: numero = 100");
200  if($max!=16){
201  throw new DbException("No devolvio el maximum correcto (10)");
202  }
203  $sum = $model->sum("id", "conditions: numero = 100");
204  if($sum!=34){
205  throw new DbException("No devolvio el sum correcto (11)");
206  }
207  $avg = $model->average("id", "conditions: numero = 100");
208  if($avg!=8.5){
209  throw new DbException("No devolvio el avg correcto (12)");
210  }
211  $model->find_first("numero = 100");
212  if($model->id!=1){
213  throw new DbException("find_first con condicion fallo (13)");
214  }
215  $model->find_first(15);
216  if($model->id!=15){
217  throw new DbException("find_first a llave primaria (14)");
218  }
219  $model2 = $model->find_first("id > 10");
220  if($model2->id!=11){
221  throw new DbException("find_first a condicion (15)");
222  }
223  if($model->count()!=20){
224  throw new DbException("count sin parametros (16)");
225  }
226  if($model->count("numero = 100")!=4){
227  throw new DbException("count con parametros (17)");
228  }
229  if(count($model->distinct("id", "conditions: numero = 100"))!=4){
230  throw new DbException("fallo distinct (18)");
231  }
232  $rows = $model->find_all_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 14 ORDER BY 1");
233  if($rows[0]->id!=12){
234  throw new DbException("fallo find_all_by_sql (19)");
235  }
236  $row = $model->find_by_sql("SELECT * FROM kumbia_test WHERE id > 11 AND id < 13 ORDER BY 1");
237  if($row->id!=12){
238  throw new DbException("fallo find_by_sql (20)");
239  }
240  if(count($model->find_all_by_numero(100))!=4){
241  throw new DbException("fallo find_all_by_numero (21)");
242  }
243  $model->find_by_id(16);
244  if($model->id!=16){
245  throw new DbException("fallo find_by_id (22)");
246  }
247  $num = $model->count_by_numero(100);
248  if($model->id!=16){
249  throw new DbException("fallo find_by_id (22)");
250  }
251  }
252  catch(Exception $e){
253  $test = false;
254  print "<div style='background:#FFBBBB;border:1px solid red'>";
255  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
256  print "</div>";
257  return;
258  }
259  if($test){
260  $end_benckmark = microtime(true) - $start_benchmark;
261  print "<div style='background:#CCFF99;border:1px solid green'>";
262  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
263  print "</div>";
264  }
265 
266  $test = true;
267  $test_name = "ELIMINAR REGISTROS DE PRUEBA EN EL MODELO";
268  $start_benchmark = microtime(true);
269  try {
270  $model->delete(18);
271  $model->delete_all("id < 10");
272  $model->delete_all();
273  }
274  catch(Exception $e){
275  $test = false;
276  print "<div style='background:#FFBBBB;border:1px solid red'>";
277  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
278  print "</div>";
279  }
280  if($test){
281  $end_benckmark = microtime(true) - $start_benchmark;
282  print "<div style='background:#CCFF99;border:1px solid green'>";
283  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
284  print "</div>";
285  }
286 
287 
288  print "<div style='background:#CCFF99;border:1px solid green'>";
289  print "<strong>Tiempo total de los Test ".(microtime(true) - $init_time)."</strong>";
290  print "</div>";
291 
292 ?>