RawSqlTestCase.php 10.1 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 *  $Id: RawSqlTestCase.php 1181 2007-03-20 23:22:51Z gnat $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_RawSql_TestCase
 * This class tests the functinality of Doctrine_RawSql component
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision: 1181 $
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase 
{
zYne's avatar
zYne committed
36

37
    public function testQueryParser()
zYne's avatar
zYne committed
38
    {
39
        $sql = 'SELECT {p.*} FROM photos p';
zYne's avatar
zYne committed
40
        $query = new Doctrine_RawSql($this->connection);
zYne's avatar
zYne committed
41 42
        $query->parseQuery($sql);
        
43
        $this->assertEqual($query->getQueryPart('from'), array('photos p'));
zYne's avatar
zYne committed
44 45


46
        $sql = 'SELECT {p.*} FROM (SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE p.can_see = -1 AND t.tag_id = 62 LIMIT 200';
zYne's avatar
zYne committed
47 48
        $query->parseQuery($sql);

49 50 51
        $this->assertEqual($query->getQueryPart('from'), array('(SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id'));
        $this->assertEqual($query->getQueryPart('where'), array('p.can_see = -1 AND t.tag_id = 62'));
        $this->assertEqual($query->getQueryPart('limit'), array(200));
zYne's avatar
zYne committed
52
    }
zYne's avatar
zYne committed
53 54
    public function testAsteriskOperator() 
    {
55 56
        // Selecting with *

zYne's avatar
zYne committed
57
        $query = new Doctrine_RawSql($this->connection);
58
        $query->parseQuery('SELECT {entity.*} FROM entity');
59 60
        $fields = $query->getFields();

61
        $this->assertEqual($fields, array('entity.*'));
62

63
        $query->addComponent('entity', 'Entity');
64 65 66 67 68

        $coll = $query->execute();

        $this->assertEqual($coll->count(), 11);
    }
zYne's avatar
zYne committed
69 70
    public function testLazyPropertyLoading() 
    {
zYne's avatar
zYne committed
71 72
        $query = new Doctrine_RawSql($this->connection);
        $this->connection->clear();
73 74 75

        // selecting proxy objects (lazy property loading)

76
        $query->parseQuery('SELECT {entity.name}, {entity.id} FROM entity');
77 78
        $fields = $query->getFields();

79 80
        $this->assertEqual($fields, array('entity.name', 'entity.id'));
        $query->addComponent('entity', 'Entity');
81 82 83 84 85

        $coll = $query->execute();

        $this->assertEqual($coll->count(), 11);

zYne's avatar
zYne committed
86 87
        $this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_PROXY);
        $this->assertEqual($coll[3]->state(), Doctrine_Record::STATE_PROXY); 
88
    }
zYne's avatar
zYne committed
89 90
    public function testSmartMapping() 
    {
zYne's avatar
zYne committed
91
        $query = new Doctrine_RawSql($this->connection);
92 93
        // smart component mapping (no need for additional addComponent call
        
94
        $query->parseQuery('SELECT {entity.name}, {entity.id} FROM entity');
95 96
        $fields = $query->getFields();

97
        $this->assertEqual($fields, array('entity.name', 'entity.id'));
98 99 100 101 102

        $coll = $query->execute();

        $this->assertEqual($coll->count(), 11);

zYne's avatar
zYne committed
103 104
        $this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_PROXY);
        $this->assertEqual($coll[3]->state(), Doctrine_Record::STATE_PROXY);
105 106
    }

zYne's avatar
zYne committed
107 108
    public function testMultipleComponents() 
    {
zYne's avatar
zYne committed
109
        $query = new Doctrine_RawSql($this->connection);
110 111
        // multi component fetching

112
        $query->parseQuery('SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id');
113

114 115 116 117 118 119 120
        $query->addComponent('entity', 'Entity');

        $query->addComponent('phonenumber', 'Entity.Phonenumber');

        $coll = $query->execute();
        $this->assertEqual($coll->count(), 11);

121
        $count = $this->conn->count();
doctrine's avatar
doctrine committed
122
        
123
        $coll[4]->Phonenumber[0]->phonenumber;
124
        $this->assertEqual($count, $this->conn->count());
125 126

        $coll[5]->Phonenumber[0]->phonenumber;
127
        $this->assertEqual($count, $this->conn->count());
128 129 130 131 132 133 134 135 136 137 138
    }
    public function testAliasesAreSupportedInAddComponent()
    {
        $query = new Doctrine_RawSql();
        $query->parseQuery('SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id');

        $query->addComponent('entity', 'Entity e');
        $query->addComponent('phonenumber', 'e.Phonenumber');

        $this->assertEqual(array_keys($query->getAliasMap()), array('e', 'e.Phonenumber'));

139 140
        $coll = $query->execute();
        $this->assertEqual($coll->count(), 11);
doctrine's avatar
doctrine committed
141

142
        $count = $this->conn->count();
143
        
zYne's avatar
zYne committed
144
        $coll[4]['Phonenumber'][0]['phonenumber'];
145
        $this->assertEqual($count, $this->conn->count());
146

zYne's avatar
zYne committed
147
        $coll[5]['Phonenumber'][0]['phonenumber'];
148
        $this->assertEqual($count, $this->conn->count());
149
    }
150
    public function testPrimaryKeySelectForcing()
zYne's avatar
zYne committed
151
    {
152 153
        // forcing the select of primary key fields
        
zYne's avatar
zYne committed
154
        $query = new Doctrine_RawSql($this->connection);
155

156
        $query->parseQuery('SELECT {entity.name} FROM entity');
157 158 159 160 161 162 163 164
        
        $coll = $query->execute();
        
        $this->assertEqual($coll->count(), 11);
        $this->assertTrue(is_numeric($coll[0]->id));
        $this->assertTrue(is_numeric($coll[3]->id));
        $this->assertTrue(is_numeric($coll[7]->id));
    }
165
    public function testConvenienceMethods()
zYne's avatar
zYne committed
166
    {
zYne's avatar
zYne committed
167
        $query = new Doctrine_RawSql($this->connection);
168
        $query->select('{entity.name}')->from('entity');
169
        $query->addComponent('entity', 'User');
zYne's avatar
zYne committed
170

171 172 173 174 175 176 177
        $coll = $query->execute();

        $this->assertEqual($coll->count(), 8);
        $this->assertTrue(is_numeric($coll[0]->id));
        $this->assertTrue(is_numeric($coll[3]->id));
        $this->assertTrue(is_numeric($coll[7]->id));
    }
178

zYne's avatar
zYne committed
179 180
    public function testColumnAggregationInheritance() 
    {
181
        // forcing the select of primary key fields
182

zYne's avatar
zYne committed
183
        $query = new Doctrine_RawSql($this->connection);
184

185 186
        $query->parseQuery('SELECT {entity.name} FROM entity');
        $query->addComponent('entity', 'User');
187 188 189 190 191 192 193
        $coll = $query->execute();

        $this->assertEqual($coll->count(), 8);
        $this->assertTrue(is_numeric($coll[0]->id));
        $this->assertTrue(is_numeric($coll[3]->id));
        $this->assertTrue(is_numeric($coll[7]->id));
    }
194

zYne's avatar
zYne committed
195 196
    public function testColumnAggregationInheritanceWithOrderBy() 
    {
197 198 199 200
        // forcing the select of primary key fields

        $query = new Doctrine_RawSql($this->connection);

201 202
        $query->parseQuery('SELECT {entity.name} FROM entity ORDER BY entity.name');
        $query->addComponent('entity', 'User');
203 204 205 206 207 208 209 210 211 212 213 214

        $this->assertEqual($query->getQuery(), "SELECT entity.name AS entity__name, entity.id AS entity__id FROM entity WHERE entity.type = 0 ORDER BY entity.name");


        $coll = $query->execute();

        $this->assertEqual($coll->count(), 8);
        $this->assertTrue(is_numeric($coll[0]->id));
        $this->assertTrue(is_numeric($coll[3]->id));
        $this->assertTrue(is_numeric($coll[7]->id));

    }
215

zYne's avatar
zYne committed
216 217
    public function testQueryParser2() 
    {
218 219 220 221 222 223 224
        $query = new Doctrine_RawSql();
        
        $query->parseQuery("SELECT {entity.name} FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");

        $this->assertEqual($query->getQuery(),
        "SELECT entity.name AS entity__name, entity.id AS entity__id FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");
    }
225

zYne's avatar
zYne committed
226
    public function testSelectingWithoutIdentifiersOnRootComponent()
227 228 229
    {
        $query = new Doctrine_RawSql();

zYne's avatar
zYne committed
230
        $query->parseQuery('SELECT {entity.name}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
231 232
        $query->addComponent('entity', 'Entity');
        $query->addComponent('phonenumber', 'Entity.Phonenumber');
zYne's avatar
zYne committed
233 234
        $this->assertEqual($query->getSql(), 'SELECT entity.name AS entity__name, entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
        $coll = $query->execute(array(), Doctrine::FETCH_ARRAY);
235

zYne's avatar
zYne committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
        $this->assertEqual(count($coll), 3);
    }
    
    public function testSwitchingTheFieldOrder()
    {
        $query = new Doctrine_RawSql();

        $query->parseQuery('SELECT {phonenumber.*}, {entity.name} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
        $query->addComponent('entity', 'Entity');
        $query->addComponent('phonenumber', 'Entity.Phonenumber');
        $this->assertEqual($query->getSql(), 'SELECT entity.name AS entity__name, entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
        $coll = $query->execute(array(), Doctrine::FETCH_ARRAY);

        $this->assertEqual(count($coll), 3);
250
    }
251 252
}
?>