1<?php 2/* 3 * $Id$ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information, see 19 * <http://www.doctrine-project.org>. 20 */ 21 22/** 23 * Doctrine_Collection_TestCase 24 * 25 * @package Doctrine 26 * @author Konsta Vesterinen <kvesteri@cc.hut.fi> 27 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 28 * @category Object Relational Mapping 29 * @link www.doctrine-project.org 30 * @since 1.0 31 * @version $Revision$ 32 */ 33class Doctrine_Collection_TestCase extends Doctrine_UnitTestCase 34{ 35 public function testLoadRelatedForAssociation() 36 { 37 $coll = $this->connection->query('FROM User'); 38 39 $this->assertEqual($coll->count(), 8); 40 41 $coll[0]->Group[1]->name = 'Actors House 2'; 42 43 $coll[0]->Group[2]->name = 'Actors House 3'; 44 45 $coll[2]->Group[0]->name = 'Actors House 4'; 46 $coll[2]->Group[1]->name = 'Actors House 5'; 47 $coll[2]->Group[2]->name = 'Actors House 6'; 48 49 $coll[5]->Group[0]->name = 'Actors House 7'; 50 $coll[5]->Group[1]->name = 'Actors House 8'; 51 $coll[5]->Group[2]->name = 'Actors House 9'; 52 53 $coll->save(); 54 55 $this->connection->clear(); 56 57 $coll = $this->connection->query('FROM User'); 58 59 $this->assertEqual($coll->count(), 8); 60 $this->assertEqual($coll[0]->Group->count(), 2); 61 $this->assertEqual($coll[1]->Group->count(), 1); 62 $this->assertEqual($coll[2]->Group->count(), 3); 63 $this->assertEqual($coll[5]->Group->count(), 3); 64 65 $this->connection->clear(); 66 67 $coll = $this->connection->query('FROM User'); 68 69 $this->assertEqual($coll->count(), 8); 70 71 $count = $this->connection->count(); 72 73 $coll->loadRelated('Group'); 74 $this->assertEqual(($count + 1), $this->connection->count()); 75 $this->assertEqual($coll[0]->Group->count(), 2); 76 $this->assertEqual(($count + 1), $this->connection->count()); 77 $this->assertEqual($coll[1]->Group->count(), 1); 78 79 $this->assertEqual(($count + 1), $this->connection->count()); 80 81 $this->assertEqual($coll[2]->Group->count(), 3); 82 83 $this->assertEqual(($count + 1), $this->connection->count()); 84 $this->assertEqual($coll[5]->Group->count(), 3); 85 86 $this->assertEqual(($count + 1), $this->connection->count()); 87 88 $this->connection->clear(); 89 } 90 91 public function testOffsetGetWithNullArgumentReturnsNewRecord() 92 { 93 $coll = new Doctrine_Collection('User'); 94 $this->assertEqual($coll->count(), 0); 95 96 $coll[]->name = 'zYne'; 97 98 $this->assertEqual($coll->count(), 1); 99 $this->assertEqual($coll[0]->name, 'zYne'); 100 } 101 102 public function testLoadRelatedForNormalAssociation() 103 { 104 $resource = new Doctrine_Collection('Resource'); 105 $resource[0]->name = 'resource 1'; 106 $resource[0]->Type[0]->type = 'type 1'; 107 $resource[0]->Type[1]->type = 'type 2'; 108 $resource[1]->name = 'resource 2'; 109 $resource[1]->Type[0]->type = 'type 3'; 110 $resource[1]->Type[1]->type = 'type 4'; 111 112 $resource->save(); 113 114 $this->connection->clear(); 115 116 $resources = $this->connection->query('FROM Resource'); 117 118 $count = $this->connection->count(); 119 $resources->loadRelated('Type'); 120 121 $this->assertEqual(($count + 1), $this->connection->count()); 122 $this->assertEqual($resources[0]->name, 'resource 1'); 123 $this->assertEqual($resource[0]->Type[0]->type, 'type 1'); 124 $this->assertEqual($resource[0]->Type[1]->type, 'type 2'); 125 $this->assertEqual(($count + 1), $this->connection->count()); 126 127 $this->assertEqual($resource[1]->name, 'resource 2'); 128 $this->assertEqual($resource[1]->Type[0]->type, 'type 3'); 129 $this->assertEqual($resource[1]->Type[1]->type, 'type 4'); 130 $this->assertEqual(($count + 1), $this->connection->count()); 131 } 132 133 public function testAdd() 134 { 135 $coll = new Doctrine_Collection($this->objTable); 136 $coll->add(new User()); 137 $this->assertEqual($coll->count(),1); 138 $coll->add(new User()); 139 $this->assertTrue($coll->count(),2); 140 141 $this->assertEqual($coll->getKeys(), array(0,1)); 142 143 $coll[2] = new User(); 144 145 $this->assertTrue($coll->count(),3); 146 $this->assertEqual($coll->getKeys(), array(0,1,2)); 147 } 148 149 public function testLoadRelated() 150 { 151 $coll = $this->connection->query('FROM User u'); 152 153 $q = $coll->loadRelated(); 154 155 $this->assertTrue($q instanceof Doctrine_Query); 156 157 $q->addFrom('User.Group g'); 158 159 $this->assertEqual($q->getSqlQuery($coll->getPrimaryKeys()), '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, e2.id AS e2__id, e2.name AS e2__name, e2.loginname AS e2__loginname, e2.password AS e2__password, e2.type AS e2__type, e2.created AS e2__created, e2.updated AS e2__updated, e2.email_id AS e2__email_id FROM entity e LEFT JOIN groupuser g ON (e.id = g.user_id) LEFT JOIN entity e2 ON e2.id = g.group_id AND e2.type = 1 WHERE (e.id IN (?, ?, ?, ?, ?, ?, ?, ?) AND (e.type = 0))'); 160 161 $coll2 = $q->execute($coll->getPrimaryKeys()); 162 $this->assertEqual($coll2->count(), $coll->count()); 163 164 $count = $this->connection->count(); 165 $coll[0]->Group[0]; 166 $this->assertEqual($count, $this->connection->count()); 167 } 168 169 public function testLoadRelatedForLocalKeyRelation() 170 { 171 $coll = $this->connection->query('FROM User'); 172 173 $this->assertEqual($coll->count(), 8); 174 175 $count = $this->connection->count(); 176 $coll->loadRelated('Email'); 177 178 $this->assertEqual(($count + 1), $this->connection->count()); 179 180 $this->assertEqual($coll[0]->Email->address, 'zYne@example.com'); 181 182 $this->assertEqual(($count + 1), $this->connection->count()); 183 184 $this->assertEqual($coll[2]->Email->address, 'caine@example.com'); 185 186 $this->assertEqual($coll[3]->Email->address, 'kitano@example.com'); 187 188 $this->assertEqual($coll[4]->Email->address, 'stallone@example.com'); 189 190 $this->assertEqual(($count + 1), $this->connection->count()); 191 192 $this->connection->clear(); 193 } 194 195 public function testLoadRelatedForForeignKey() 196 { 197 $coll = $this->connection->query("FROM User"); 198 $this->assertEqual($coll->count(), 8); 199 200 $count = $this->connection->count(); 201 $coll->loadRelated("Phonenumber"); 202 203 $this->assertEqual(($count + 1), $this->connection->count()); 204 205 $this->assertEqual($coll[0]->Phonenumber[0]->phonenumber, "123 123"); 206 207 $this->assertEqual(($count + 1), $this->connection->count()); 208 209 $coll[0]->Phonenumber[1]->phonenumber; 210 211 $this->assertEqual(($count + 1), $this->connection->count()); 212 213 $this->assertEqual($coll[4]->Phonenumber[0]->phonenumber, "111 555 333"); 214 $this->assertEqual($coll[4]["Phonenumber"][1]->phonenumber, "123 213"); 215 $this->assertEqual($coll[4]["Phonenumber"][2]->phonenumber, "444 555"); 216 217 $this->assertEqual($coll[5]->Phonenumber[0]->phonenumber, "111 222 333"); 218 219 220 $this->assertEqual($coll[6]->Phonenumber[0]->phonenumber, "111 222 333"); 221 $this->assertEqual($coll[6]["Phonenumber"][1]->phonenumber, "222 123"); 222 $this->assertEqual($coll[6]["Phonenumber"][2]->phonenumber, "123 456"); 223 224 $this->assertEqual(($count + 1), $this->connection->count()); 225 226 $this->connection->clear(); 227 } 228 229 public function testCount() 230 { 231 $coll = new Doctrine_Collection($this->connection->getTable('User')); 232 $this->assertEqual($coll->count(), 0); 233 $coll[0]; 234 $this->assertEqual($coll->count(), 1); 235 } 236 237 public function testGenerator() 238 { 239 $coll = new Doctrine_Collection($this->objTable); 240 $coll->setKeyColumn('name'); 241 242 $user = new User(); 243 $user->name = "name"; 244 $coll->add($user); 245 246 $this->assertTrue($coll["name"] === $user); 247 248 $this->connection->getTable("email")->setAttribute(Doctrine_Core::ATTR_COLL_KEY,"address"); 249 $emails = $this->connection->getTable("email")->findAll(); 250 foreach($emails as $k => $v) { 251 $this->assertTrue(gettype($k), "string"); 252 } 253 254 } 255 256 public function testFetchCollectionWithIdAsIndex() 257 { 258 $user = new User(); 259 $user->attribute(Doctrine_Core::ATTR_COLL_KEY, 'id'); 260 261 $users = $user->getTable()->findAll(); 262 $this->assertFalse($users->contains(0)); 263 $this->assertEqual($users->count(), 8); 264 } 265 266 public function testFetchCollectionWithNameAsIndex() 267 { 268 $user = new User(); 269 $user->attribute(Doctrine_Core::ATTR_COLL_KEY, 'name'); 270 271 $users = $user->getTable()->findAll(); 272 $this->assertFalse($users->contains(0)); 273 $this->assertEqual($users->count(), 8); 274 } 275 276 public function testFetchMultipleCollections() 277 { 278 $this->connection->clear(); 279 280 $user = new User(); 281 $user->attribute(Doctrine_Core::ATTR_COLL_KEY, 'id'); 282 $phonenumber = new Phonenumber(); 283 $phonenumber->attribute(Doctrine_Core::ATTR_COLL_KEY, 'id'); 284 285 286 $q = new Doctrine_Query(); 287 $users = $q->from('User u, u.Phonenumber p')->execute(); 288 $this->assertFalse($users->contains(0)); 289 $this->assertEqual($users->count(), 8); 290 291 $this->assertEqual($users[4]->name, 'zYne'); 292 293 $this->assertEqual($users[4]->Phonenumber[0]->exists(), false); 294 $this->assertEqual($users[4]->Phonenumber[1]->exists(), false); 295 } 296 297 public function testCustomManagerCollectionClass() 298 { 299 $manager = Doctrine_Manager::getInstance(); 300 $manager->setAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS, 'MyCollection'); 301 302 $user = new User(); 303 $this->assertTrue($user->Phonenumber instanceof MyCollection); 304 305 $manager->setAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS, 'Doctrine_Collection'); 306 } 307 308 public function testCustomConnectionCollectionClass() 309 { 310 $conn = Doctrine_Core::getTable('Phonenumber')->getConnection(); 311 $conn->setAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS, 'MyConnectionCollection'); 312 313 $user = new User(); 314 $this->assertTrue($user->Phonenumber instanceof MyConnectionCollection); 315 316 $conn->unsetAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS); 317 } 318 319 public function testCustomTableCollectionClass() 320 { 321 $userTable = Doctrine_Core::getTable('Phonenumber'); 322 $userTable->setAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS, 'MyPhonenumberCollection'); 323 324 $user = new User(); 325 $this->assertTrue($user->Phonenumber instanceof MyPhonenumberCollection); 326 327 $userTable->unsetAttribute(Doctrine_Core::ATTR_COLLECTION_CLASS); 328 } 329} 330 331class MyCollection extends Doctrine_Collection 332{ 333} 334 335class MyConnectionCollection extends MyCollection 336{ 337} 338 339class MyPhonenumberCollection extends MyConnectionCollection 340{ 341}