From 7778d2b0321663a0f952760cfdbff16e8f4e7121 Mon Sep 17 00:00:00 2001 From: doctrine Date: Mon, 15 May 2006 12:15:20 +0000 Subject: [PATCH] IdentityMap map added, small enhancements and bug fixes --- classes/Association.class.php | 9 +- classes/Collection.class.php | 5 +- classes/Configurable.class.php | 20 +++-- classes/Doctrine.class.php | 16 +++- classes/Iterator.class.php | 18 +++- classes/Query.class.php | 51 ++++++++--- classes/Record.class.php | 27 +++--- classes/Relation.class.php | 7 +- classes/Session.class.php | 42 +++++---- classes/Table.class.php | 124 +++++++++++++++++++------- tests/AccessTestCase.class.php | 59 +++++++----- tests/CollectionTestCase.class.php | 4 +- tests/ConfigurableTestCase.class.php | 8 +- tests/EventListenerTestCase.class.php | 2 + tests/ManagerTestCase.class.php | 2 + tests/QueryTestCase.class.php | 1 - tests/RecordTestCase.class.php | 63 +++++++------ tests/SessionTestCase.class.php | 4 +- tests/UnitTestCase.class.php | 22 +++-- tests/ValidatorTestCase.class.php | 18 ++-- 20 files changed, 335 insertions(+), 167 deletions(-) diff --git a/classes/Association.class.php b/classes/Association.class.php index 07ca5484e..5b499fd89 100644 --- a/classes/Association.class.php +++ b/classes/Association.class.php @@ -2,8 +2,13 @@ require_once("Relation.class.php"); /** * Doctrine_Association this class takes care of association mapping - * (= many-to-many relationships, where the relationship is handled through an additional relational table - * which holds 2 foreign keys) + * (= many-to-many relationships, where the relationship is handled with an additional relational table + * which holds 2 foreign keys) + * + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL */ class Doctrine_Association extends Doctrine_Relation { /** diff --git a/classes/Collection.class.php b/classes/Collection.class.php index 599df098d..b7b6c970b 100644 --- a/classes/Collection.class.php +++ b/classes/Collection.class.php @@ -1,12 +1,11 @@ attributes[$i] = $listener; } /** - * @return mixed the value of the attribute + * returns the value of an attribute + * + * @param integer $attribute + * @return mixed */ final public function getAttribute($attribute) { $attribute = (int) $attribute; @@ -142,12 +144,17 @@ abstract class Doctrine_Configurable { } /** * getAttributes + * returns all attributes as an array + * * @return array */ final public function getAttributes() { return $this->attributes; } /** + * sets a parent for this configurable component + * the parent must be configurable component itself + * * @param Doctrine_Configurable $component * @return void */ @@ -156,6 +163,9 @@ abstract class Doctrine_Configurable { } /** * getParent + * returns the parent of this component + * + * @return Doctrine_Configurable */ final public function getParent() { return $this->parent; diff --git a/classes/Doctrine.class.php b/classes/Doctrine.class.php index e65f52bda..c60650099 100644 --- a/classes/Doctrine.class.php +++ b/classes/Doctrine.class.php @@ -3,6 +3,10 @@ require_once("Exception.class.php"); /** * Doctrine * the base class of Doctrine framework + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL */ final class Doctrine { /** @@ -202,7 +206,11 @@ final class Doctrine { * @var string $path doctrine root directory */ private static $path; - + /** + * returns the doctrine root + * + * @return string + */ public static function getPath() { if(! self::$path) self::$path = dirname(__FILE__); @@ -211,6 +219,8 @@ final class Doctrine { } /** * loads all runtime classes + * + * @return void */ public static function loadAll() { if(! self::$path) @@ -253,6 +263,10 @@ final class Doctrine { } /** * simple autoload function + * returns true if the class was loaded, otherwise false + * + * @param string $classname + * @return boolean */ public static function autoload($classname) { if(! self::$path) diff --git a/classes/Iterator.class.php b/classes/Iterator.class.php index f61da6348..61246fa3f 100644 --- a/classes/Iterator.class.php +++ b/classes/Iterator.class.php @@ -2,10 +2,14 @@ /** * Doctrine_Iterator * iterates through Doctrine_Collection + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL */ abstract class Doctrine_Iterator implements Iterator { /** - * @var Doctrine_Collection $collection + * @var Doctrine_Collection $collection */ protected $collection; /** @@ -35,6 +39,8 @@ abstract class Doctrine_Iterator implements Iterator { $this->count = $this->collection->count(); } /** + * rewinds the iterator + * * @return void */ public function rewind() { @@ -45,18 +51,24 @@ abstract class Doctrine_Iterator implements Iterator { } /** - * @return integer the current key + * returns the current key + * + * @return integer */ public function key() { return $this->key; } /** - * @return Doctrine_Record the current record + * returns the current record + * + * @return Doctrine_Record */ public function current() { return $this->collection->get($this->key); } /** + * advances the internal pointer + * * @return void */ public function next() { diff --git a/classes/Query.class.php b/classes/Query.class.php index 587494adc..d5cf4760d 100644 --- a/classes/Query.class.php +++ b/classes/Query.class.php @@ -67,6 +67,8 @@ class Doctrine_Query extends Doctrine_Access { "offset" => false, ); /** + * constructor + * * @param Doctrine_Session $session */ public function __construct(Doctrine_Session $session) { @@ -75,6 +77,8 @@ class Doctrine_Query extends Doctrine_Access { /** * clear * resets all the variables + * + * @return void */ private function clear() { $this->fetchModes = array(); @@ -99,13 +103,14 @@ class Doctrine_Query extends Doctrine_Access { $this->joined = array(); } /** - * loadFields -- this method loads fields for a given factory and - * constructs a little bit of sql for every field - * - * fields of the factories become: [tablename].[fieldname] as [tablename]__[fieldname] + * loadFields + * loads fields for a given table and + * constructs a little bit of sql for every field + * + * fields of the tables become: [tablename].[fieldname] as [tablename]__[fieldname] * * @access private - * @param object Doctrine_Table $table a Doctrine_Table object + * @param object Doctrine_Table $table a Doctrine_Table object * @param integer $fetchmode fetchmode the table is using eg. Doctrine::FETCH_LAZY * @return void */ @@ -141,8 +146,11 @@ class Doctrine_Query extends Doctrine_Access { } } /** + * sets a query part + * * @param string $name * @param array $args + * @return void */ public function __call($name, $args) { $name = strtolower($name); @@ -168,7 +176,9 @@ class Doctrine_Query extends Doctrine_Access { return $this; } /** - * @param $name query part name + * returns a query part + * + * @param $name query part name * @return mixed */ public function get($name) { @@ -178,8 +188,10 @@ class Doctrine_Query extends Doctrine_Access { return $this->parts[$name]; } /** - * @param $name query part name - * @param $value query part value + * sets a query part + * + * @param $name query part name + * @param $value query part value * @return boolean */ public function set($name, $value) { @@ -207,7 +219,9 @@ class Doctrine_Query extends Doctrine_Access { return false; } /** - * @return string the built sql query + * returns the built sql query + * + * @return string */ final public function getQuery() { if(empty($this->parts["columns"]) || empty($this->parts["from"])) @@ -271,6 +285,7 @@ class Doctrine_Query extends Doctrine_Access { /** * applyInheritance * applies column aggregation inheritance to DQL query + * * @return boolean */ final public function applyInheritance() { @@ -324,8 +339,8 @@ class Doctrine_Query extends Doctrine_Access { } /** * getData - * @param $key the factory name - * @return array the data row for the specified factory + * @param $key the component name + * @return array the data row for the specified component */ final public function getData($key) { if(isset($this->data[$key]) && is_array($this->data[$key])) @@ -335,7 +350,8 @@ class Doctrine_Query extends Doctrine_Access { } /** * execute - * executes the datagraph and populates Doctrine_Collections + * executes the dql query and populates all collections + * * @param string $params * @return Doctrine_Collection the root collection */ @@ -423,6 +439,8 @@ class Doctrine_Query extends Doctrine_Access { } /** * parseData + * parses a PDOStatement + * * @return array */ public function parseData(PDOStatement $stmt) { @@ -447,6 +465,9 @@ class Doctrine_Query extends Doctrine_Access { return $array; } /** + * returns a Doctrine_Table for given name + * + * @param string $name component name * @return Doctrine_Table */ public function getTable($name) { @@ -454,6 +475,8 @@ class Doctrine_Query extends Doctrine_Access { } /** * getCollection + * + * @parma string $name component name * @param integer $index */ private function getCollection($name) { @@ -504,6 +527,9 @@ class Doctrine_Query extends Doctrine_Access { } /** * DQL PARSER + * + * @param string $query DQL query + * @return void */ final public function parseQuery($query) { $this->clear(); @@ -588,6 +614,7 @@ class Doctrine_Query extends Doctrine_Access { /** * DQL SELECT PARSER * parses the select part of the query string + * * @param string $str * @return void */ diff --git a/classes/Record.class.php b/classes/Record.class.php index 64649d58c..61d96d73a 100644 --- a/classes/Record.class.php +++ b/classes/Record.class.php @@ -54,7 +54,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ protected $id; /** - * @var array $data the dao data + * @var array $data the record data */ protected $data = array(); @@ -63,12 +63,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ private $modified = array(); /** - * @var integer $state the state of this data access object + * @var integer $state the state of this record * @see STATE_* constants */ private $state; /** - * @var array $collections the collections this dao is in + * @var array $collections the collections this record is in */ private $collections = array(); /** @@ -151,12 +151,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite // listen the onLoad event $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this); } - // add data access object to registry $this->table->getRepository()->add($this); - - - - $this->table->setData(array()); } } /** @@ -166,6 +161,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite public function setUp() { } /** * return the object identifier + * * @return integer */ public function getOID() { @@ -200,6 +196,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite return $cols; } /** + * prepares identifiers + * * @return void */ public function prepareIdentifiers() { @@ -217,6 +215,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } /** * this method is automatically called when this Doctrine_Record is serialized + * + * @return array */ public function __sleep() { $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onSleep($this); @@ -241,6 +241,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite /** * __wakeup * this method is automatically called everytime a Doctrine_Record object is unserialized + * + * @return void */ public function __wakeup() { $this->modified = array(); @@ -388,6 +390,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } return $this->data[$name]; } + if($name == $this->table->getIdentifier()) return $this->id; @@ -755,11 +758,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @param integer $id * @return void */ - final public function setID($id = null) { - if($id === null) { - $this->id = null; + final public function setID($id = false) { + if($id === false) { + $this->id = false; $this->cleanData(); - $this->state = Doctrine_Record::STATE_TCLEAN; + $this->state = Doctrine_Record::STATE_TCLEAN; $this->modified = array(); } else { $this->id = $id; diff --git a/classes/Relation.class.php b/classes/Relation.class.php index 3b9fd2ad2..47b67dfd3 100644 --- a/classes/Relation.class.php +++ b/classes/Relation.class.php @@ -1,13 +1,10 @@ transaction_level--; - + if($this->transaction_level == 0) { - - + + if($this->getAttribute(Doctrine::ATTR_LOCKMODE) == Doctrine::LOCK_OPTIMISTIC) { $this->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($this); - + $this->dbh->beginTransaction(); - + $this->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($this); } - - if($this->getAttribute(Doctrine::ATTR_VLD)) { + + if($this->getAttribute(Doctrine::ATTR_VLD)) $this->validator = new Doctrine_Validator(); - } + $this->bulkInsert(); $this->bulkUpdate(); $this->bulkDelete(); - - if(isset($this->validator) && $this->validator->hasErrors()) { - $this->rollback(); - throw new Doctrine_Validator_Exception($this->validator); + if($this->getAttribute(Doctrine::ATTR_VLD)) { + if($this->validator->hasErrors()) { + $this->rollback(); + throw new Doctrine_Validator_Exception($this->validator); + } } - + $this->dbh->commit(); $this->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionCommit($this); - $this->delete = array(array()); + $this->delete = array(); $this->state = Doctrine_Session::STATE_OPEN; - + $this->validator = null; - + } elseif($this->transaction_level == 1) $this->state = Doctrine_Session::STATE_ACTIVE; } @@ -477,8 +479,9 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab $id = $table->getMaxIdentifier(); } - + $record->setID($id); + $id++; // listen the onInsert event @@ -556,7 +559,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab $ids = array(); foreach($deletes as $k => $record) { $ids[] = $record->getID(); - $record->setID(null); + $record->setID(false); } if($record instanceof Doctrine_Record) { $table = $record->getTable(); @@ -716,6 +719,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab return false; $seq = $record->getTable()->getSequenceName(); + if( ! empty($seq)) { $id = $this->getNextID($seq); $name = $record->getTable()->getIdentifier(); diff --git a/classes/Table.class.php b/classes/Table.class.php index 4a685002c..e0ccf8ed2 100644 --- a/classes/Table.class.php +++ b/classes/Table.class.php @@ -59,8 +59,10 @@ class Doctrine_Table extends Doctrine_Configurable { */ private $sequenceName; /** - * @var Doctrine_Repository $repository first level cache + * @var array $identityMap first level cache */ + private $identityMap = array(); + private $repository; /** @@ -115,7 +117,7 @@ class Doctrine_Table extends Doctrine_Configurable { do { if($class == "Doctrine_Record") break; - $name = ucwords(strtolower($class)); + $name = $class; $names[] = $name; } while($class = get_parent_class($class)); @@ -206,8 +208,7 @@ class Doctrine_Table extends Doctrine_Configurable { * @return void */ final public function initComponents() { - $this->repository = new Doctrine_Repository($this); - + $this->repository = new Doctrine_Repository($this); switch($this->getAttribute(Doctrine::ATTR_CACHE)): case Doctrine::CACHE_SQLITE: $this->cache = new Doctrine_Cache_Sqlite($this); @@ -217,6 +218,9 @@ class Doctrine_Table extends Doctrine_Configurable { break; endswitch; } + public function getRepository() { + return $this->repository; + } /** * setColumn * @param string $name @@ -322,7 +326,9 @@ class Doctrine_Table extends Doctrine_Configurable { return $array; } /** - * @var array bound relations + * returns all bound relations + * + * @return array */ final public function getBounds() { return $this->bound; @@ -366,12 +372,6 @@ class Doctrine_Table extends Doctrine_Configurable { final public function getCache() { return $this->cache; } - /** - * @return Doctrine_Repository - */ - final public function getRepository() { - return $this->repository; - } /** * @param string $name component name of which a foreign key object is bound * @return Doctrine_Relation @@ -456,7 +456,9 @@ class Doctrine_Table extends Doctrine_Configurable { } } /** - * @return array an array containing all foreign key objects + * returns an array containing all foreign key objects + * + * @return array */ final public function getForeignKeys() { $a = array(); @@ -466,6 +468,9 @@ class Doctrine_Table extends Doctrine_Configurable { return $a; } /** + * sets the database table name + * + * @param string $name database table name * @return void */ final public function setTableName($name) { @@ -473,29 +478,36 @@ class Doctrine_Table extends Doctrine_Configurable { } /** - * @return string database table name this class represents + * returns the database table name + * + * @return string */ final public function getTableName() { return $this->tableName; } /** - * createDAO + * create + * creates a new record + * * @param $array an array where keys are field names and values representing field values - * @return Doctrine_Record A new Data Access Object. Uses an sql insert statement when saved + * @return Doctrine_Record */ public function create(array $array = array()) { - $this->data = $array; + $this->data = $array; $this->isNewEntry = true; - $record = $this->getRecord(); + $record = new $this->name($this); $this->isNewEntry = false; + $this->data = array(); return $record; } /** + * finds a record by its identifier + * * @param $id database row id * @throws Doctrine_Find_Exception * @return Doctrine_Record a record for given database identifier */ - public function find($id = null) { + public function find($id) { if($id !== null) { $query = $this->query." WHERE ".implode(" = ? AND ",$this->primaryKeys)." = ?"; $query = $this->applyInheritance($query); @@ -507,12 +519,12 @@ class Doctrine_Table extends Doctrine_Configurable { if($this->data === false) throw new Doctrine_Find_Exception(); } - return new $this->name($this); + return $this->getRecord(); } /** * applyInheritance * @param $where query where part to be modified - * @return query where part with column aggregation inheritance added + * @return string query where part with column aggregation inheritance added */ final public function applyInheritance($where) { if( ! empty($this->inheritanceMap)) { @@ -527,7 +539,9 @@ class Doctrine_Table extends Doctrine_Configurable { } /** * findAll - * @return Doctrine_Collection a collection of all data access objects + * returns a collection of records + * + * @return Doctrine_Collection */ public function findAll() { $graph = new Doctrine_Query($this->session); @@ -536,19 +550,49 @@ class Doctrine_Table extends Doctrine_Configurable { } /** * findBySql - * @return Doctrine_Collection a collection of data access objects + * finds records with given sql where clause + * returns a collection of records + * + * @param string $sql SQL after WHERE clause + * @param array $params query parameters + * @return Doctrine_Collection */ public function findBySql($sql, array $params = array()) { - $graph = new Doctrine_Query($this->session); - $users = $graph->query("FROM ".$this->name." WHERE ".$sql, $params); + $q = new Doctrine_Query($this->session); + $users = $q->query("FROM ".$this->name." WHERE ".$sql, $params); return $users; } + /** + * clear + * clears the first level cache (identityMap) + * + * @return void + */ + public function clear() { + $this->identityMap = array(); + } /** * getRecord + * first checks if record exists in identityMap, if not + * returns a new record + * * @return Doctrine_Record */ public function getRecord() { - return new $this->name($this); + $key = $this->getIdentifier(); + if(isset($this->data[$key])) { + $id = $this->data[$key]; + if(isset($this->identityMap[$id])) + $record = $this->identityMap[$id]; + else { + $record = new $this->name($this); + $this->identityMap[$id] = $record; + } + $this->data = array(); + + return $record; + } + throw new Doctrine_Exception("No primary key found"); } /** * @param $id database row id @@ -571,7 +615,7 @@ class Doctrine_Table extends Doctrine_Configurable { } /** * getTableDescription - * @return Doctrine_Table_Description the columns object for this factory + * @return Doctrine_Table_Description */ final public function getTableDescription() { return $this->columns; @@ -611,20 +655,27 @@ class Doctrine_Table extends Doctrine_Configurable { return $coll; } /** + * returns all columns and their definitions + * * @return array */ final public function getColumns() { return $this->columns; } /** - * @return array an array containing all the column names + * returns an array containing all the column names + * + * @return array */ public function getColumnNames() { return array_keys($this->columns); } /** * setData - * @param array $data internal data, users are strongly discouraged to use this function + * doctrine uses this function internally + * users are strongly discouraged to use this function + * + * @param array $data internal data * @return void */ public function setData(array $data) { @@ -642,25 +693,34 @@ class Doctrine_Table extends Doctrine_Configurable { return isset($data[0])?$data[0]:1; } /** - * @return boolean whether or not a newly created object is new or not + * return whether or not a newly created object is new or not + * + * @return boolean */ final public function isNewEntry() { return $this->isNewEntry; } /** - * @return string simple cached query + * returns simple cached query + * + * @return string */ final public function getQuery() { return $this->query; } /** - * @return array internal data, used by Doctrine_Record instances when retrieving data from database + * returns internal data, used by Doctrine_Record instances + * when retrieving data from database + * + * @return array */ final public function getData() { return $this->data; } /** - * @return string string representation of this object + * returns a string representation of this object + * + * @return string */ public function __toString() { return Doctrine_Lib::getTableAsString($this); diff --git a/tests/AccessTestCase.class.php b/tests/AccessTestCase.class.php index 19961431e..d1cb139e0 100644 --- a/tests/AccessTestCase.class.php +++ b/tests/AccessTestCase.class.php @@ -1,39 +1,56 @@ assertEqual($this->new["name"],null); + $user = new User(); + $this->assertEqual($user["name"],null); - $this->new["name"] = "Jack"; - $this->assertEqual($this->new["name"],"Jack"); - - $this->assertEqual($this->old["name"],"zYne"); + $user["name"] = "Jack"; + $this->assertEqual($user["name"],"Jack"); - $this->old["name"] = "Jack"; - $this->assertEqual($this->old["name"],"Jack"); + $user->save(); + + $user = $this->session->getTable("User")->find($user->getID()); + $this->assertEqual($user->name,"Jack"); + + $user["name"] = "Jack"; + $this->assertEqual($user["name"],"Jack"); + $user["name"] = "zYne"; + $this->assertEqual($user["name"],"zYne"); } public function testOverload() { - $this->assertEqual($this->new->name,null); + $user = new User(); + $this->assertEqual($user->name,null); - $this->new->name = "Jack"; - $this->assertEqual($this->new->name,"Jack"); + $user->name = "Jack"; - $this->assertEqual($this->old->name,"zYne"); + $this->assertEqual($user->name,"Jack"); + + $user->save(); - $this->old->name = "Jack"; - $this->assertEqual($this->old->name,"Jack"); + $user = $this->session->getTable("User")->find($user->getID()); + $this->assertEqual($user->name,"Jack"); + + $user->name = "Jack"; + $this->assertEqual($user->name,"Jack"); + $user->name = "zYne"; + $this->assertEqual($user->name,"zYne"); } public function testSet() { - $this->assertEqual($this->new->get("name"),null); + $user = new User(); + $this->assertEqual($user->get("name"),null); - $this->new->set("name","Jack"); - $this->assertEqual($this->new->get("name"),"Jack"); + $user->set("name","Jack"); + $this->assertEqual($user->get("name"),"Jack"); - $this->assertEqual($this->old->get("name"),"zYne"); + $user->save(); - $this->old->set("name","Jack"); - $this->assertEqual($this->old->get("name"),"Jack"); - - $this->assertEqual($this->old->getID(),4); + $user = $this->session->getTable("User")->find($user->getID()); + + $this->assertEqual($user->get("name"),"Jack"); + + $user->set("name","Jack"); + $this->assertEqual($user->get("name"),"Jack"); } } ?> diff --git a/tests/CollectionTestCase.class.php b/tests/CollectionTestCase.class.php index 2860d87ca..25d416083 100644 --- a/tests/CollectionTestCase.class.php +++ b/tests/CollectionTestCase.class.php @@ -41,7 +41,9 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { $generator = new Doctrine_IndexGenerator($this->objTable->getIdentifier()); $coll->setGenerator($generator); $generator = $coll->getGenerator(); - $this->assertEqual($generator->getIndex($this->old), 4); + + $user = $this->session->getTable("User")->find(4); + $this->assertEqual($generator->getIndex($user), 4); } public function testGenerator() { diff --git a/tests/ConfigurableTestCase.class.php b/tests/ConfigurableTestCase.class.php index c42370862..0fda72bd4 100644 --- a/tests/ConfigurableTestCase.class.php +++ b/tests/ConfigurableTestCase.class.php @@ -2,7 +2,11 @@ require_once("UnitTestCase.class.php"); class Doctrine_ConfigurableTestCase extends Doctrine_UnitTestCase { + public function prepareTables() { } + public function prepareData() { } public function testSetAttribute() { + $table = $this->session->getTable("User"); + $this->manager->setAttribute(Doctrine::ATTR_CACHE_TTL,100); $this->assertEqual($this->manager->getAttribute(Doctrine::ATTR_CACHE_TTL),100); @@ -64,14 +68,14 @@ class Doctrine_ConfigurableTestCase extends Doctrine_UnitTestCase { $e = false; try { - $this->objTable->setAttribute(Doctrine::ATTR_COLL_KEY, "unknown"); + $table->setAttribute(Doctrine::ATTR_COLL_KEY, "unknown"); } catch(Exception $e) { } $this->assertTrue($e instanceof Exception); $e = true; try { - $this->objTable->setAttribute(Doctrine::ATTR_COLL_KEY, "name"); + $table->setAttribute(Doctrine::ATTR_COLL_KEY, "name"); } catch(Exception $e) { } $this->assertTrue($e); diff --git a/tests/EventListenerTestCase.class.php b/tests/EventListenerTestCase.class.php index d5955506c..36ed720fd 100644 --- a/tests/EventListenerTestCase.class.php +++ b/tests/EventListenerTestCase.class.php @@ -9,5 +9,7 @@ class Doctrine_EventListenerTestCase extends Doctrine_UnitTestCase { $this->assertTrue($last->getObject() instanceof Doctrine_Session); $this->assertTrue($last->getCode() == Doctrine_Debugger::EVENT_OPEN); } + public function prepareData() { } + public function prepareTables() { } } ?> diff --git a/tests/ManagerTestCase.class.php b/tests/ManagerTestCase.class.php index 67dcab4bc..05b2ff9dc 100644 --- a/tests/ManagerTestCase.class.php +++ b/tests/ManagerTestCase.class.php @@ -19,5 +19,7 @@ class Doctrine_ManagerTestCase extends Doctrine_UnitTestCase { public function testGetSessions() { $this->assertEqual(count($this->manager->getSessions()),1); } + public function prepareData() { } + public function prepareTables() { } } ?> diff --git a/tests/QueryTestCase.class.php b/tests/QueryTestCase.class.php index e8b9fe836..7c867f6e9 100644 --- a/tests/QueryTestCase.class.php +++ b/tests/QueryTestCase.class.php @@ -174,7 +174,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertTrue($users instanceof Doctrine_Collection_Immediate); $count2 = $this->session->getDBH()->count(); - $this->assertEqual($count,$count2); $users = $query->query("FROM User-b"); diff --git a/tests/RecordTestCase.class.php b/tests/RecordTestCase.class.php index aba445aa3..5538ee99c 100644 --- a/tests/RecordTestCase.class.php +++ b/tests/RecordTestCase.class.php @@ -37,7 +37,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->assertEqual($task->name, "Task 1"); $this->assertEqual($task->Resource[0]->name, "Resource 1"); $this->assertEqual($task->Resource->count(), 1); - $this->assertEqual($task->Subtask[0]->name, "Subtask 1"); + $this->assertEqual($task->Subtask[0]->name, "Subtask 1"); } @@ -107,11 +107,16 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->assertEqual($user->name, "Jack Daniels"); $this->assertEqual($user->created, null); $this->assertEqual($user->updated, null); + $this->assertEqual($user->getTable()->getData(), array()); } + public function testNewOperator() { + $table = $this->session->getTable("User"); + + $this->assertEqual($this->session->getTable("User")->getData(), array()); $user = new User(); - $this->assertTrue($user->getState() == Doctrine_Record::STATE_TCLEAN); + $this->assertEqual(Doctrine_Lib::getRecordStateAsString($user->getState()), Doctrine_Lib::getRecordStateAsString(Doctrine_Record::STATE_TCLEAN)); $user->name = "John Locke"; $this->assertTrue($user->name,"John Locke"); @@ -243,37 +248,36 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->assertEqual($e->code,2); $this->assertEqual($e->message,"changed message"); $this->assertEqual($e->Description[0]->description, "1st changed description"); - $this->assertEqual($e->Description[1]->description, "2nd changed description"); - - - + $this->assertEqual($e->Description[1]->description, "2nd changed description"); } public function testInsert() { - $this->new->name = "John Locke"; - $this->new->save(); + $user = new User(); + $user->name = "John Locke"; + $user->save(); - $this->assertTrue($this->new->getModified() == array()); - $this->assertTrue($this->new->getState() == Doctrine_Record::STATE_CLEAN); + $this->assertTrue($user->getModified() == array()); + $this->assertTrue($user->getState() == Doctrine_Record::STATE_CLEAN); $debug = $this->listener->getMessages(); $p = array_pop($debug); $this->assertTrue($p->getObject() instanceof Doctrine_Session); $this->assertTrue($p->getCode() == Doctrine_Debugger::EVENT_COMMIT); - $this->new->delete(); - $this->assertTrue($this->new->getState() == Doctrine_Record::STATE_TCLEAN); + $user->delete(); + $this->assertTrue($user->getState() == Doctrine_Record::STATE_TCLEAN); } public function testUpdate() { - $this->old->set("name","Jack Daniels",true); + $user = $this->session->getTable("User")->find(4); + $user->set("name","Jack Daniels",true); - $this->old->save(true); + $user->save(); //print $this->old->name; - $this->assertEqual($this->old->getModified(), array()); - $this->assertEqual($this->old->name, "Jack Daniels"); + $this->assertEqual($user->getModified(), array()); + $this->assertEqual($user->name, "Jack Daniels"); $debug = $this->listener->getMessages(); $p = array_pop($debug); @@ -291,7 +295,8 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { } public function testCopy() { - $new = $this->old->copy(); + $user = $this->session->getTable("User")->find(4); + $new = $user->copy(); $this->assertTrue($new instanceof Doctrine_Record); $this->assertTrue($new->getState() == Doctrine_Record::STATE_TDIRTY); } @@ -522,27 +527,31 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { } public function testCount() { - $this->assertTrue(is_integer($this->old->count())); + $user = $this->session->getTable("User")->find(4); + + $this->assertTrue(is_integer($user->count())); } public function testGetReference() { - $this->assertTrue($this->old->Email instanceof Doctrine_Record); - $this->assertTrue($this->old->Phonenumber instanceof Doctrine_Collection); - $this->assertTrue($this->old->Group instanceof Doctrine_Collection); + $user = $this->session->getTable("User")->find(4); - $this->assertTrue($this->old->Phonenumber->count() == 1); + $this->assertTrue($user->Email instanceof Doctrine_Record); + $this->assertTrue($user->Phonenumber instanceof Doctrine_Collection); + $this->assertTrue($user->Group instanceof Doctrine_Collection); + + $this->assertTrue($user->Phonenumber->count() == 1); } public function testSerialize() { - $old = $this->old; - $old = serialize($old); + $user = $this->session->getTable("User")->find(4); + $str = serialize($user); - $this->assertEqual(unserialize($old)->getID(),$this->old->getID()); + $this->assertEqual(unserialize($str)->getID(),$user->getID()); } public function testGetIterator() { - $this->assertTrue($this->old->getIterator() instanceof ArrayIterator); + $user = $this->session->getTable("User")->find(4); + $this->assertTrue($user->getIterator() instanceof ArrayIterator); } - } ?> diff --git a/tests/SessionTestCase.class.php b/tests/SessionTestCase.class.php index c409f3da5..8a6f09416 100644 --- a/tests/SessionTestCase.class.php +++ b/tests/SessionTestCase.class.php @@ -17,8 +17,8 @@ class Doctrine_SessionTestCase extends Doctrine_UnitTestCase { } public function testFlush() { - - $this->assertTrue(is_numeric($this->old->Phonenumber[0]->entity_id)); + $user = $this->session->getTable("User")->find(4); + $this->assertTrue(is_numeric($user->Phonenumber[0]->entity_id)); $user = $this->session->create("Email"); $user = $this->session->create("User"); diff --git a/tests/UnitTestCase.class.php b/tests/UnitTestCase.class.php index c2e64fb06..478299680 100644 --- a/tests/UnitTestCase.class.php +++ b/tests/UnitTestCase.class.php @@ -14,7 +14,6 @@ class Doctrine_UnitTestCase extends UnitTestCase { protected $manager; protected $session; protected $objTable; - protected $repository; protected $new; protected $old; protected $dbh; @@ -33,7 +32,6 @@ class Doctrine_UnitTestCase extends UnitTestCase { $this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE); $this->tables = array("entity","email","phonenumber","groupuser","album","song","element","error","description","address","account","task","resource","assignment"); - $tables = $this->tables; @@ -50,23 +48,22 @@ class Doctrine_UnitTestCase extends UnitTestCase { $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener); } - foreach($tables as $name) { + $this->prepareTables(); + $this->prepareData(); + } + public function prepareTables() { + foreach($this->tables as $name) { $this->dbh->query("DROP TABLE IF EXISTS $name"); } - foreach($tables as $name) { + foreach($this->tables as $name) { $name = ucwords($name); $table = $this->session->getTable($name); $table->getCache()->deleteAll(); + $table->clear(); } - - $this->objTable = $this->session->getTable("User"); - $this->repository = $this->objTable->getRepository(); - //$this->cache = $this->objTable->getCache(); - - $this->prepareData(); } public function prepareData() { $groups = new Doctrine_Collection($this->session->getTable("Group")); @@ -137,9 +134,10 @@ class Doctrine_UnitTestCase extends UnitTestCase { public function setUp() { if( ! $this->init) $this->init(); + if(isset($this->objTable)) + $this->objTable->clear(); + $this->init = true; - $this->new = $this->objTable->create(); - $this->old = $this->objTable->find(4); } } ?> diff --git a/tests/ValidatorTestCase.class.php b/tests/ValidatorTestCase.class.php index d3b6fe5fb..1b90edab1 100644 --- a/tests/ValidatorTestCase.class.php +++ b/tests/ValidatorTestCase.class.php @@ -1,19 +1,21 @@ session->getTable("User")->find(4); $set = array("password" => "this is an example of too long password", "loginname" => "this is an example of too long loginname", "name" => "valid name", "created" => "invalid"); - $this->old->setArray($set); - $email = $this->old->Email; + $user->setArray($set); + $email = $user->Email; $email->address = "zYne@invalid"; - $this->assertTrue($this->old->getModified() == $set); + $this->assertTrue($user->getModified() == $set); $validator = new Doctrine_Validator(); - $validator->validateRecord($this->old); + $validator->validateRecord($user); $validator->validateRecord($email); $stack = $validator->getErrorStack(); @@ -31,6 +33,7 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { $this->assertEqual($stack["Email"][1]["address"], Doctrine_Validator::ERR_UNIQUE); } + public function testIsValidEmail() { $validator = new Doctrine_Validator_Email(); @@ -47,10 +50,10 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { } public function testSave() { $this->manager->setAttribute(Doctrine::ATTR_VLD, true); - + $user = $this->session->getTable("User")->find(4); try { - $this->old->name = "this is an example of too long name not very good example but an example nevertheless"; - $this->old->save(); + $user->name = "this is an example of too long name not very good example but an example nevertheless"; + $user->save(); } catch(Doctrine_Validator_Exception $e) { $this->assertEqual($e->getErrorStack(),array("User" => array(array("name" => 0)))); } @@ -66,6 +69,7 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { $this->assertTrue(is_array($a)); $this->assertEqual($a["Email"][0]["address"], Doctrine_Validator::ERR_VALID); $this->assertEqual($a["User"][0]["name"], Doctrine_Validator::ERR_LENGTH); + $this->manager->setAttribute(Doctrine::ATTR_VLD, false); } }