Changeset 4338 for trunk/tests/Orm

Show
Ignore:
Timestamp:
05/06/08 14:41:22 (8 months ago)
Author:
romanb
Message:

Refactorings. Started with new hydrator for 2.0.

Location:
trunk/tests/Orm
Files:
2 modified

Legend:

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

    r4225 r4338  
    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    ); 
     17    /** Getter for the hydration mode dataProvider */ 
     18    public static function hydrationModeProvider() 
     19    { 
     20        return self::$hydrationModeProviderData; 
     21    } 
     22     
    1223    /** 
    13      * Fakes the DQL query: select u.id, u.name from CmsUser u 
     24     * Select u.id, u.name from CmsUser u 
    1425     * 
    1526     */ 
    16     public function testBasic() 
     27    public function testBasicHydration() 
    1728    { 
    1829        // Faked query components 
     
    7384         
    7485    } 
     86     
     87    /** 
     88     * select u.id, u.status, p.phonenumber, upper(u.name) nameUpper from User u 
     89     * join u.phonenumbers p 
     90     * = 
     91     * select u.id, u.status, p.phonenumber, upper(u.name) as u__0 from USERS u 
     92     * INNER JOIN PHONENUMBERS p ON u.id = p.user_id 
     93     *  
     94     * @dataProvider hydrationModeProvider 
     95     */ 
     96    public function testNewHydrationMixedQueryFetchJoin($hydrationMode) 
     97    { 
     98        // Faked query components 
     99        $queryComponents = array( 
     100            'u' => array( 
     101                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'), 
     102                'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'), 
     103                'parent' => null, 
     104                'relation' => null, 
     105                'map' => null, 
     106                'agg' => array('0' => 'nameUpper') 
     107                ), 
     108            'p' => array( 
     109                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsPhonenumber'), 
     110                'mapper' => $this->sharedFixture['connection']->getMapper('CmsPhonenumber'), 
     111                'parent' => 'u', 
     112                'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('phonenumbers'), 
     113                'map' => null 
     114                ) 
     115            ); 
     116         
     117        // Faked table alias map 
     118        $tableAliasMap = array( 
     119            'u' => 'u', 
     120            'p' => 'p' 
     121            ); 
     122         
     123        // Faked result set 
     124        $resultSet = array( 
     125            //row1 
     126            array( 
     127                'u__id' => '1', 
     128                'u__status' => 'developer', 
     129                'u__0' => 'ROMANB', 
     130                'p__phonenumber' => '42', 
     131                ), 
     132            array( 
     133                'u__id' => '1', 
     134                'u__status' => 'developer', 
     135                'u__0' => 'ROMANB', 
     136                'p__phonenumber' => '43', 
     137                ), 
     138            array( 
     139                'u__id' => '2', 
     140                'u__status' => 'developer', 
     141                'u__0' => 'JWAGE', 
     142                'p__phonenumber' => '91' 
     143                ) 
     144            ); 
     145             
     146        $stmt = new Doctrine_HydratorMockStatement($resultSet); 
     147        $hydrator = new Doctrine_HydratorNew(); 
     148        $hydrator->setQueryComponents($queryComponents); 
     149         
     150        $hydrator->setResultMixed(true); 
     151         
     152        $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode); 
     153        //var_dump($result); 
     154         
     155        $this->assertEquals(2, count($result)); 
     156        $this->assertTrue(is_array($result)); 
     157        $this->assertTrue(is_array($result[0])); 
     158        $this->assertTrue(is_array($result[1])); 
     159         
     160        $this->assertEquals(3, count($result[0][0])); 
     161        // first user => 2 phonenumbers 
     162        $this->assertEquals(2, count($result[0][0]['phonenumbers'])); 
     163        $this->assertEquals('ROMANB', $result[0]['nameUpper']); 
     164        // second user => 1 phonenumber 
     165        $this->assertEquals(1, count($result[1][0]['phonenumbers'])); 
     166        $this->assertEquals('JWAGE', $result[1]['nameUpper']); 
     167         
     168        $this->assertEquals(42, $result[0][0]['phonenumbers'][0]['phonenumber']); 
     169        $this->assertEquals(43, $result[0][0]['phonenumbers'][1]['phonenumber']); 
     170        $this->assertEquals(91, $result[1][0]['phonenumbers'][0]['phonenumber']); 
     171         
     172        if ($hydrationMode == Doctrine::HYDRATE_RECORD) { 
     173            $this->assertTrue($result[0][0] instanceof Doctrine_Record); 
     174            $this->assertTrue($result[0][0]['phonenumbers'] instanceof Doctrine_Collection); 
     175            $this->assertTrue($result[0][0]['phonenumbers'][0] instanceof Doctrine_Record); 
     176            $this->assertTrue($result[0][0]['phonenumbers'][1] instanceof Doctrine_Record); 
     177            $this->assertTrue($result[1][0] instanceof Doctrine_Record); 
     178            $this->assertTrue($result[1][0]['phonenumbers'] instanceof Doctrine_Collection); 
     179        }  
     180    } 
     181     
     182    /** 
     183     * select u.id, u.status, count(p.phonenumber) numPhones from User u 
     184     * join u.phonenumbers p group by u.status, u.id 
     185     * = 
     186     * select u.id, u.status, count(p.phonenumber) as p__0 from USERS u 
     187     * INNER JOIN PHONENUMBERS p ON u.id = p.user_id group by u.id, u.status 
     188     *  
     189     * @dataProvider hydrationModeProvider 
     190     */ 
     191    public function testNewHydrationBasicsMixedQueryNormalJoin($hydrationMode) 
     192    { 
     193        // Faked query components 
     194        $queryComponents = array( 
     195            'u' => array( 
     196                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'), 
     197                'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'), 
     198                'parent' => null, 
     199                'relation' => null, 
     200                'map' => null 
     201                ), 
     202            'p' => array( 
     203                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsPhonenumber'), 
     204                'mapper' => $this->sharedFixture['connection']->getMapper('CmsPhonenumber'), 
     205                'parent' => 'u', 
     206                'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('phonenumbers'), 
     207                'map' => null, 
     208                'agg' => array('0' => 'numPhones') 
     209                ) 
     210            ); 
     211         
     212        // Faked table alias map 
     213        $tableAliasMap = array( 
     214            'u' => 'u', 
     215            'p' => 'p' 
     216            ); 
     217         
     218        // Faked result set 
     219        $resultSet = array( 
     220            //row1 
     221            array( 
     222                'u__id' => '1', 
     223                'u__status' => 'developer', 
     224                'p__0' => '2', 
     225                ), 
     226            array( 
     227                'u__id' => '2', 
     228                'u__status' => 'developer', 
     229                'p__0' => '1', 
     230                ) 
     231            ); 
     232         
     233             
     234        $stmt = new Doctrine_HydratorMockStatement($resultSet); 
     235        $hydrator = new Doctrine_HydratorNew(); 
     236        $hydrator->setQueryComponents($queryComponents); 
     237         
     238        $hydrator->setResultMixed(true); 
     239         
     240        $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode); 
     241        //var_dump($result); 
     242         
     243        $this->assertEquals(2, count($result)); 
     244        $this->assertTrue(is_array($result)); 
     245        $this->assertTrue(is_array($result[0])); 
     246        $this->assertTrue(is_array($result[1])); 
     247         
     248        // first user => 2 phonenumbers 
     249        $this->assertEquals(2, $result[0]['numPhones']); 
     250        // second user => 1 phonenumber 
     251        $this->assertEquals(1, $result[1]['numPhones']); 
     252         
     253        if ($hydrationMode == Doctrine::HYDRATE_RECORD) { 
     254            $this->assertTrue($result[0][0] instanceof Doctrine_Record); 
     255            $this->assertTrue($result[1][0] instanceof Doctrine_Record); 
     256        } 
     257    } 
     258     
     259    /**  
     260     * select u.id, u.status, upper(u.name) nameUpper from User u index by u.id 
     261     * join u.phonenumbers p indexby p.phonenumber 
     262     * = 
     263     * select u.id, u.status, upper(u.name) as p__0 from USERS u 
     264     * INNER JOIN PHONENUMBERS p ON u.id = p.user_id 
     265     *  
     266     * @dataProvider hydrationModeProvider 
     267     */ 
     268    public function testNewHydrationMixedQueryFetchJoinCustomIndex($hydrationMode) 
     269    { 
     270        // Faked query components 
     271        $queryComponents = array( 
     272            'u' => array( 
     273                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsUser'), 
     274                'mapper' => $this->sharedFixture['connection']->getMapper('CmsUser'), 
     275                'parent' => null, 
     276                'relation' => null, 
     277                'agg' => array('0' => 'nameUpper'), 
     278                'map' => 'id' 
     279                ), 
     280            'p' => array( 
     281                'table' => $this->sharedFixture['connection']->getClassMetadata('CmsPhonenumber'), 
     282                'mapper' => $this->sharedFixture['connection']->getMapper('CmsPhonenumber'), 
     283                'parent' => 'u', 
     284                'relation' => $this->sharedFixture['connection']->getClassMetadata('CmsUser')->getRelation('phonenumbers'), 
     285                'map' => 'phonenumber' 
     286                ) 
     287            ); 
     288         
     289        // Faked table alias map 
     290        $tableAliasMap = array( 
     291            'u' => 'u', 
     292            'p' => 'p' 
     293            ); 
     294         
     295        // Faked result set 
     296        $resultSet = array( 
     297            //row1 
     298            array( 
     299                'u__id' => '1', 
     300                'u__status' => 'developer', 
     301                'u__0' => 'ROMANB', 
     302                'p__phonenumber' => '42', 
     303                ), 
     304            array( 
     305                'u__id' => '1', 
     306                'u__status' => 'developer', 
     307                'u__0' => 'ROMANB', 
     308                'p__phonenumber' => '43', 
     309                ), 
     310            array( 
     311                'u__id' => '2', 
     312                'u__status' => 'developer', 
     313                'u__0' => 'JWAGE', 
     314                'p__phonenumber' => '91' 
     315                ) 
     316            ); 
     317         
     318             
     319        $stmt = new Doctrine_HydratorMockStatement($resultSet); 
     320        $hydrator = new Doctrine_HydratorNew(); 
     321        $hydrator->setQueryComponents($queryComponents); 
     322         
     323        // give the hydrator an artificial hint 
     324        $hydrator->setResultMixed(true); 
     325         
     326        $result = $hydrator->hydrateResultSet($stmt, $tableAliasMap, $hydrationMode); 
     327        if ($hydrationMode == Doctrine::HYDRATE_ARRAY) { 
     328            //var_dump($result); 
     329        } 
     330         
     331        $this->assertEquals(2, count($result)); 
     332        $this->assertTrue(is_array($result)); 
     333        $this->assertTrue(is_array($result[0])); 
     334        $this->assertTrue(is_array($result[1])); 
     335         
     336         
     337        // first user => 2 phonenumbers. notice the custom indexing by user id 
     338        $this->assertEquals(2, count($result[0]['1']['phonenumbers'])); 
     339        // second user => 1 phonenumber. notice the custom indexing by user id 
     340        $this->assertEquals(1, count($result[1]['2']['phonenumbers'])); 
     341         
     342        // test the custom indexing of the phonenumbers 
     343        $this->assertTrue(isset($result[0]['1']['phonenumbers']['42'])); 
     344        $this->assertTrue(isset($result[0]['1']['phonenumbers']['43'])); 
     345        $this->assertTrue(isset($result[1]['2']['phonenumbers']['91'])); 
     346         
     347        // test the scalar values 
     348        $this->assertEquals('ROMANB', $result[0]['nameUpper']); 
     349        $this->assertEquals('JWAGE', $result[1]['nameUpper']); 
     350         
     351        if ($hydrationMode == Doctrine::HYDRATE_RECORD) { 
     352            $this->assertTrue($result[0]['1'] instanceof Doctrine_Record); 
     353            $this->assertTrue($result[1]['2'] instanceof Doctrine_Record); 
     354            $this->assertTrue($result[0]['1']['phonenumbers'] instanceof Doctrine_Collection); 
     355            $this->assertEquals(2, count($result[0]['1']['phonenumbers'])); 
     356        } 
     357         
     358    } 
     359     
     360     
     361     
     362     
     363     
     364     
     365     
     366     
    75367} 
  • trunk/tests/Orm/UnitOfWorkTestCase.php

    r4328 r4338  
    1919    public function testRegisterNew() 
    2020    { 
     21        $this->_user->username = 'romanb'; 
     22        $this->_user->id = 1; 
    2123        $this->_unitOfWork->registerNew($this->_user); 
    2224        $this->assertFalse($this->_unitOfWork->contains($this->_user)); 
     
    3638        $this->assertFalse($this->_unitOfWork->isRegisteredNew($this->_user)); 
    3739        $this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user)); 
    38          
     40    } 
     41     
     42    public function testRegisterRemovedOnTransientEntityIsIgnored() 
     43    { 
     44        $this->_user->username = 'romanb'; 
     45        $this->_user->id = 1; 
     46        $this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user)); 
     47        $this->_unitOfWork->registerRemoved($this->_user); 
     48        $this->assertFalse($this->_unitOfWork->isRegisteredRemoved($this->_user));         
    3949    } 
    4050