. * * @package PhpMyObject * @author Nicolas Boiteux * @link http://pmo.developpez.com/ * @since PhpMyObject v0.1x * @version $Revision: $ * @copyright Copyright (C) 2007-2008 Nicolas Boiteux * @license GPLv3 {@link http://www.gnu.org/licenses/gpl} * @filesource * @deprecated This will be replaced by the PMO_tests. */ /** 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
"; $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
"; $table = PMO_MyTable::factory('country'); if(get_class($table) == "PMO_MyTable_country") echo("class is ok!
"); else echo("Failed!
"); //print '
';print_r($table);print '
'; } /** * test 3 : load object */ public function test3(){ echo "test 3 : load object
"; 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("
"); //print '
';print_r($object);print '
'; } public function test4(){ echo "test 4 : select de 1 enregistrement
"; $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
"; $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()
"; $object = PMO_MyObject::factory('inventory'); $object->inventory_id = 1; $object->load(); print_r($object->toXml("nothing")."
"); } /** * getTable */ public function test7(){ echo "test 7 : getTable
"; $object = PMO_MyObject::factory('inventory'); $table = $object->getTable(); if($table instanceof PMO_MyTable_inventory) echo("done!
"); else echo("Failed!
"); //print '
';print_r($object);print '
'; } /** * getMapByTable */ public function test8(){ echo "test 8 : getMapByTable
"; $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!
"); else echo("Failed!
"); //print '
';print_r($newmap);print '
'; } /** * getMapByValue */ public function test9(){ echo "test 9 : getMapByValue
"; $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!
"); else echo("Failed!
"); //print '
';print_r($map);print '
'; } /** * changement de driver pdo mysql on another base */ public function test10(){ echo "test 10 : changement de driver pdo on another base
"; $pdo = new PDO("mysql:host=localhost;dbname=sakila", "pmo", "pmo"); $controller = new PMO_MyController($pdo); $map = $controller->query("SELECT * FROM address ;"); if($map instanceof PMO_MyMap) echo("done!
"); else echo("Failed!
"); //print '
';print_r($map);print '
'; } /** * getObjectByObject */ public function test11(){ echo "test 11 : getObjectByObject
"; 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!
"); else echo("Failed!
"); //print '
';print_r($goodobject);print '
'; } /** * getObjectByValue */ public function test12(){ echo "test 12 : getObjectByValue
"; $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!
"); else echo("Failed!
"); //print '
';print_r($object);print '
'; } /** * getMapByObjectByValue */ public function test13(){ echo "test 13 : getMapByObjectByValue
"; $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!
"); else echo("Fail!
"); //print '
';print_r($newobject);print '
'; } /** * classloader */ public function test14(){ echo "test 14 : classloader
"; $object = PMO_MyObject::factory('film_actor'); if($object instanceof film_actor) echo("class is ok!
"); else echo("Fail!
"); } /** * getperm + setperm */ public function test15(){ echo "test 15 : getperm + setperm
"; $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')."
"); } /** * select lazy loading */ public function test16(){ echo "test 16 : select lazy loading
"; $controller = new PMO_MyController(); $map = $controller->query("SELECT rental_id,sum(customer_id),rental_date FROM rental GROUP BY customer_id LIMIT 3"); echo "
".print_r($map,true)."
"; $result = $map->fetch(); if($result['collector']->get('sum(customer_id)')) echo("Ok
"); else echo("Failed
"); //print '
';print_r($result['collector']);print '
'; } /** * save + delete */ public function test17(){ echo "test 17 : save + delete
"; 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
"; $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!
"); else echo("Fail!
"); //print '
';print_r($result);print '
'; } /** * insert with autoincrement */ public function test19(){ echo "test 19 verbose + autoincrement:
"; 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!
"); }else{ echo("Fail!
"); } //print '
';print_r($result);print '
'; } /** * select dans une table vide */ public function test20(){ echo "test 20 : select dans une table vide
"; $controller = new PMO_MyController(); try{ $pseudo = "wrongpseudo"; $map = $controller->query("SELECT * FROM testempty WHERE pseudo = '".$pseudo."' ;"); echo("Fail!
"); }catch(Exception $e){ echo("done!
"); } } /** * 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
"; $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
"; $config = PMO_MyConfig::factory(); $config->show(); } /** * utilisation pdo externe Sqlite */ public function test23(){ echo "
test 23 : utilisation pdo externe Sqlite
"; $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!
"); else echo("Failed!
"); //print '
';print_r($map);print '
'; } /** * utilisation du transactionnel insert + save */ public function test24(){ echo "test 24 : utilisation du transactionnel insert
"; 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 !
"); else echo("Failed !
"); } /** * 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
"; $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
"; $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!
"); else echo("Failed!
"); } /** * utilisation de PMO_MyRequest pour select sur plusieurs tables */ public function test27(){ echo "test 27 : select sur plusieurs tables avec PMO_MyRequest
"; $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!
"; else echo("Failed!
"); } /** * utilisation de setFk + select sur plusieurs tables avec PMO_MyRequest */ public function test28(){ echo "test 28 : select sur plusieurs tables avec PMO_MyRequest
"; $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!
"; else echo("Failed!
"); } /** * count du nombre d'objets renvoyé dans la map */ public function test29(){ echo "test 29 count des rows
"; $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) echo("failed !
"); else echo("ok!
"); //print '
';print_r($newmap);print '
'; } /** * fetch par Table */ public function test30(){ echo "test 30 fetch par table
"; $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!
"; else echo("Failed!
"); } //print '
';print_r($map);print '
'; } /** * test memcache */ public function test31(){ //echo("test 31: Memcache
"); //$memcache = new Memcache; //$memcache->connect('192.168.0.2', 11211) or echo ("Connexion impossible"); //$object = PMO_MyObject::factory('film_actor'); //$object->actor_id = 1; //$memcache->set('actor_id1', $object, false, 10) or echo ("Echec de la sauvegarde des données sur le serveur"); //$object2 = $memcache->get('actor_id1'); //if($object2 instanceof film_actor) // echo("Ok
"); //else // echo("Failed!
"); //$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
"); foreach($test as $key=>$value) if(is_string($value)) echo "Ok!
"; else echo("Failed!
"); echo("Test 32: PMO_MyArray while
"); while($result = $test->each()) if(is_array($result)) echo "Ok!
"; else echo("Failed!
"); echo("Test 32: PMO_MyArray fetch
"); while($result = $test->fetch()) if(is_string($value)) echo "Ok!
"; else echo("Failed!
"); } public function test33() { echo "test 33: select de deux colonnes
"; $sql = 'SELECT first_name,last_name'; $sql .= ' FROM customer'; $sql .= ' WHERE store_id = 2'; $c = new PMO_MyController; $m = $c->query($sql); if ($map instanceOf PMO_MyMap) echo "done!
"; else echo("Failed!
"); } public function test34() { echo "test 34: select de deux fonctions
"; $sql = 'SELECT first_name,last_name,min(create_date),max(last_update)'; $sql .= ' FROM customer'; $sql .= ' WHERE store_id = 2'; $sql .= ' GROUP BY first_name,last_name'; $c = new PMO_MyController; try { $m = $c->query($sql); if ($map instanceOf PMO_MyMap) echo "done!
"; else echo("Failed!
"); } catch(Exception $e) { echo "Fail! Ce select génère une erreur: ".$e->getMessage()."
"; } } } // 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->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->test25(); $test->test26(); $test->test27(); $test->test28(); $test->test29(); $test->test30(); $test->test31(); $test->test32(); $test->test33(); $test->test34(); $time_end = microtime(true); $time = $time_end - $time_start; echo "

Did it in $time seconds

"; ?>