From f92773fa1cf50454a16d38e8b1a246a695f5a1be Mon Sep 17 00:00:00 2001 From: romanb Date: Tue, 6 May 2008 21:03:31 +0000 Subject: [PATCH] Added another hydration test. --- lib/Doctrine/Record.php | 2 +- tests/Orm/ClassMetadataTestCase.php | 18 +++ tests/Orm/Hydration/BasicHydrationTest.php | 151 ++++++++++++++++++++- tests/models/cms/CmsArticle.php | 11 ++ tests/models/cms/CmsUser.php | 2 + 5 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 tests/Orm/ClassMetadataTestCase.php create mode 100644 tests/models/cms/CmsArticle.php diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 5a551f619..5e8244ed4 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -31,7 +31,7 @@ * @link www.phpdoctrine.org * @since 1.0 * @version $Revision$ - * @todo Rename to "Entity". Split up into "Entity" and "ActiveRecord (extends Entity)"??? + * @todo Rename to "Entity". Split up into "Entity" and "ActiveEntity (extends Entity)"??? * @todo Remove as many methods as possible. */ abstract class Doctrine_Record extends Doctrine_Access implements Countable, IteratorAggregate, Serializable diff --git a/tests/Orm/ClassMetadataTestCase.php b/tests/Orm/ClassMetadataTestCase.php new file mode 100644 index 000000000..fc1479de1 --- /dev/null +++ b/tests/Orm/ClassMetadataTestCase.php @@ -0,0 +1,18 @@ + array( + 'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'), + 'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'), + 'parent' => null, + 'relation' => null, + 'map' => null, + 'agg' => array('0' => 'nameUpper') + ), + 'p' => array( + 'table' => $this->sharedFixture['connection']->getClassMetadata('CmsPhonenumber'), + 'mapper' => $this->sharedFixture['connection']->getMapper('CmsPhonenumber'), + 'parent' => 'u', + 'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('phonenumbers'), + 'map' => null + ), + 'a' => array( + 'table' => $this->sharedFixture['connection']->getClassMetadata('CmsArticle'), + 'mapper' => $this->sharedFixture['connection']->getMapper('CmsArticle'), + 'parent' => 'u', + 'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('articles'), + 'map' => null + ), + ); + + // Faked table alias map + $tableAliasMap = array( + 'u' => 'u', + 'p' => 'p', + 'a' => 'a' + ); + + // Faked result set + $resultSet = array( + //row1 + array( + 'u__id' => '1', + 'u__status' => 'developer', + 'u__0' => 'ROMANB', + 'p__phonenumber' => '42', + 'a__id' => '1', + 'a__topic' => 'Getting things done!' + ), + array( + 'u__id' => '1', + 'u__status' => 'developer', + 'u__0' => 'ROMANB', + 'p__phonenumber' => '43', + 'a__id' => '1', + 'a__topic' => 'Getting things done!' + ), + array( + 'u__id' => '1', + 'u__status' => 'developer', + 'u__0' => 'ROMANB', + 'p__phonenumber' => '42', + 'a__id' => '2', + 'a__topic' => 'ZendCon' + ), + array( + 'u__id' => '1', + 'u__status' => 'developer', + 'u__0' => 'ROMANB', + 'p__phonenumber' => '43', + 'a__id' => '2', + 'a__topic' => 'ZendCon' + ), + array( + 'u__id' => '2', + 'u__status' => 'developer', + 'u__0' => 'JWAGE', + 'p__phonenumber' => '91', + 'a__id' => '3', + 'a__topic' => 'LINQ' + ), + array( + 'u__id' => '2', + 'u__status' => 'developer', + 'u__0' => 'JWAGE', + 'p__phonenumber' => '91', + 'a__id' => '4', + 'a__topic' => 'PHP6' + ), + ); + + $stmt = new Doctrine_HydratorMockStatement($resultSet); + $hydrator = new Doctrine_HydratorNew(); + $hydrator->setQueryComponents($queryComponents); + + $hydrator->setResultMixed(true); + + $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode); + if ($hydrationMode == Doctrine::HYDRATE_ARRAY) { + //var_dump($result); + } + + $this->assertEquals(2, count($result)); + $this->assertTrue(is_array($result)); + $this->assertTrue(is_array($result[0])); + $this->assertTrue(is_array($result[1])); + + // first user => 2 phonenumbers, 2 articles + $this->assertEquals(2, count($result[0][0]['phonenumbers'])); + $this->assertEquals(2, count($result[0][0]['articles'])); + $this->assertEquals('ROMANB', $result[0]['nameUpper']); + // second user => 1 phonenumber, 2 articles + $this->assertEquals(1, count($result[1][0]['phonenumbers'])); + $this->assertEquals(2, count($result[1][0]['articles'])); + $this->assertEquals('JWAGE', $result[1]['nameUpper']); + + $this->assertEquals(42, $result[0][0]['phonenumbers'][0]['phonenumber']); + $this->assertEquals(43, $result[0][0]['phonenumbers'][1]['phonenumber']); + $this->assertEquals(91, $result[1][0]['phonenumbers'][0]['phonenumber']); + + $this->assertEquals('Getting things done!', $result[0][0]['articles'][0]['topic']); + $this->assertEquals('ZendCon', $result[0][0]['articles'][1]['topic']); + $this->assertEquals('LINQ', $result[1][0]['articles'][0]['topic']); + $this->assertEquals('PHP6', $result[1][0]['articles'][1]['topic']); + + if ($hydrationMode == Doctrine::HYDRATE_RECORD) { + $this->assertTrue($result[0][0] instanceof Doctrine_Record); + $this->assertTrue($result[0][0]['phonenumbers'] instanceof Doctrine_Collection); + $this->assertTrue($result[0][0]['phonenumbers'][0] instanceof Doctrine_Record); + $this->assertTrue($result[0][0]['phonenumbers'][1] instanceof Doctrine_Record); + $this->assertTrue($result[0][0]['articles'] instanceof Doctrine_Collection); + $this->assertTrue($result[0][0]['articles'][0] instanceof Doctrine_Record); + $this->assertTrue($result[0][0]['articles'][1] instanceof Doctrine_Record); + $this->assertTrue($result[1][0] instanceof Doctrine_Record); + $this->assertTrue($result[1][0]['phonenumbers'] instanceof Doctrine_Collection); + $this->assertTrue($result[1][0]['phonenumbers'][0] instanceof Doctrine_Record); + $this->assertTrue($result[1][0]['articles'][0] instanceof Doctrine_Record); + $this->assertTrue($result[1][0]['articles'][1] instanceof Doctrine_Record); + } + } diff --git a/tests/models/cms/CmsArticle.php b/tests/models/cms/CmsArticle.php new file mode 100644 index 000000000..9de10e6d9 --- /dev/null +++ b/tests/models/cms/CmsArticle.php @@ -0,0 +1,11 @@ +mapColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true)); + $class->mapColumn('topic', 'string', 255); + $class->mapColumn('text', 'string'); + $class->mapColumn('user_id', 'integer', 4); + } +} diff --git a/tests/models/cms/CmsUser.php b/tests/models/cms/CmsUser.php index af8f17a2e..3c03f0879 100644 --- a/tests/models/cms/CmsUser.php +++ b/tests/models/cms/CmsUser.php @@ -9,5 +9,7 @@ class CmsUser extends Doctrine_Record $class->hasMany('CmsPhonenumber as phonenumbers', array( 'local' => 'id', 'foreign' => 'user_id')); + $class->hasMany('CmsArticle as articles', array( + 'local' => 'id', 'foreign' => 'user_id')); } }