KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
adapters.php
Ir a la documentación de este archivo.
1 <?php
25  $config = Config::read('databases');
26 
27  $user = "root";
28  $password = "hea101";
29  if(isset($config->database->pdo)){
30  $dsn = $config->database->dsn;
31  $db = new Db($dsn, $user, $password);
32  } else {
33  $host = "localhost";
34  $dbname = "test";
35  $db = new Db($host, $user, $password, $dbname);
36  }
37 
38 
39  $db->debug = true;
41 
42  $test = true;
43  $test_name = "CREAR Y BORRAR UNA TABLA";
44  $init_time = $start_benchmark = microtime(true);
45  try {
46  $value1 = $db->drop_table("kumbia_test");
47  if(!$value1){
48  throw new DbException("No se pudo crear la tabla de prueba (1)");
49  }
50  $value2 = $db->create_table("test.kumbia_test", array(
51  "id" => array(
52  "type" => db::TYPE_INTEGER,
53  "not_null" => true,
54  "primary" => true,
55  "auto" => true
56  ),
57  "texto" => array(
58  "type" => db::TYPE_VARCHAR,
59  "not_null" => true,
60  "size" => 40
61  ),
62  "fecha" => array(
63  "type" => db::TYPE_DATE,
64  )
65  ));
66  if($value2===false){
67  throw new DbException("No se pudo crear la tabla de prueba (2)");
68  }
69  if(!$db->table_exists("kumbia_test")){
70  throw new DbException("No se pudo comprobar la existencia de la tabla de prueba (3)");
71  }
72  }
73  catch(Exception $e){
74  $test = false;
75  print "<div style='background:#FFBBBB;border:1px solid red'>";
76  print "Primer Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
77  print "</div>";
78  }
79  if($test){
80  $end_benckmark = microtime(true) - $start_benchmark;
81  print "<div style='background:#CCFF99;border:1px solid green'>";
82  print "Primer Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
83  print "</div>";
84  }
85 
86  $test = true;
87  $test_name = "INSERTAR DATOS EN UNA TABLA DE PRUEBA";
88  $start_benchmark = microtime(true);
89  try {
90  $value1 = $db->insert("kumbia_test", array("4", "'Hello'", "'2005-04-04'"));
91  if(!$value1){
92  throw new DbException("No se puede insertar en la tabla de prueba (1)");
93  }
94  $value2 = $db->insert("kumbia_test", array("2", "'Hello'", "'2005-02-04'"), array("id", "texto", "fecha"));
95  if(!$value2){
96  throw new DbException("No se puede insertar en la tabla de prueba (2)");
97  }
98  $value3 = $db->insert("kumbia_test", array("'Hello'", "'2005-02-04'"), array("texto", "fecha"));
99  if(!$value3){
100  throw new DbException("No se puede insertar en la tabla de prueba (3)");
101  }
102  if($db->affected_rows()!=1){
103  throw new DbException("No se puede insertar en la tabla de prueba (4)");
104  }
105  Flash::notice($db->last_insert_id("kumbia_test", "id"));
106  }
107  catch(Exception $e){
108  $test = false;
109  print "<div style='background:#FFBBBB;border:1px solid red'>";
110  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
111  print "</div>";
112  }
113  if($test){
114  $end_benckmark = microtime(true) - $start_benchmark;
115  print "<div style='background:#CCFF99;border:1px solid green'>";
116  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
117  print "</div>";
118  }
119 
120  $test = true;
121  $test_name = "ACTUALIZAR DATOS EN UNA TABLA DE PRUEBA";
122  $start_benchmark = microtime(true);
123  try {
124  $value1 = $db->update("kumbia_test", array("texto"), array("'Esto es un Texto'"));
125  if(!$value1){
126  throw new DbException("No se puede actualizar en la tabla de prueba (1)");
127  }
128  $value2 = $db->update("kumbia_test", array("texto", "fecha"), array("'Esto es otro Texto'", "'2007-02-02'"), "id = 1");
129  if($value2===false){
130  throw new DbException("No se puede actualizar en la tabla de prueba (2)");
131  }
132  }
133  catch(Exception $e){
134  print "<div style='background:#FFBBBB;border:1px solid red'>";
135  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
136  print "</div>";
137  }
138  if($test){
139  $end_benckmark = microtime(true) - $start_benchmark;
140  print "<div style='background:#CCFF99;border:1px solid green'>";
141  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
142  print "</div>";
143  }
144 
145  $test = true;
146  $test_name = "BORRAR DATOS EN UNA TABLA DE PRUEBA";
147  $start_benchmark = microtime(true);
148  try {
149  $value1 = $db->delete("kumbia_test", "id = 4");
150  if(!$value1){
151  throw new DbException("No se puede borrar en la tabla de prueba (1)");
152  }
153  }
154  catch(Exception $e){
155  print "<div style='background:#FFBBBB;border:1px solid red'>";
156  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
157  print "</div>";
158  }
159  if($test){
160  $end_benckmark = microtime(true) - $start_benchmark;
161  print "<div style='background:#CCFF99;border:1px solid green'>";
162  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
163  print "</div>";
164  }
165 
166  $test = true;
167  $test_name = "CONSULTAR DATOS EN UNA TABLA DE PRUEBA";
168  $start_benchmark = microtime(true);
169  try {
170  $value1 = $db->query("SELECT * FROM kumbia_test ORDER BY id");
171  if(!$value1){
172  throw new DbException("No se puede consultar en la tabla de prueba (1)");
173  }
174  while($row = $db->fetch_array()){
175  if($row['id']!=0 && $row['id']!=1 && $row['id']!=2 && $row['id']!=4 && $row['id']!=5){
176  throw new DbException("No se puede consultar en la tabla de prueba {$row['id']} (2)");
177  }
178  }
179  if(!isset($config->database->pdo)){
180  if($db->num_rows()!=2){
181  throw new DbException("No se puede consultar en la tabla de prueba (3)");
182  }
183  }
184  $value2 = $db->fetch_one("SELECT * FROM kumbia_test {$db->limit(1)}");
185  if(!is_array($value2)){
186  throw new DbException("No se puede consultar en la tabla de prueba (4)");
187  }
188  $value3 = $db->fetch_all("SELECT * FROM kumbia_test");
189  if(count($value3)!=2){
190  throw new DbException("No se puede consultar en la tabla de prueba (5)");
191  }
192  $value4 = $db->in_query_assoc("SELECT * FROM kumbia_test {$db->limit(1)}");
193  if(count($value4[0])!=3){
194  throw new DbException("No se puede consultar en la tabla de prueba (6)");
195  }
196  $value5 = $db->in_query_num("SELECT * FROM kumbia_test {$db->limit(1)}");
197  if(count($value5[0])!=3){
198  throw new DbException("No se puede consultar en la tabla de prueba (7)");
199  }
200  $value6 = $db->in_query("SELECT * FROM kumbia_test {$db->limit(1)}");
201  if(count($value6[0])!=6){
202  throw new DbException("No se puede consultar en la tabla de prueba (8)");
203  }
204  if(!isset($config->database->pdo)){
205  $value7 = $db->data_seek(1, $value1);
206  if(!$value7){
207  throw new DbException("No se puede consultar en la tabla de prueba (9)");
208  }
209  }
210  $value8 = $db->fetch_array($value1);
211  if($value8['id']!=5 && $value8['id']!=2 && $value8['id']!=0){
212  throw new DbException("No se puede consultar en la tabla de prueba {$value8['id']} (10)");
213  }
214  }
215  catch(Exception $e){
216  print "<div style='background:#FFBBBB;border:1px solid red'>";
217  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
218  print "</div>";
219  }
220  if($test){
221  $end_benckmark = microtime(true) - $start_benchmark;
222  print "<div style='background:#CCFF99;border:1px solid green'>";
223  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
224  print "</div>";
225  }
226 
227  $test = true;
228  $test_name = "COMPROBAR NOMBRE DE CAMPO";
229  $start_benchmark = microtime(true);
230  try {
231  $value1 = $db->query("SELECT * FROM kumbia_test");
232  if(!$value1){
233  throw new DbException("No se comprobar nombre del campo (1)");
234  }
235  $value2 = $db->field_name(1);
236  if($value2!='texto'){
237  throw new DbException("No se comprobar nombre del campo (2)");
238  }
239  }
240  catch(Exception $e){
241  print "<div style='background:#FFBBBB;border:1px solid red'>";
242  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
243  print "</div>";
244  }
245  if($test){
246  $end_benckmark = microtime(true) - $start_benchmark;
247  print "<div style='background:#CCFF99;border:1px solid green'>";
248  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
249  print "</div>";
250  }
251 
252  $test = true;
253  $test_name = "INSTRUCCIONES TRANSACCIONALES";
254  $start_benchmark = microtime(true);
255  try {
256  $db->begin();
257  $db->query("delete from kumbia_test");
258  $db->rollback();
259  $db->begin();
260  $db->query("delete from kumbia_test");
261  $db->commit();
262  }
263  catch(Exception $e){
264  print "<div style='background:#FFBBBB;border:1px solid red'>";
265  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
266  print "</div>";
267  }
268  if($test){
269  $end_benckmark = microtime(true) - $start_benchmark;
270  print "<div style='background:#CCFF99;border:1px solid green'>";
271  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
272  print "</div>";
273  }
274 
275  $test = true;
276  $test_name = "CONTAR TABLAS Y DESCRIBIR TABLA TEST";
277  $start_benchmark = microtime(true);
278  try {
279  Flash::notice("HAY ".count($db->list_tables())." TABLA(S) EN LA BASE DE DATOS");
280  print_r($db->describe_table("kumbia_test"));
281  }
282  catch(Exception $e){
283  print "<div style='background:#FFBBBB;border:1px solid red'>";
284  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
285  print "</div>";
286  }
287  if($test){
288  $end_benckmark = microtime(true) - $start_benchmark;
289  print "<div style='background:#CCFF99;border:1px solid green'>";
290  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
291  print "</div>";
292  }
293 
294  $test = true;
295  $test_name = "CERRAR LA CONEXION A LA BASE DE DATOS";
296  $start_benchmark = microtime(true);
297  try {
298  $value1 = $db->close();
299  if(!$value1){
300  throw new DbException("No se puede cerrar la conexion (1)");
301  }
302  }
303  catch(Exception $e){
304  print "<div style='background:#FFBBBB;border:1px solid red'>";
305  print "Test '$test_name' (FALL&Oacute;) con mensaje: ({$e->getMessage()})";
306  print "</div>";
307  }
308  if($test){
309  $end_benckmark = microtime(true) - $start_benchmark;
310  print "<div style='background:#CCFF99;border:1px solid green'>";
311  print "Test '$test_name' (OK) con tiempo: ({$end_benckmark})";
312  print "</div>";
313  }
314 
315 
316  print "<div style='background:#CCFF99;border:1px solid green'>";
317  print "<strong>Tiempo total de los Test ".(microtime(true) - $init_time)."</strong>";
318  print "</div>";
319 
320 
321 ?>