diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 9fe3087c0..f3fd9bc22 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -585,10 +585,12 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { return false; } /** + * getRelation + * * @param string $name component name of which a foreign key object is bound * @return Doctrine_Relation */ - final public function getRelation($name) { + final public function getRelation($name, $recursive = true) { $original = $name; if(isset($this->relations[$name])) @@ -672,10 +674,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { $this->relations[$alias] = $relation; return $this->relations[$alias]; } - try { - throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original); - } catch(Exception $e) { - print $e; + // load all relations + $this->getRelations(); + + if($recursive) { + return $this->getRelation($original, false); + } else { + throw new Doctrine_Table_Exception($this->name . " doesn't have a relation to " . $original); } } /** diff --git a/tests/ConnectionTransactionTestCase.php b/tests/ConnectionTransactionTestCase.php new file mode 100644 index 000000000..3d01eabb4 --- /dev/null +++ b/tests/ConnectionTransactionTestCase.php @@ -0,0 +1,183 @@ +messages[] = $m; + } + public function pop() { + return array_pop($this->messages); + } + public function clear() { + $this->messages = array(); + } + public function getAll() { + return $this->messages; + } +} + +class Doctrine_Connection_Transaction_TestCase extends Doctrine_UnitTestCase { + public function prepareData() { } + public function testInsert() { + $count = count($this->dbh); + + + $listener = new Transaction_TestLogger(); + + $user = new User(); + $user->getTable()->getConnection()->setListener($listener); + + $this->connection->beginTransaction(); + + $user->name = 'John'; + + $user->save(); + + $this->assertEqual($listener->pop(), 'onSave'); + $this->assertEqual($listener->pop(), 'onInsert'); + $this->assertEqual($listener->pop(), 'onPreInsert'); + $this->assertEqual($listener->pop(), 'onPreSave'); + $this->assertEqual($listener->pop(), 'onSetProperty'); + $this->assertEqual($listener->pop(), 'onTransactionBegin'); + $this->assertEqual($listener->pop(), 'onPreTransactionBegin'); + + $this->assertEqual($user->id, 1); + + $this->assertTrue($count < count($this->dbh)); + + $this->connection->commit(); + + $this->assertEqual($listener->pop(), 'onTransactionCommit'); + $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + } + public function testInsertMultiple() { + $count = count($this->dbh); + + + $listener = new Transaction_TestLogger(); + + $users = new Doctrine_Collection('User'); + $users->getTable()->getConnection()->setListener($listener); + + $this->connection->beginTransaction(); + + $users[0]->name = 'Arnold'; + $users[1]->name = 'Vincent'; + + $users[0]->save(); + $users[1]->save(); + + + $this->assertEqual($listener->pop(), 'onSave'); + $this->assertEqual($listener->pop(), 'onInsert'); + $this->assertEqual($listener->pop(), 'onPreInsert'); + $this->assertEqual($listener->pop(), 'onPreSave'); + $this->assertEqual($listener->pop(), 'onSave'); + $this->assertEqual($listener->pop(), 'onInsert'); + $this->assertEqual($listener->pop(), 'onPreInsert'); + $this->assertEqual($listener->pop(), 'onPreSave'); + + $this->assertEqual($users[0]->id, 2); + + $this->assertEqual($users[1]->id, 3); + + $this->assertTrue($count < count($this->dbh)); + + $this->connection->commit(); + + $this->assertEqual($listener->pop(), 'onTransactionCommit'); + $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + } + public function testUpdate() { + $count = count($this->dbh); + + + $user = $this->connection->getTable('User')->find(1); + + $listener = new Transaction_TestLogger(); + $user->getTable()->getConnection()->setListener($listener); + $this->connection->beginTransaction(); + + $user->name = 'Jack'; + + $user->save(); + + $this->assertEqual($listener->pop(), 'onSave'); + $this->assertEqual($listener->pop(), 'onUpdate'); + $this->assertEqual($listener->pop(), 'onPreUpdate'); + $this->assertEqual($listener->pop(), 'onPreSave'); + $this->assertEqual($listener->pop(), 'onSetProperty'); + $this->assertEqual($listener->pop(), 'onTransactionBegin'); + $this->assertEqual($listener->pop(), 'onPreTransactionBegin'); + + $this->assertEqual($user->id, 1); + + $this->assertTrue($count < count($this->dbh)); + + $this->connection->commit(); + + $this->assertEqual($listener->pop(), 'onTransactionCommit'); + $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + } + public function testUpdateMultiple() { + $count = count($this->dbh); + + $listener = new Transaction_TestLogger(); + + $users = $this->connection->query('FROM User'); + $users->getTable()->getConnection()->setListener($listener); + + $this->connection->beginTransaction(); + + $users[1]->name = 'Arnold'; + $users[2]->name = 'Vincent'; + + $users[1]->save(); + $users[2]->save(); + + + $this->assertEqual($listener->pop(), 'onSave'); + $this->assertEqual($listener->pop(), 'onUpdate'); + $this->assertEqual($listener->pop(), 'onPreUpdate'); + $this->assertEqual($listener->pop(), 'onPreSave'); + $this->assertEqual($listener->pop(), 'onSave'); + $this->assertEqual($listener->pop(), 'onUpdate'); + $this->assertEqual($listener->pop(), 'onPreUpdate'); + $this->assertEqual($listener->pop(), 'onPreSave'); + + $this->assertEqual($users[1]->id, 2); + + $this->assertEqual($users[2]->id, 3); + + $this->assertTrue($count < count($this->dbh)); + + $this->connection->commit(); + + $this->assertEqual($listener->pop(), 'onTransactionCommit'); + $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + } + public function testDelete() { + $count = count($this->dbh); + + $listener = new Transaction_TestLogger(); + $listener->clear(); + $users = $this->connection->query('FROM User'); + $users->getTable()->getConnection()->setListener($listener); + + $this->connection->beginTransaction(); + + $users->delete(); + + $this->assertEqual($listener->pop(), 'onPreDelete'); + + $this->assertTrue($count, count($this->dbh)); + + $this->connection->commit(); + + $this->assertTrue(($count + 1), count($this->dbh)); + + $this->assertEqual($listener->pop(), 'onTransactionCommit'); + $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + } +} +?> diff --git a/tests/RelationTestCase.php b/tests/RelationTestCase.php new file mode 100644 index 000000000..ad21b18c1 --- /dev/null +++ b/tests/RelationTestCase.php @@ -0,0 +1,35 @@ +tables = array(); + } + public function testManyToManyRelation() { + $user = new User(); + + // test that join table relations can be initialized even before the association have been initialized + try { + $user->Groupuser; + $this->pass(); + } catch(Doctrine_Table_Exception $e) { + $this->fail(); + } + $this->assertTrue($user->getTable()->getRelation('Groupuser') instanceof Doctrine_Relation_ForeignKey); + $this->assertTrue($user->getTable()->getRelation('Group') instanceof Doctrine_Relation_Association); + } + public function testOneToOneLocalKeyRelation() { + $user = new User(); + + $this->assertTrue($user->getTable()->getRelation('Email') instanceof Doctrine_Relation_LocalKey); + } + public function testOneToOneForeignKeyRelation() { + $user = new User(); + + $this->assertTrue($user->getTable()->getRelation('Account') instanceof Doctrine_Relation_ForeignKey); + } + public function testOneToManyForeignKeyRelation() { + $user = new User(); + + $this->assertTrue($user->getTable()->getRelation('Phonenumber') instanceof Doctrine_Relation_ForeignKey); + } +} diff --git a/tests/run.php b/tests/run.php index a466d1e9d..9b3445505 100644 --- a/tests/run.php +++ b/tests/run.php @@ -33,12 +33,15 @@ require_once("ImportTestCase.php"); require_once("BooleanTestCase.php"); require_once("EnumTestCase.php"); require_once("RelationAccessTestCase.php"); +require_once("RelationTestCase.php"); require_once("DataDictSqliteTestCase.php"); error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); +$test->addTestCase(new Doctrine_Relation_TestCase()); +/** $test->addTestCase(new Doctrine_EventListenerTestCase()); $test->addTestCase(new Doctrine_RecordTestCase()); @@ -98,7 +101,7 @@ $test->addTestCase(new Doctrine_BooleanTestCase()); $test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_EventListener_Chain_TestCase()); - +*/ //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());