Changeset 4342 for trunk/tests/Orm

Show
Ignore:
Timestamp:
05/08/08 15:17:35 (8 months ago)
Author:
romanb
Message:

Continued work on new hydration.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/tests/Orm/Hydration/BasicHydrationTest.php

    r4339 r4342  
    1010    } 
    1111     
    12     /** The data of the hydration mode dataProvider */ 
    13     protected static $hydrationModeProviderData = array( 
    14       array('hydrationMode' => Doctrine::HYDRATE_RECORD), 
    15       array('hydrationMode' => Doctrine::HYDRATE_ARRAY) 
    16     ); 
    1712    /** Getter for the hydration mode dataProvider */ 
    1813    public static function hydrationModeProvider() 
    1914    { 
    20         return self::$hydrationModeProviderData; 
     15        return array( 
     16          array('hydrationMode' => Doctrine::HYDRATE_RECORD), 
     17          array('hydrationMode' => Doctrine::HYDRATE_ARRAY) 
     18        ); 
    2119    } 
    2220     
     
    2422     * Select u.id, u.name from CmsUser u 
    2523     * 
     24     * @dataProvider hydrationModeProvider 
    2625     */ 
    27     public function testBasicHydration() 
     26    public function testNewHydrationSimpleEntityQuery($hydrationMode) 
    2827    { 
    2928        // Faked query components 
     
    4544        // Faked result set 
    4645        $resultSet = array( 
    47             //row1 
    4846            array( 
    4947                'u__id' => '1', 
    5048                'u__name' => 'romanb' 
    5149                ), 
    52             //row2 
    5350            array( 
    5451                'u__id' => '2', 
     
    5956             
    6057        $stmt = new Doctrine_HydratorMockStatement($resultSet); 
    61         $hydrator = new Doctrine_Hydrator(); 
     58        $hydrator = new Doctrine_HydratorNew(); 
    6259        $hydrator->setQueryComponents($queryComponents); 
    6360         
    64         $arrayResult = $hydrator->hydrateResultSet($stmt, $tableAliasMap, Doctrine::HYDRATE_ARRAY);         
    65          
    66         $this->assertTrue(is_array($arrayResult)); 
    67         $this->assertEquals(2, count($arrayResult)); 
    68         $this->assertEquals(1, $arrayResult[0]['id']); 
    69         $this->assertEquals('romanb', $arrayResult[0]['name']); 
    70         $this->assertEquals(2, $arrayResult[1]['id']); 
    71         $this->assertEquals('jwage', $arrayResult[1]['name']); 
    72          
    73         $stmt->setResultSet($resultSet); 
    74         $objectResult = $hydrator->hydrateResultSet($stmt, $tableAliasMap, Doctrine::HYDRATE_RECORD);     
    75          
    76         $this->assertTrue($objectResult instanceof Doctrine_Collection); 
    77         $this->assertEquals(2, count($objectResult)); 
    78         $this->assertTrue($objectResult[0] instanceof Doctrine_Record); 
    79         $this->assertEquals(1, $objectResult[0]->id); 
    80         $this->assertEquals('romanb', $objectResult[0]->name); 
    81         $this->assertTrue($objectResult[1] instanceof Doctrine_Record); 
    82         $this->assertEquals(2, $objectResult[1]->id); 
    83         $this->assertEquals('jwage', $objectResult[1]->name); 
    84          
     61        $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode);         
     62         
     63        $this->assertEquals(2, count($result)); 
     64        $this->assertEquals(1, $result[0]['id']); 
     65        $this->assertEquals('romanb', $result[0]['name']); 
     66        $this->assertEquals(2, $result[1]['id']); 
     67        $this->assertEquals('jwage', $result[1]['name']); 
     68           
     69        if ($hydrationMode == Doctrine::HYDRATE_RECORD) { 
     70            $this->assertTrue($result instanceof Doctrine_Collection); 
     71            $this->assertTrue($result[0] instanceof Doctrine_Record); 
     72            $this->assertTrue($result[1] instanceof Doctrine_Record); 
     73        } else { 
     74            $this->assertTrue(is_array($result)); 
     75        } 
    8576    } 
    8677     
     
    189180     * @dataProvider hydrationModeProvider 
    190181     */ 
    191     public function testNewHydrationBasicsMixedQueryNormalJoin($hydrationMode) 
     182    public function testNewHydrationMixedQueryNormalJoin($hydrationMode) 
    192183    { 
    193184        // Faked query components 
     
    355346            $this->assertEquals(2, count($result[0]['1']['phonenumbers'])); 
    356347        } 
    357          
    358348    } 
    359349     
    360350     
    361 /** 
     351    /** 
    362352     * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic 
    363353     * from User u 
     
    510500    } 
    511501     
    512      
    513      
    514      
    515      
     502    /** 
     503     * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper, a.id, a.topic, 
     504     * c.id, c.topic 
     505     * from User u 
     506     * join u.phonenumbers p 
     507     * join u.articles a 
     508     * left join a.comments c 
     509     * = 
     510     * select u.id, u.status, p.phonenumber, upper(u.name) as u__0, a.id, a.topic, 
     511     * c.id, c.topic 
     512     * from USERS u 
     513     * inner join PHONENUMBERS p ON u.id = p.user_id 
     514     * inner join ARTICLES a ON u.id = a.user_id 
     515     * left outer join COMMENTS c ON a.id = c.article_id 
     516     *  
     517     * @dataProvider hydrationModeProvider 
     518     */ 
     519    public function testNewHydrationMixedQueryMultipleDeepMixedFetchJoin($hydrationMode) 
     520    { 
     521        // Faked query components 
     522        $queryComponents = array( 
     523            'u' => array( 
     524                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'), 
     525                'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'), 
     526                'parent' => null, 
     527                'relation' => null, 
     528                'map' => null, 
     529                'agg' => array('0' => 'nameUpper') 
     530                ), 
     531            'p' => array( 
     532                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsPhonenumber'), 
     533                'mapper' => $this->sharedFixture['connection']->getMapper('CmsPhonenumber'), 
     534                'parent' => 'u', 
     535                'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('phonenumbers'), 
     536                'map' => null 
     537                ), 
     538            'a' => array( 
     539                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsArticle'), 
     540                'mapper' => $this->sharedFixture['connection']->getMapper('CmsArticle'), 
     541                'parent' => 'u', 
     542                'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('articles'), 
     543                'map' => null 
     544                ), 
     545            'c' => array( 
     546                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsComment'), 
     547                'mapper' => $this->sharedFixture['connection']->getMapper('CmsComment'), 
     548                'parent' => 'a', 
     549                'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsArticle')->getRelation('comments'), 
     550                'map' => null 
     551                ), 
     552            ); 
     553         
     554        // Faked table alias map 
     555        $tableAliasMap = array( 
     556            'u' => 'u', 
     557            'p' => 'p', 
     558            'a' => 'a', 
     559            'c' => 'c' 
     560            ); 
     561         
     562        // Faked result set 
     563        $resultSet = array( 
     564            //row1 
     565            array( 
     566                'u__id' => '1', 
     567                'u__status' => 'developer', 
     568                'u__0' => 'ROMANB', 
     569                'p__phonenumber' => '42', 
     570                'a__id' => '1', 
     571                'a__topic' => 'Getting things done!', 
     572                'c__id' => '1', 
     573                'c__topic' => 'First!' 
     574                ), 
     575           array( 
     576                'u__id' => '1', 
     577                'u__status' => 'developer', 
     578                'u__0' => 'ROMANB', 
     579                'p__phonenumber' => '43', 
     580                'a__id' => '1', 
     581                'a__topic' => 'Getting things done!', 
     582                'c__id' => '1', 
     583                'c__topic' => 'First!' 
     584                ), 
     585            array( 
     586                'u__id' => '1', 
     587                'u__status' => 'developer', 
     588                'u__0' => 'ROMANB', 
     589                'p__phonenumber' => '42', 
     590                'a__id' => '2', 
     591                'a__topic' => 'ZendCon', 
     592                'c__id' => null, 
     593                'c__topic' => null 
     594                ), 
     595           array( 
     596                'u__id' => '1', 
     597                'u__status' => 'developer', 
     598                'u__0' => 'ROMANB', 
     599                'p__phonenumber' => '43', 
     600                'a__id' => '2', 
     601                'a__topic' => 'ZendCon', 
     602                'c__id' => null, 
     603                'c__topic' => null 
     604                ), 
     605            array( 
     606                'u__id' => '2', 
     607                'u__status' => 'developer', 
     608                'u__0' => 'JWAGE', 
     609                'p__phonenumber' => '91', 
     610                'a__id' => '3', 
     611                'a__topic' => 'LINQ', 
     612                'c__id' => null, 
     613                'c__topic' => null 
     614                ), 
     615           array( 
     616                'u__id' => '2', 
     617                'u__status' => 'developer', 
     618                'u__0' => 'JWAGE', 
     619                'p__phonenumber' => '91', 
     620                'a__id' => '4', 
     621                'a__topic' => 'PHP6', 
     622                'c__id' => null, 
     623                'c__topic' => null 
     624                ), 
     625            ); 
     626             
     627        $stmt = new Doctrine_HydratorMockStatement($resultSet); 
     628        $hydrator = new Doctrine_HydratorNew(); 
     629        $hydrator->setQueryComponents($queryComponents); 
     630         
     631        $hydrator->setResultMixed(true); 
     632         
     633        $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode); 
     634        if ($hydrationMode == Doctrine::HYDRATE_ARRAY) { 
     635            //var_dump($result); 
     636        } 
     637         
     638        $this->assertEquals(2, count($result)); 
     639        $this->assertTrue(is_array($result)); 
     640        $this->assertTrue(is_array($result[0])); 
     641        $this->assertTrue(is_array($result[1])); 
     642         
     643        // first user => 2 phonenumbers, 2 articles, 1 comment on first article 
     644        $this->assertEquals(2, count($result[0][0]['phonenumbers'])); 
     645        $this->assertEquals(2, count($result[0][0]['articles'])); 
     646        $this->assertEquals(1, count($result[0][0]['articles'][0]['comments'])); 
     647        $this->assertEquals('ROMANB', $result[0]['nameUpper']); 
     648        // second user => 1 phonenumber, 2 articles, no comments 
     649        $this->assertEquals(1, count($result[1][0]['phonenumbers'])); 
     650        $this->assertEquals(2, count($result[1][0]['articles'])); 
     651        $this->assertEquals('JWAGE', $result[1]['nameUpper']); 
     652         
     653        $this->assertEquals(42, $result[0][0]['phonenumbers'][0]['phonenumber']); 
     654        $this->assertEquals(43, $result[0][0]['phonenumbers'][1]['phonenumber']); 
     655        $this->assertEquals(91, $result[1][0]['phonenumbers'][0]['phonenumber']); 
     656         
     657        $this->assertEquals('Getting things done!', $result[0][0]['articles'][0]['topic']); 
     658        $this->assertEquals('ZendCon', $result[0][0]['articles'][1]['topic']); 
     659        $this->assertEquals('LINQ', $result[1][0]['articles'][0]['topic']); 
     660        $this->assertEquals('PHP6', $result[1][0]['articles'][1]['topic']); 
     661         
     662        $this->assertEquals('First!', $result[0][0]['articles'][0]['comments'][0]['topic']); 
     663 
     664        $this->assertTrue(isset($result[0][0]['articles'][0]['comments'])); 
     665        $this->assertFalse(isset($result[0][0]['articles'][1]['comments'])); 
     666        $this->assertFalse(isset($result[1][0]['articles'][0]['comments'])); 
     667        $this->assertFalse(isset($result[1][0]['articles'][1]['comments'])); 
     668         
     669        if ($hydrationMode == Doctrine::HYDRATE_RECORD) { 
     670            $this->assertTrue($result[0][0] instanceof Doctrine_Record); 
     671            $this->assertTrue($result[1][0] instanceof Doctrine_Record); 
     672            // phonenumbers 
     673            $this->assertTrue($result[0][0]['phonenumbers'] instanceof Doctrine_Collection); 
     674            $this->assertTrue($result[0][0]['phonenumbers'][0] instanceof Doctrine_Record); 
     675            $this->assertTrue($result[0][0]['phonenumbers'][1] instanceof Doctrine_Record); 
     676            $this->assertTrue($result[1][0]['phonenumbers'] instanceof Doctrine_Collection); 
     677            $this->assertTrue($result[1][0]['phonenumbers'][0] instanceof Doctrine_Record); 
     678            // articles 
     679            $this->assertTrue($result[0][0]['articles'] instanceof Doctrine_Collection); 
     680            $this->assertTrue($result[0][0]['articles'][0] instanceof Doctrine_Record); 
     681            $this->assertTrue($result[0][0]['articles'][1] instanceof Doctrine_Record); 
     682            $this->assertTrue($result[1][0]['articles'][0] instanceof Doctrine_Record); 
     683            $this->assertTrue($result[1][0]['articles'][1] instanceof Doctrine_Record); 
     684            // article comments 
     685            $this->assertTrue($result[0][0]['articles'][0]['comments'] instanceof Doctrine_Collection); 
     686            $this->assertTrue($result[0][0]['articles'][0]['comments'][0] instanceof Doctrine_Record); 
     687        } 
     688    } 
     689     
     690     
     691    /** 
     692     * Tests that the hydrator does not rely on a particular order of the rows 
     693     * in the result set. 
     694     *  
     695     * DQL: 
     696     * select c.id, c.position, c.name, b.id, b.position 
     697     * from ForumCategory c inner join c.boards b 
     698     * order by c.position asc, b.position asc 
     699     *  
     700     * Checks whether the boards are correctly assigned to the categories. 
     701     * 
     702     * The 'evil' result set that confuses the object population is displayed below. 
     703     *  
     704     * c.id  | c.position | c.name   | boardPos | b.id | b.category_id (just for clarity) 
     705     *  1    | 0          | First    | 0        |   1  | 1 
     706     *  2    | 0          | Second   | 0        |   2  | 2   <-- 
     707     *  1    | 0          | First    | 1        |   3  | 1 
     708     *  1    | 0          | First    | 2        |   4  | 1 
     709     *  
     710     * @dataProvider hydrationModeProvider 
     711     */ 
     712    public function testNewHydrationEntityQueryCustomResultSetOrder($hydrationMode) 
     713    { 
     714        // Faked query components 
     715        $queryComponents = array( 
     716            'c' => array( 
     717                'table' => $this->sharedFixture['connection']->getClassMetadata('ForumCategory'), 
     718                'mapper' => $this->sharedFixture['connection']->getMapper('ForumCategory'), 
     719                'parent' => null, 
     720                'relation' => null, 
     721                'map' => null 
     722                ), 
     723            'b' => array( 
     724                'table' => $this->sharedFixture['connection']->getClassMetadata('ForumBoard'), 
     725                'mapper' => $this->sharedFixture['connection']->getMapper('ForumBoard'), 
     726                'parent' => 'c', 
     727                'relation' => $this->sharedFixture['connection']->getClassMetadata('ForumCategory')->getRelation('boards'), 
     728                'map' => null 
     729                ), 
     730            ); 
     731         
     732        // Faked table alias map 
     733        $tableAliasMap = array( 
     734            'c' => 'c', 
     735            'b' => 'b' 
     736            ); 
     737         
     738        // Faked result set 
     739        $resultSet = array( 
     740            array( 
     741                'c__id' => '1', 
     742                'c__position' => '0', 
     743                'c__name' => 'First', 
     744                'b__id' => '1', 
     745                'b__position' => '0', 
     746                'b__category_id' => '1' 
     747                ), 
     748           array( 
     749                'c__id' => '2', 
     750                'c__position' => '0', 
     751                'c__name' => 'Second', 
     752                'b__id' => '2', 
     753                'b__position' => '0', 
     754                'b__category_id' => '2' 
     755                ), 
     756            array( 
     757                'c__id' => '1', 
     758                'c__position' => '0', 
     759                'c__name' => 'First', 
     760                'b__id' => '3', 
     761                'b__position' => '1', 
     762                'b__category_id' => '1' 
     763                ), 
     764           array( 
     765                'c__id' => '1', 
     766                'c__position' => '0', 
     767                'c__name' => 'First', 
     768                'b__id' => '4', 
     769                'b__position' => '2', 
     770                'b__category_id' => '1' 
     771                ) 
     772            ); 
     773             
     774        $stmt = new Doctrine_HydratorMockStatement($resultSet); 
     775        $hydrator = new Doctrine_HydratorNew(); 
     776        $hydrator->setQueryComponents($queryComponents); 
     777         
     778        //$hydrator->setResultMixed(true); 
     779         
     780        $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode); 
     781        if ($hydrationMode == Doctrine::HYDRATE_ARRAY) { 
     782            //var_dump($result); 
     783        } 
     784         
     785        $this->assertEquals(2, count($result)); 
     786        $this->assertTrue(isset($result[0]['boards'])); 
     787        $this->assertEquals(3, count($result[0]['boards'])); 
     788        $this->assertTrue(isset($result[1]['boards'])); 
     789        $this->assertEquals(1, count($result[1]['boards'])); 
     790         
     791        if ($hydrationMode == Doctrine::HYDRATE_ARRAY) { 
     792            $this->assertTrue(is_array($result)); 
     793            $this->assertTrue(is_array($result[0])); 
     794            $this->assertTrue(is_array($result[1])); 
     795        } else { 
     796            $this->assertTrue($result instanceof Doctrine_Collection); 
     797            $this->assertTrue($result[0] instanceof Doctrine_Record); 
     798            $this->assertTrue($result[1] instanceof Doctrine_Record); 
     799        }      
     800 
     801    } 
    516802}