428TestCase.php 1.92 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * Doctrine_Ticket_428_TestCase
 *
 * @package     Doctrine
 * @author      Tamcy <7am.online@gmail.com>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
10
 * @link        www.phpdoctrine.org
11 12 13 14 15
 * @since       1.0
 * @version     $Revision$
 */
class Doctrine_Ticket_428_TestCase extends Doctrine_UnitTestCase
{
16 17
    private $_albums;
    
18 19 20 21 22 23
    public function prepareTables()
    {
        $this->tables = array('Album', 'Song');
        parent::prepareTables();
    }
    
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    public function prepareData()
    {
    }

    public function testInitData() 
    {
        // Since the tests do a $this->objTable()->clear() before each method call
        // using the User model is not recommended for this test
        $albums = new Doctrine_Collection('Album');

        $albums[0]->name = 'Revolution';
        $albums[0]->Song[0]->title = 'Revolution';
        $albums[0]->Song[1]->title = 'Hey Jude';
        $albums[0]->Song[2]->title = 'Across the Universe';
        $albums[0]->Song[3]->title = 'Michelle';
        $albums->save();
        $this->assertEqual(count($albums[0]->Song), 4);
41
        $this->_albums = $albums;
42 43 44 45
    }

    public function testAggregateValueMappingSupportsLeftJoins() 
    {
46 47 48 49
        foreach ($this->_albums as $album) {
            $album->clearRelated();
        }
        
50 51 52 53
        $q = new Doctrine_Query();

        $q->select('a.name, COUNT(s.id) count')->from('Album a')->leftJoin('a.Song s')->groupby('a.id');
        $albums = $q->execute();
54
        
55 56 57 58 59 60 61 62 63 64 65
        // Should not reuse the existing collection in this case
        $this->assertEqual(count($albums[0]->Song), 1);

        try {
            // Collection[0] should refer to the object with aggregate value
            $this->assertEqual($albums[0]['Song'][0]['count'], 4);
        } catch (Exception $e) {
            $this->fail('count aggregate value should be available');
        }
    }
}