Commit a20ceff3 authored by zYne's avatar zYne

tests and implementation for hydrate hooks

parent 2c7ced23
......@@ -52,6 +52,8 @@ class Doctrine_Event
const SAVEPOINT_ROLLBACK = 35;
const SAVEPOINT_COMMIT = 36;
const HYDRATE = 40;
/*
* RECORD EVENT CODES
*/
......@@ -155,7 +157,7 @@ class Doctrine_Event
case self::SAVEPOINT_ROLLBACK:
return 'rollback savepoint';
case self::SAVEPOINT_COMMIT:
return 'commit Ssavepoint';
return 'commit savepoint';
case self::RECORD_DELETE:
return 'delete record';
......@@ -222,6 +224,20 @@ class Doctrine_Event
return $this;
}
/**
* setOption
* sets the value of an option by reference
*
* @param string $option the name of the option
* @param mixed $value the value of the given option
* @return Doctrine_Event this object
*/
public function set($option, &$value)
{
$this->_options[$option] =& $value;
return $this;
}
/**
* start
* starts the internal timer of this event
......
......@@ -777,8 +777,10 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
*/
public function execute($params = array(), $hydrationMode = null)
{
$params = array_merge($this->_params['set'], $this->_params['where'],
$this->_params['having'], $params);
$params = array_merge($this->_params['set'],
$this->_params['where'],
$this->_params['having'],
$params);
if ($this->_cache) {
$cacheDriver = $this->getCacheDriver();
......@@ -984,6 +986,8 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
return $array;
}
$event = new Doctrine_Event(Doctrine_Event::HYDRATE, null);
while ($data = $stmt->fetch(Doctrine::FETCH_ASSOC)) {
$currData = array();
$identifiable = array();
......@@ -1028,12 +1032,17 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
// dealing with root component
$table = $this->_aliasMap[$rootAlias]['table'];
$componentName = $table->getComponentName();
$event->set('data', $currData[$rootAlias]);
$table->getListener()->preHydrate($event);
$element = $driver->getElement($currData[$rootAlias], $componentName);
$oneToOne = false;
$index = $driver->search($element, $array);
if ($index === false) {
if ($index === false) {
$event->set('data', $element);
$table->getListener()->postHydrate($event);
if (isset($this->_aliasMap[$rootAlias]['map'])) {
$key = $this->_aliasMap[$rootAlias]['map'];
......@@ -1058,6 +1067,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
$map = $this->_aliasMap[$alias];
$table = $this->_aliasMap[$alias]['table'];
$componentName = $table->getComponentName();
$event->set('data', $data);
$table->getListener()->preHydrate($event);
$element = $driver->getElement($data, $componentName);
$parent = $map['parent'];
......@@ -1079,6 +1091,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
$index = $driver->search($element, $prev[$parent][$componentAlias]);
if ($index === false) {
$event->set('data', $element);
$table->getListener()->postHydrate($event);
if (isset($map['map'])) {
$key = $map['map'];
if (isset($prev[$parent][$componentAlias][$key])) {
......
......@@ -54,54 +54,40 @@ class Doctrine_Hydrate_TestCase extends Doctrine_UnitTestCase
'p' => array('id' => null, 'phonenumber' => null, 'user_id' => null)
)
);
public function prepareData()
{ }
public function testExecuteForEmptyAliasMapThrowsException()
public function testHydrateHooks()
{
$h = new Doctrine_Hydrate_Mock();
try {
$h->execute();
$this->fail('Should throw exception');
} catch(Doctrine_Hydrate_Exception $e) {
$this->pass();
}
$user = new User();
$user->getTable()->addListener(new HydrationListener);
$user->name = 'zYne';
$user->save();
$this->conn->clear();
$user = Doctrine_Query::create()->from('User u')->fetchOne();
$this->assertEqual($user->name, 'ZYNE');
$this->assertEqual($user->password, 'DEFAULT PASS');
}
public function testExecuteForEmptyTableAliasMapThrowsException()
}
class HydrationListener extends Doctrine_EventListener
{
public function preHydrate(Doctrine_Event $event)
{
$h = new Doctrine_Hydrate_Mock();
$h->setData($this->testData1);
$h->setAliasMap(array('u' => array('table' => $this->conn->getTable('User'))));
try {
$h->execute();
$this->fail('Should throw exception');
} catch(Doctrine_Hydrate_Exception $e) {
$this->pass();
}
$data = $event->data;
$data['password'] = 'default pass';
$event->data = $data;
}
/**
public function testHydrate()
public function postHydrate(Doctrine_Event $event)
{
$h = new Doctrine_Hydrate_Mock();
$h->setData($this->testData1);
$h->setAliasMap(array('u' => array('table' => $this->conn->getTable('User')),
'p' => array('table' => $this->conn->getTable('Phonenumber'),
'parent' => 'u',
'relation' => $this->conn->getTable('User')->getRelation('Phonenumber')))
);
$h->setTableAliases(array('e' => 'u', 'p' => 'p'));
$coll = $h->execute();
$this->assertTrue($coll instanceof Doctrine_Collection2, 'instance of Doctrine_Collection expected');
$this->assertEqual($coll->count(), 4);
$count = count($this->dbh);
$this->assertEqual($coll[0]->Phonenumber->count(), 1);
$this->assertEqual($coll[1]->Phonenumber->count(), 2);
$this->assertEqual($coll[2]->Phonenumber->count(), 1);
$this->assertEqual($coll[3]->Phonenumber->count(), 0);
$this->assertEqual(count($this->dbh), $count);
foreach ($event->data as $key => $value) {
$event->data[$key] = strtoupper($value);
}
}
*/
}
class Doctrine_Hydrate_Mock extends Doctrine_Hydrate
{
......@@ -111,8 +97,11 @@ class Doctrine_Hydrate_Mock extends Doctrine_Hydrate
{
$this->data = $data;
}
public function _fetch($params = array(), $return = Doctrine::FETCH_RECORD)
public function getQuery()
{
}
public function execute($params = array(), $hydrationMode = null)
{
return $this->data;
}
......
......@@ -197,7 +197,7 @@ $core->addTestCase(new Doctrine_Hydrate_FetchMode_TestCase());
$core->addTestCase(new Doctrine_Tokenizer_TestCase());
//$core->addTestCase(new Doctrine_Collection_Offset_TestCase());
//$core->addTestCase(new Doctrine_BatchIterator_TestCase());
//$core->addTestCase(new Doctrine_Hydrate_TestCase());
$core->addTestCase(new Doctrine_Hydrate_TestCase());
$test->addTestCase($core);
// Relation handling
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment