forked from code34/phpmyobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
696 lines (596 loc) · 18.2 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<?php
/**
* This file contains the test class used to check for PhpMyObject basic sanity.
*
* This file is part of the PhpMyObject project.
*
* For questions, help, comments, discussion, etc., please join our
* forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
* or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
*
* PhpMyProject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package PhpMyObject
* @author Nicolas Boiteux <[email protected]>
* @link http://pmo.developpez.com/
* @since PhpMyObject v0.1x
* @version $Revision:$
* @version $revision:$
* @version $Id:$
* @version $Id: $
* @version $id:$
* @version $id: $
* @copyright Copyright (C) 2007-2008 Nicolas Boiteux
* @license GPLv3 {@link http://www.gnu.org/licenses/gpl}
* @filesource
*/
/** requires the PMO_MyController class and the config file */
require_once(dirname(__FILE__).'/PMO_core/PMO_MyController.php');
require_once(dirname(__FILE__).'/your_config.php');
/**
* This class tests PhpMyObject basic sanity.
* @package PhpMyObject
*/
class test {
/**
* select de 5 enregistrements + fetch
*/
public function test1(){
echo "test 1 : select de 5 enregistrements + fetch<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id
limit 5;");
while ($result = $map->fetch()){
$test = $result['rental']->rental_id;
}
}
public function test2(){
echo "test 2 : factory d'un objet PMO_MyTable<br>";
$table = PMO_MyTable::factory('country');
if(get_class($table) == "PMO_MyTable_country")
echo("class is ok!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($table);print '</pre>';
}
/**
* test 3 : load object
*/
public function test3(){
echo "test 3 : load object<br>";
PMO_MyConfig::factory()->set('PMO_MyDbms.LOG', 'TRUE');
$object = PMO_MyObject::factory('inventory');
$object->inventory_id = 1;
$object->load();
print_r(PMO_MyDbms::factory()->getLog());
echo("<br>");
//print '<pre>';print_r($object);print '</pre>';
}
public function test4(){
echo "test 4 : select de 1 enregistrement<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$result = $map->fetch();
$rental = $result['rental'];
}
/**
* factory + load + save object
*/
public function test5(){
echo "test 5 : factory + load + save object<br>";
$object = PMO_MyObject::factory('inventory');
$object->inventory_id = 1;
$object->load();
$object->last_update = "2006-02-15 05:09:18";
$object->save();
}
/**
* toXML()
*/
public function test6(){
echo "test 6 : toXML()<br>";
$object = PMO_MyObject::factory('inventory');
$object->inventory_id = 1;
$object->load();
print_r($object->toXml("nothing")."<br>");
}
/**
* getTable
*/
public function test7(){
echo "test 7 : getTable<br>";
$object = PMO_MyObject::factory('inventory');
$table = $object->getTable();
if($table instanceof PMO_MyTable_inventory)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($object);print '</pre>';
}
/**
* getMapByTable
*/
public function test8(){
echo "test 8 : getMapByTable<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$newmap = $map->getMapByTable('rental');
if($newmap instanceof PMO_MyMap)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($newmap);print '</pre>';
}
/**
* getMapByValue
*/
public function test9(){
echo "test 9 : getMapByValue<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$newmap = $map->getMapByValue('rental', 'rental_id', 4863);
if($newmap instanceof PMO_MyMap)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($map);print '</pre>';
}
/**
* changement de driver pdo mysql on another base
*/
public function test10(){
echo "test 10 : changement de driver pdo on another base<br>";
$pdo = new PDO("mysql:host=localhost;dbname=test", "pmo", "pmo");
$controller = new PMO_MyController($pdo);
$map = $controller->query("SELECT * FROM address ;");
if($map instanceof PMO_MyMap)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($map);print '</pre>';
}
/**
* getObjectByObject
*/
public function test11(){
echo "test 11 : getObjectByObject<br>";
PMO_MyDbms::killInstance();
$config = PMO_MyConfig::factory();
$config->set('PMO_MyDbms.DRIVER','mysql');
$config->set('PMO_MyDbms.PDODRIVER','mysql');
$config->set('PMO_MyDbms.HOST','localhost');
$config->set('PMO_MyDbms.USER','pmo');
$config->set('PMO_MyDbms.PASS','pmo');
$config->set('PMO_MyDbms.BASE','sakila');
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$object = PMO_MyObject::factory('rental');
$object->rental_id = 4863;
$goodobject = $map->getObjectByObject($object);
if($goodobject instanceof PMO_MyObject)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($goodobject);print '</pre>';
}
/**
* getObjectByValue
*/
public function test12(){
echo "test 12 : getObjectByValue<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$object = $map->getObjectByValue('rental', 'rental_id', 4863);
if($object instanceof PMO_MyObject)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($object);print '</pre>';
}
/**
* getMapByObjectByValue
*/
public function test13(){
echo "test 13 : getMapByObjectByValue<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$object = PMO_MyObject::factory('rental');
$object->rental_id = 4863;
$newobject = $map->getMapByObjectByValue($object);
if($newobject instanceof PMO_MyMap)
echo("done!<br>");
else
die("Fail!<br>");
//print '<pre>';print_r($newobject);print '</pre>';
}
/**
* classloader
*/
public function test14(){
echo "test 14 : classloader<br>";
$object = PMO_MyObject::factory('film_actor');
if($object instanceof film_actor)
echo("class is ok!<br>");
else
die("Fail!<br>");
}
/**
* getperm + setperm
*/
public function test15(){
echo "test 15 : getperm + setperm<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id
limit 5;");
$result = $map->fetch();
$table = $result['rental']->getTable();
echo $table->getPerm('inventory_id');
$table->setPerm('inventory_id', 'rw');
echo($table->getPerm('inventory_id')."<br>");
}
/**
* select lazy loading
*/
public function test16(){
echo "test 16 : select lazy loading<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT rental_id,sum(customer_id),rental_date FROM rental GROUP BY customer_id ");
$result = $map->fetch();
if($result['collector']->get('sum(customer_id)'))
echo("Ok<br>");
else
die("Failed<br>");
//print '<pre>';print_r($result['collector']);print '</pre>';
}
/**
* save + delete
*/
public function test17(){
echo "test 17 : save + delete <br>";
PMO_MyDbms::killInstance();
$config = PMO_MyConfig::factory();
$config->set('PMO_MyDbms.DRIVER','mysql');
$config->set('PMO_MyDbms.PDODRIVER','mysql');
$config->set('PMO_MyDbms.HOST','localhost');
$config->set('PMO_MyDbms.USER','pmo');
$config->set('PMO_MyDbms.PASS','pmo');
$config->set('PMO_MyDbms.BASE','sakila');
$object = PMO_MyObject::factory('nico');
$object->nom = "ville";
$object->save();
$controller = new PMO_MyController();
$map = $controller->query("SELECT id,nom FROM nico LIMIT 1;");
$result = $map->fetch();
$object = $result['nico'];
$object->delete();
}
/**
* raw query
*/
public function test18(){
echo "test 18 : raw query<br>";
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "pmo", "pmo");
$controller = new PMO_MyController($pdo);
$map = $controller->rawquery("SELECT customer_id,rental_date FROM rental LIMIT 1;");
$result = $map->fetchArray();
if(is_array($result))
echo("done!<br>");
else
die("Fail!<br>");
//print '<pre>';print_r($result);print '</pre>';
}
/**
* insert with autoincrement
*/
public function test19(){
echo "test 19 verbose + autoincrement: <br/>";
PMO_MyConfig::factory()->set('PMO_MyDbms.LOG',TRUE);
$objet = PMO_MyObject::factory('nico');
$objet->nom = "toto";
$objet->save();
if(count(PMO_MyDbms::factory()->getLog() > 0)){
echo("done! </br>");
}else{
die("Fail! </br>");
}
//print '<pre>';print_r($result);print '</pre>';
}
/**
* select dans une table vide
*/
public function test20(){
echo "test 20 : select dans une table vide<br>";
$controller = new PMO_MyController();
try{
$pseudo = "wrongpseudo";
$map = $controller->query("SELECT * FROM testempty WHERE pseudo = '".$pseudo."' ;");
die("Fail!<br>");
}catch(Exception $e){
echo("done!<br>");
}
}
/**
* select de 1 enregistrement avec factory sans utiliser la primary key
*/
public function test21(){
echo "test 21 : select et creation d'un enregistrement aléatoire<br>";
$pseudo = "pseudo".rand(1,1000);
$objet = PMO_MyObject::factory('testempty');
$objet->pseudo = $pseudo;
try{
$objet->load();
}catch(Exception $e){
$objet->save();
}
}
/**
* factory de PMO_MyConfig et affichage des paramêtres de configuration
*/
public function test22(){
echo "test 22 : factory de PMO_MyConfig et affichage des paramêtres de configuration <br>";
$config = PMO_MyConfig::factory();
$config->show();
}
/**
* utilisation pdo externe Sqlite
*/
public function test23(){
echo "<br>test 23 : utilisation pdo externe Sqlite <br>";
$database = dirname(__FILE__).'/test2.db';
$pdo = new PDO("sqlite:".$database);
$controller = new PMO_MyController($pdo);
$map = $controller->query("SELECT * FROM test LIMIT 1 ;");
if($map instanceof PMO_MyMap)
echo("done!<br>");
else
die("Failed!<br>");
//print '<pre>';print_r($map);print '</pre>';
}
/**
* utilisation du transactionnel insert + save
*/
public function test24(){
echo "test 24 : utilisation du transactionnel insert <br/>";
PMO_MyDbms::killInstance();
$config = PMO_MyConfig::factory();
$config->set('PMO_MyDbms.DRIVER','pdo');
$config->set('PMO_MyDbms.PDODRIVER','mysql');
$config->set('PMO_MyDbms.HOST','localhost');
$config->set('PMO_MyDbms.USER','pmo');
$config->set('PMO_MyDbms.PASS','pmo');
$config->set('PMO_MyDbms.BASE','sakila');
$dbms = PMO_MyDbms::factory();
$dbms->beginTransaction();
$object = PMO_MyObject::factory('transaction');
$object->nom = "testrollback";
$object->save();
if($dbms->commit())
echo("done ! <br/>");
else
die("Failed ! <br/>");
}
/**
* suppresion d'une classe persistante PMO_MyTable et regénération
*/
public function test25(){
echo "test 25 : suppresion d'une classe persistante PMO_MyTable et regénération <br/>";
$filename = 'PMO_Core/PMO_MyTable/PMO_MyTable_nico.php';
if(file_exists($filename))
unlink($filename);
$controller = new PMO_MyController();
$map = $controller->query("SELECT id,nom FROM nico LIMIT 1;");
$result = $map->fetch();
$object = $result['nico'];
}
/**
* utilisation de PMO_MyRequest pour select sur 1 table
*/
public function test26(){
echo "test 26 : select sur film actor avec PMO_MyRequest <br/>";
$request = new PMO_MyRequest();
$request->from('film_actor')->limit('10');
$controller = new PMO_MyController();
$map = $controller->objectquery($request);
if($map instanceof PMO_MyMap)
echo("done!<br>");
else
die("Failed!<br>");
}
/**
* utilisation de PMO_MyRequest pour select sur plusieurs tables
*/
public function test27(){
echo "test 27 : select sur plusieurs tables avec PMO_MyRequest</br>";
$request = new PMO_MyRequest;
$request->field('last_name', 'first_name', 'title', 'film_actor.actor_id', 'film.film_id')
->from('actor', 'film', 'film_actor')
->where('actor.actor_id = film_actor.actor_id', 'film.film_id = film_actor.film_id')
->order('last_name', 'first_name', 'title')
->limit('10');
$controller = new PMO_MyController;
$map = $controller->objectquery($request);
if ($map instanceOf PMO_MyMap)
echo "done!<br>";
else
die("Failed!<br>");
}
/**
* utilisation de setFk + select sur plusieurs tables avec PMO_MyRequest
*/
public function test28(){
echo "test 28 : select sur plusieurs tables avec PMO_MyRequest</br>";
$table = PMO_MyTable::factory('film_actor');
$table->setFk("actor_id", array("actor"=>"actor_id"));
$table->setFk("film_id", array("film"=>"film_id"));
$request = new PMO_MyRequest;
$request->field('last_name', 'first_name', 'title', 'film_actor.actor_id', 'film.film_id')
->from('film_actor')
->getLinked()
->order('last_name', 'first_name', 'title')
->limit('10');
$controller = new PMO_MyController;
$map = $controller->objectquery($request);
if ($map instanceOf PMO_MyMap)
echo "done!<br>";
else
die("Failed!<br>");
}
/**
* count du nombre d'objets renvoyé dans la map
*/
public function test29(){
echo "test 29 count des rows<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 5;");
$count = 0;
while($result = $map->fetch()){
$count++;
}
if($count == 4)
die("failed ! </br>");
else
echo("ok! </br>");
//print '<pre>';print_r($newmap);print '</pre>';
}
/**
* fetch par Table
*/
public function test30(){
echo "test 30 fetch par table<br>";
$controller = new PMO_MyController();
$map = $controller->query("SELECT * FROM rental,inventory,customer,staff
WHERE rental.inventory_id=inventory.inventory_id
AND customer.customer_id=rental.customer_id
AND staff.staff_id=rental.staff_id limit 1;");
$count = 0;
while($table = $map->fetchTable('rental')){
if($table instanceof PMO_Object)
echo "Ok!</br>";
else
die("Failed!</br>");
}
//print '<pre>';print_r($map);print '</pre>';
}
/**
* test memcache
*/
public function test31(){
//echo("test 31: Memcache <br/>");
//$memcache = new Memcache;
//$memcache->connect('192.168.0.2', 11211) or die ("Connexion impossible");
//$object = PMO_MyObject::factory('film_actor');
//$object->actor_id = 1;
//$memcache->set('actor_id1', $object, false, 10) or die ("Echec de la sauvegarde des données sur le serveur");
//$object2 = $memcache->get('actor_id1');
//if($object2 instanceof film_actor)
// echo("Ok<br>");
//else
// die("Failed!<br>");
//$memcache->close();
}
/**
* test PMO_MyArray
*/
public function test32(){
$test = new PMO_MyArray();
$test->add('to', 'tooo');
$test->add('ti', 'tiiii');
echo("Test 32: PMO_MyArray foreach<br>");
foreach($test as $key=>$value)
if(is_string($value))
echo "Ok!<br/>";
else
die("Failed!<br/>");
echo("Test 32: PMO_MyArray while<br>");
while($result = $test->each())
if(is_array($result))
echo "Ok!<br/>";
else
die("Failed!<br/>");
echo("Test 32: PMO_MyArray fetch<br>");
while($result = $test->fetch())
if(is_string($value))
echo "Ok!<br/>";
else
die("Failed!<br/>");
}
}
// need to set this to true
$config = PMO_MyConfig::factory();
$config->set('PMO_MyTable.CLASS_WRITE_ON_DISK', TRUE);
$test = new test();
$time_start = microtime(true);
$test->test25();
$test->test1();
$test->test2();
$test->test3();
$test->test4();
$test->test5();
$test->test6();
$test->test7();
$test->test8();
$test->test9();
$test->test10();
$test->test11();
$test->test12();
$test->test13();
//$test->test14();
$test->test15();
$test->test16();
$test->test17();
$test->test18();
$test->test19();
$test->test20();
$test->test21();
$test->test22();
$test->test23();
$test->test24();
$test->test26();
$test->test27();
$test->test28();
$test->test29();
$test->test30();
$test->test31();
$test->test32();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did it in $time seconds<br>";
?>