Ejemplo n.º 1
0
 public function testDummy()
 {
     // Test meta object
     $meta = DatabaseObjectMeta::forClass('DBODummy');
     $table = $meta->getTable();
     $key = $meta->getKey();
     $this->assertEqual('DBODummy', $meta->getClass());
     $this->assertEqual('dbodummy', $table);
     $this->assertEqual('dbodummy_id', $key);
     $colMap = $meta->getColumnMap();
     $this->assertEqual($colMap["aDate"], "a_date");
     $this->assertEqual($colMap["aDateTime"], "a_date_time");
     $this->assertEqual($colMap["aTime"], "a_time");
     $this->assertEqual($colMap["mess"], "mess");
     $this->assertEqual($colMap["id"], "dbodummy_id");
     $fieldSet = $meta->getFieldSetSQL();
     $this->assertEqual($fieldSet, implode(', ', array('`a_date`=FROM_UNIXTIME(:a_date)', '`a_date_time`=FROM_UNIXTIME(:a_date_time)', '`a_time`=SEC_TO_TIME(:a_time)', '`mess`=:mess')));
     $pfx = "`{$table}`.";
     $colSQL = $meta->getColumnsSQL();
     $this->assertEqual($colSQL, implode(', ', array("UNIX_TIMESTAMP({$pfx}`a_date`) AS `a_date`", "UNIX_TIMESTAMP({$pfx}`a_date_time`) AS `a_date_time`", "TIME_TO_SEC({$pfx}`a_time`) AS `a_time`", "{$pfx}`mess` AS `mess`")));
     $colSQL = $meta->getColumnsSQL(null, null);
     $this->assertEqual($colSQL, implode(', ', array("UNIX_TIMESTAMP(`a_date`) AS `a_date`", "UNIX_TIMESTAMP(`a_date_time`) AS `a_date_time`", "TIME_TO_SEC(`a_time`) AS `a_time`", "`mess` AS `mess`")));
     $drop = "DROP TABLE IF EXISTS {$table}";
     $create = "CREATE TABLE {$table} (\n  " . implode(",\n  ", DBODummy::$CreateSpec) . "\n)";
     $this->expectExact('perform', $drop);
     $this->expectExact('perform', $create);
     $this->db->perform($drop);
     $this->db->perform($create);
     $dummy = new DBODummy();
     // Create a dummy
     $this->populate($dummy);
     // TODO populate really should be a method on the object or its meta
     $this->expectExact('lock', "LOCK TABLES {$table} WRITE");
     $this->expectExact('prepare', "INSERT INTO `{$table}` SET {$fieldSet}");
     $this->expectExact('execute', json_encode(array(":a_date" => (int) $dummy->aDate, ":a_date_time" => (int) $dummy->aDateTime, ":a_time" => (int) $dummy->aTime, ":mess" => $dummy->mess)));
     $this->expectExact('lastInsertId', 1);
     // The table is virgin
     $this->expectExact('unlock', 'UNLOCK TABLES');
     $dummy->create();
     // Change the dummy and save
     $this->populate($dummy);
     $this->expectExact('prepare', "UPDATE `{$table}` SET {$fieldSet} WHERE `{$key}` = :{$key} LIMIT 1");
     $this->expectExact('execute', json_encode(array(":a_date" => (int) $dummy->aDate, ":a_date_time" => (int) $dummy->aDateTime, ":a_time" => (int) $dummy->aTime, ":mess" => $dummy->mess, ":dbodummy_id" => $dummy->id)));
     $dummy->update();
     // Fetch a dummy
     $dummyId = $dummy->id;
     $dummyData = array($dummy->aDate, $dummy->aDateTime, $dummy->aTime, $dummy->mess);
     $dummy = null;
     $this->expectExact('prepare', "SELECT {$colSQL} FROM `{$table}` WHERE `{$key}` = ? LIMIT 1");
     $this->expectExact('executef', json_encode(array($dummyId)));
     $dummy = DatabaseObject::load('DBODummy', $dummyId);
     $this->assertEqual($dummyId, $dummy->id);
     $this->assertEqual($dummyData[0], $dummy->aDate);
     $this->assertEqual($dummyData[1], $dummy->aDateTime);
     $this->assertEqual($dummyData[2], $dummy->aTime);
     $this->assertEqual($dummyData[3], $dummy->mess);
     // Kill a dummy
     $this->expectExact('prepare', "DELETE FROM `{$table}` WHERE `{$key}` = ?");
     $this->expectExact('executef', json_encode(array($dummyId)));
     $dummy->delete();
     $this->assertEqual(-1, $dummy->id);
     // Delete only clears id
     $this->assertEqual($dummyData[0], $dummy->aDate);
     $this->assertEqual($dummyData[1], $dummy->aDateTime);
     $this->assertEqual($dummyData[2], $dummy->aTime);
     $this->assertEqual($dummyData[3], $dummy->mess);
     // TODO once wipe added, values should be nullifed
     // TODO update a non-existent id should fail
     // TODO fetch non-existent id should fail
 }
 public static function loadFor($linkClass, DatabaseObject $for)
 {
     assert(class_exists($linkClass));
     assert(is_subclass_of($linkClass, __CLASS__));
     if (!isset($for->id)) {
         throw new InvalidArgumentException('unsaved DatabaseObject');
     }
     $database = $for->getDatabase();
     $meta = DatabaseObjectLinkMeta::forClass($linkClass, $for->_db);
     $data = array();
     if (is_a($for, $meta->getFromClass())) {
         $sql = $meta->getSQL('link_load_from');
         $data['from'] = $for;
         $other = 'to';
         $otherClass = $meta->getToClass();
     } elseif (is_a($for, $meta->getToClass())) {
         $sql = $meta->getSQL('link_load_to');
         $data['to'] = $for;
         $other = 'from';
         $otherClass = $meta->getFromClass();
     } else {
         throw new InvalidArgumentException('invalid DatabaseObject, expecting a ' . $meta->getFromClass() . 'or a ' . $meta->getToClass());
     }
     $otherId = null;
     $list = $database->execute($sql, $for->id);
     $list->bindColumn(1, $otherId);
     $i = 1;
     foreach (array_merge($meta->getAdditionalKey(), $meta->getAutomaticColumns()) as $column) {
         $data[$column] = null;
         $list->bindColumn(++$i, $data[$column]);
     }
     $r = array();
     $call = array($linkClass, 'factory');
     while ($list->fetch()) {
         $data[$other] = DatabaseObject::load($otherClass, $otherId);
         array_push($r, call_user_func($call, $data));
     }
     return $r;
 }
Ejemplo n.º 3
0
 public static function load($id)
 {
     return DatabaseObject::load(__CLASS__, $id);
 }