QuerySelectTestCase.php 6.06 KB
Newer Older
1 2
<?php
class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase {
zYne's avatar
zYne committed
3
    public function prepareTables() { }
4 5 6 7 8 9 10
    public function testAggregateFunctionWithDistinctKeyword() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT COUNT(DISTINCT u.name) FROM User u');

        $this->assertEqual($q->getQuery(), 'SELECT COUNT(DISTINCT e.name) AS e__0 FROM entity e WHERE (e.type = 0)');
    }
zYne's avatar
zYne committed
11 12 13 14 15 16

    public function testAggregateFunction() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT COUNT(u.id) FROM User u');

17
        $this->assertEqual($q->getQuery(), 'SELECT COUNT(e.id) AS e__0 FROM entity e WHERE (e.type = 0)');
zYne's avatar
zYne committed
18
    }
19

zYne's avatar
zYne committed
20 21 22 23 24
    public function testMultipleAggregateFunctions() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT MAX(u.id), MIN(u.name) FROM User u');

25
        $this->assertEqual($q->getQuery(), 'SELECT MAX(e.id) AS e__0, MIN(e.name) AS e__1 FROM entity e WHERE (e.type = 0)');
zYne's avatar
zYne committed
26 27 28 29 30 31
    }
    public function testMultipleAggregateFunctionsWithMultipleComponents() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT MAX(u.id), MIN(u.name), COUNT(p.id) FROM User u, u.Phonenumber p');

32
        $this->assertEqual($q->getQuery(), 'SELECT MAX(e.id) AS e__0, MIN(e.name) AS e__1, COUNT(p.id) AS p__2 FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
zYne's avatar
zYne committed
33
    }
zYne's avatar
zYne committed
34 35 36 37 38 39 40 41 42 43 44
    public function testEmptySelectPart() {
        $q = new Doctrine_Query();
        
        try {
            $q->select();
            
            $this->fail();
        } catch(Doctrine_Query_Exception $e) {
            $this->pass();
        }
    }
zYne's avatar
zYne committed
45 46 47 48 49 50 51 52 53 54
    public function testUnknownAggregateFunction() {
        $q = new Doctrine_Query();
        
        try {
            $q->parseQuery('SELECT UNKNOWN(u.id) FROM User');
            $this->fail();
        } catch(Doctrine_Query_Exception $e) {
            $this->pass();
        }
    }
zYne's avatar
zYne committed
55 56 57 58 59 60 61 62 63 64 65 66
    public function testAggregateFunctionValueHydration() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT u.id, COUNT(p.id) FROM User u, u.Phonenumber p GROUP BY u.id');

        $users = $q->execute();
        
        $this->assertEqual($users[0]->Phonenumber->getAggregateValue('COUNT'), 1);
        $this->assertEqual($users[1]->Phonenumber->getAggregateValue('COUNT'), 3);
        $this->assertEqual($users[2]->Phonenumber->getAggregateValue('COUNT'), 1);
        $this->assertEqual($users[3]->Phonenumber->getAggregateValue('COUNT'), 1);
        $this->assertEqual($users[4]->Phonenumber->getAggregateValue('COUNT'), 3);
zYne's avatar
zYne committed
67 68 69 70 71 72 73
    }

    public function testAggregateFunctionValueHydrationWithAliases() {

        $q = new Doctrine_Query();

        $q->parseQuery('SELECT u.id, COUNT(p.id) count FROM User u, u.Phonenumber p GROUP BY u.id');
zYne's avatar
zYne committed
74

zYne's avatar
zYne committed
75 76 77 78 79 80 81
        $users = $q->execute();
        
        $this->assertEqual($users[0]->Phonenumber->getAggregateValue('count'), 1);
        $this->assertEqual($users[1]->Phonenumber->getAggregateValue('count'), 3);
        $this->assertEqual($users[2]->Phonenumber->getAggregateValue('count'), 1);
        $this->assertEqual($users[3]->Phonenumber->getAggregateValue('count'), 1);
        $this->assertEqual($users[4]->Phonenumber->getAggregateValue('count'), 3);
zYne's avatar
zYne committed
82
    }
zYne's avatar
zYne committed
83 84 85
    public function testMultipleAggregateFunctionValueHydrationWithAliases() {

        $q = new Doctrine_Query();
zYne's avatar
zYne committed
86

zYne's avatar
zYne committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        $q->parseQuery('SELECT u.id, COUNT(p.id) count, MAX(p.phonenumber) max FROM User u, u.Phonenumber p GROUP BY u.id');

        $users = $q->execute();

        $this->assertEqual($users[0]->Phonenumber->getAggregateValue('count'), 1);
        $this->assertEqual($users[1]->Phonenumber->getAggregateValue('count'), 3);
        $this->assertEqual($users[2]->Phonenumber->getAggregateValue('count'), 1);
        $this->assertEqual($users[3]->Phonenumber->getAggregateValue('count'), 1);
        $this->assertEqual($users[4]->Phonenumber->getAggregateValue('count'), 3);

        $this->assertEqual($users[0]->Phonenumber->getAggregateValue('max'), '123 123');
        $this->assertEqual($users[1]->Phonenumber->getAggregateValue('max'), '789 789');
        $this->assertEqual($users[2]->Phonenumber->getAggregateValue('max'), '123 123');
        $this->assertEqual($users[3]->Phonenumber->getAggregateValue('max'), '111 222 333');
        $this->assertEqual($users[4]->Phonenumber->getAggregateValue('max'), '444 555');
    }
zYne's avatar
zYne committed
103 104 105 106 107
    public function testSingleComponentWithAsterisk() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT u.* FROM User u');

108
        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id FROM entity e WHERE (e.type = 0)');
zYne's avatar
zYne committed
109 110 111 112 113
    }
    public function testSingleComponentWithMultipleColumns() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT u.name, u.type FROM User u'); 
114
        
115
        $this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, e.type AS e__type FROM entity e WHERE (e.type = 0)');
zYne's avatar
zYne committed
116 117 118 119 120 121
    }
    public function testMultipleComponentsWithAsterisk() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT u.*, p.* FROM User u, u.Phonenumber p');

122
        $this->assertEqual($q->getQuery(),'SELECT e.id AS e__id, e.name AS e__name, e.loginname AS e__loginname, e.password AS e__password, e.type AS e__type, e.created AS e__created, e.updated AS e__updated, e.email_id AS e__email_id, p.id AS p__id, p.phonenumber AS p__phonenumber, p.entity_id AS p__entity_id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
zYne's avatar
zYne committed
123 124 125 126 127 128
    }
    public function testMultipleComponentsWithMultipleColumns() {
        $q = new Doctrine_Query();

        $q->parseQuery('SELECT u.id, u.name, p.id FROM User u, u.Phonenumber p');

129
        $this->assertEqual($q->getQuery(),'SELECT e.id AS e__id, e.name AS e__name, p.id AS p__id FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0)');
130
    }
zYne's avatar
zYne committed
131

132 133
}
?>