PageRenderTime 213ms CodeModel.GetById 80ms app.highlight 98ms RepoModel.GetById 23ms app.codeStats 1ms

/test/testsuite/generator/builder/om/QueryBuilderTest.php

http://github.com/propelorm/Propel
PHP | 1122 lines | 902 code | 189 blank | 31 comment | 3 complexity | 78cf07686b2ec3054077325984f6b31e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3/**
   4 * This file is part of the Propel package.
   5 * For the full copyright and license information, please view the LICENSE
   6 * file that was distributed with this source code.
   7 *
   8 * @license    MIT License
   9 */
  10
  11require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';
  12require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreDataPopulator.php';
  13
  14/**
  15 * Test class for QueryBuilder.
  16 *
  17 * @author     François Zaninotto
  18 * @version    $Id: QueryBuilderTest.php 1347 2009-12-03 21:06:36Z francois $
  19 * @package    generator.builder.om
  20 */
  21class QueryBuilderTest extends BookstoreTestBase
  22{
  23
  24    public function testExtends()
  25    {
  26        $q = new BookQuery();
  27        $this->assertTrue($q instanceof ModelCriteria, 'Model query extends ModelCriteria');
  28    }
  29
  30    public function testConstructor()
  31    {
  32        $query = new BookQuery();
  33        $this->assertEquals($query->getDbName(), 'bookstore', 'Constructor sets database name');
  34        $this->assertEquals($query->getModelName(), 'Book', 'Constructor sets model name');
  35    }
  36
  37    public function testCreate()
  38    {
  39        $query = BookQuery::create();
  40        $this->assertTrue($query instanceof BookQuery, 'create() returns an object of its class');
  41        $this->assertEquals($query->getDbName(), 'bookstore', 'create() sets database name');
  42        $this->assertEquals($query->getModelName(), 'Book', 'create() sets model name');
  43        $query = BookQuery::create('foo');
  44        $this->assertTrue($query instanceof BookQuery, 'create() returns an object of its class');
  45        $this->assertEquals($query->getDbName(), 'bookstore', 'create() sets database name');
  46        $this->assertEquals($query->getModelName(), 'Book', 'create() sets model name');
  47        $this->assertEquals($query->getModelAlias(), 'foo', 'create() can set the model alias');
  48    }
  49
  50    public function testCreateCustom()
  51    {
  52        // see the myBookQuery class definition at the end of this file
  53        $query = myCustomBookQuery::create();
  54        $this->assertTrue($query instanceof myCustomBookQuery, 'create() returns an object of its class');
  55        $this->assertTrue($query instanceof BookQuery, 'create() returns an object of its class');
  56        $this->assertEquals($query->getDbName(), 'bookstore', 'create() sets database name');
  57        $this->assertEquals($query->getModelName(), 'Book', 'create() sets model name');
  58        $query = myCustomBookQuery::create('foo');
  59        $this->assertTrue($query instanceof myCustomBookQuery, 'create() returns an object of its class');
  60        $this->assertEquals($query->getDbName(), 'bookstore', 'create() sets database name');
  61        $this->assertEquals($query->getModelName(), 'Book', 'create() sets model name');
  62        $this->assertEquals($query->getModelAlias(), 'foo', 'create() can set the model alias');
  63    }
  64
  65    public function testBasePreSelect()
  66    {
  67        $method = new ReflectionMethod('Table2Query', 'basePreSelect');
  68        $this->assertEquals('ModelCriteria', $method->getDeclaringClass()->getName(), 'BaseQuery does not override basePreSelect() by default');
  69
  70        $method = new ReflectionMethod('Table3Query', 'basePreSelect');
  71        $this->assertEquals('BaseTable3Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides basePreSelect() when a behavior is registered');
  72    }
  73
  74    public function testBasePreDelete()
  75    {
  76        $method = new ReflectionMethod('Table2Query', 'basePreDelete');
  77        $this->assertEquals('ModelCriteria', $method->getDeclaringClass()->getName(), 'BaseQuery does not override basePreDelete() by default');
  78
  79        $method = new ReflectionMethod('Table3Query', 'basePreDelete');
  80        $this->assertEquals('BaseTable3Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides basePreDelete() when a behavior is registered');
  81    }
  82
  83    public function testBasePostDelete()
  84    {
  85        $method = new ReflectionMethod('Table2Query', 'basePostDelete');
  86        $this->assertEquals('ModelCriteria', $method->getDeclaringClass()->getName(), 'BaseQuery does not override basePostDelete() by default');
  87
  88        $method = new ReflectionMethod('Table3Query', 'basePostDelete');
  89        $this->assertEquals('BaseTable3Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides basePostDelete() when a behavior is registered');
  90    }
  91
  92    public function testBasePreUpdate()
  93    {
  94        $method = new ReflectionMethod('Table2Query', 'basePreUpdate');
  95        $this->assertEquals('ModelCriteria', $method->getDeclaringClass()->getName(), 'BaseQuery does not override basePreUpdate() by default');
  96
  97        $method = new ReflectionMethod('Table3Query', 'basePreUpdate');
  98        $this->assertEquals('BaseTable3Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides basePreUpdate() when a behavior is registered');
  99    }
 100
 101    public function testBasePostUpdate()
 102    {
 103        $method = new ReflectionMethod('Table2Query', 'basePostUpdate');
 104        $this->assertEquals('ModelCriteria', $method->getDeclaringClass()->getName(), 'BaseQuery does not override basePostUpdate() by default');
 105
 106        $method = new ReflectionMethod('Table3Query', 'basePostUpdate');
 107        $this->assertEquals('BaseTable3Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides basePostUpdate() when a behavior is registered');
 108    }
 109
 110    public function testQuery()
 111    {
 112        BookstoreDataPopulator::depopulate();
 113        BookstoreDataPopulator::populate();
 114
 115        $q = new BookQuery();
 116        $book = $q
 117            ->setModelAlias('b')
 118            ->where('b.Title like ?', 'Don%')
 119            ->orderBy('b.ISBN', 'desc')
 120            ->findOne();
 121        $this->assertTrue($book instanceof Book);
 122        $this->assertEquals('Don Juan', $book->getTitle());
 123    }
 124
 125    public function testFindPk()
 126    {
 127        $method = new ReflectionMethod('Table4Query', 'findPk');
 128        $this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findPk()');
 129    }
 130
 131    public function testFindOneById()
 132    {
 133        $method = new ReflectionMethod('Table4Query', 'findOneById');
 134        $this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findOneById()');
 135    }
 136
 137    public function testFindPkReturnsCorrectObjectForSimplePrimaryKey()
 138    {
 139        $b = new Book();
 140        $b->setTitle('bar');
 141        $b->setIsbn('2342');
 142        $b->save($this->con);
 143        $count = $this->con->getQueryCount();
 144
 145        BookPeer::clearInstancePool();
 146
 147        $book = BookQuery::create()->findPk($b->getId(), $this->con);
 148        $this->assertEquals($b, $book);
 149        $this->assertEquals($count+1, $this->con->getQueryCount(), 'findPk() issues a database query when instance is not in pool');
 150    }
 151
 152    public function testFindPkUsesInstancePoolingForSimplePrimaryKey()
 153    {
 154        $b = new Book();
 155        $b->setTitle('foo');
 156        $b->setIsbn('2342');
 157        $b->save($this->con);
 158        $count = $this->con->getQueryCount();
 159
 160        $book = BookQuery::create()->findPk($b->getId(), $this->con);
 161        $this->assertSame($b, $book);
 162        $this->assertEquals($count, $this->con->getQueryCount(), 'findPk() does not issue a database query when instance is in pool');
 163    }
 164
 165    public function testFindPkReturnsCorrectObjectForCompositePrimaryKey()
 166    {
 167        BookstoreDataPopulator::depopulate();
 168        BookstoreDataPopulator::populate();
 169
 170        // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved
 171        $c = new ModelCriteria('bookstore', 'Book');
 172        $books = $c->find();
 173        foreach ($books as $book) {
 174            $book->save();
 175        }
 176
 177        BookPeer::clearInstancePool();
 178
 179        // retrieve the test data
 180        $c = new ModelCriteria('bookstore', 'BookListRel');
 181        $bookListRelTest = $c->findOne();
 182        $pk = $bookListRelTest->getPrimaryKey();
 183
 184        $q = new BookListRelQuery();
 185        $bookListRel = $q->findPk($pk);
 186        $this->assertEquals($bookListRelTest, $bookListRel, 'BaseQuery overrides findPk() for composite primary keysto make it faster');
 187    }
 188
 189    public function testFindPkUsesFindPkSimpleOnEmptyQueries()
 190    {
 191        BookQuery::create()->findPk(123, $this->con);
 192        $expected = 'SELECT id, title, isbn, price, publisher_id, author_id FROM book WHERE id = 123';
 193        $this->assertEquals($expected, $this->con->getLastExecutedQuery());
 194    }
 195
 196    public function testFindPkSimpleAddsObjectToInstancePool()
 197    {
 198        $b = new Book();
 199        $b->setTitle('foo');
 200        $b->setIsbn('2342');
 201        $b->save($this->con);
 202        BookPeer::clearInstancePool();
 203
 204        BookQuery::create()->findPk($b->getId(), $this->con);
 205        $count = $this->con->getQueryCount();
 206
 207        $book = BookQuery::create()->findPk($b->getId(), $this->con);
 208        $this->assertEquals($b, $book);
 209        $this->assertEquals($count, $this->con->getQueryCount());
 210    }
 211
 212    public function testFindOneByIdAddsObjectToInstancePool()
 213    {
 214        $b = new Book();
 215        $b->setTitle('foo');
 216        $b->setIsbn('2342');
 217        $b->save($this->con);
 218        BookPeer::clearInstancePool();
 219
 220        BookQuery::create()->findOneById($b->getId(), $this->con);
 221        $count = $this->con->getQueryCount();
 222
 223        $book = BookQuery::create()->findOneById($b->getId(), $this->con);
 224        $this->assertEquals($b, $book);
 225        $this->assertEquals($count, $this->con->getQueryCount());
 226    }
 227
 228    public function testFindPkUsesFindPkComplexOnNonEmptyQueries()
 229    {
 230        BookQuery::create('b')->findPk(123, $this->con);
 231        $expected = 'SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM `book` WHERE book.id=123';
 232        $this->assertEquals($expected, $this->con->getLastExecutedQuery());
 233    }
 234
 235    public function testFindPkComplexAddsObjectToInstancePool()
 236    {
 237        $b = new Book();
 238        $b->setTitle('foo');
 239        $b->setIsbn('2345');
 240        $b->save($this->con);
 241        BookPeer::clearInstancePool();
 242
 243        BookQuery::create('b')->findPk($b->getId(), $this->con);
 244        $count = $this->con->getQueryCount();
 245
 246        $book = BookQuery::create()->findPk($b->getId(), $this->con);
 247        $this->assertEquals($b, $book);
 248        $this->assertEquals($count, $this->con->getQueryCount());
 249    }
 250
 251    public function testFindPkCallsPreSelect()
 252    {
 253        $q = new mySecondBookQuery();
 254        $this->assertFalse($q::$preSelectWasCalled);
 255        $q->findPk(123);
 256        $this->assertTrue($q::$preSelectWasCalled);
 257    }
 258
 259    public function testFindPkDoesNotCallPreSelectWhenUsingInstancePool()
 260    {
 261        $b = new Book();
 262        $b->setTitle('foo');
 263        $b->setIsbn('1245');
 264        $b->save();
 265
 266        $q = new mySecondBookQuery();
 267        $this->assertFalse($q::$preSelectWasCalled);
 268        $q->findPk($b->getId());
 269        $this->assertFalse($q::$preSelectWasCalled);
 270    }
 271
 272    public function testFindPks()
 273    {
 274        $method = new ReflectionMethod('Table4Query', 'findPks');
 275        $this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findPks()');
 276    }
 277
 278    public function testFindPksSimpleKey()
 279    {
 280        BookstoreDataPopulator::depopulate();
 281        BookstoreDataPopulator::populate();
 282
 283        BookPeer::clearInstancePool();
 284
 285        // prepare the test data
 286        $c = new ModelCriteria('bookstore', 'Book');
 287        $c->orderBy('Book.Id', 'desc');
 288        $testBooks = $c->find();
 289        $testBook1 = $testBooks->pop();
 290        $testBook2 = $testBooks->pop();
 291
 292        $q = new BookQuery();
 293        $books = $q->findPks(array($testBook1->getId(), $testBook2->getId()));
 294        $this->assertEquals(array($testBook1, $testBook2), $books->getData(), 'BaseQuery overrides findPks() to make it faster');
 295    }
 296
 297    public function testFindPksCompositeKey()
 298    {
 299        BookstoreDataPopulator::depopulate();
 300        BookstoreDataPopulator::populate();
 301
 302        // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved
 303        $c = new ModelCriteria('bookstore', 'Book');
 304        $books = $c->find();
 305        foreach ($books as $book) {
 306            $book->save();
 307        }
 308
 309        BookPeer::clearInstancePool();
 310
 311        // retrieve the test data
 312        $c = new ModelCriteria('bookstore', 'BookListRel');
 313        $bookListRelTest = $c->find();
 314        $search = array();
 315        foreach ($bookListRelTest as $obj) {
 316            $search[]= $obj->getPrimaryKey();
 317        }
 318
 319        $q = new BookListRelQuery();
 320        $objs = $q->findPks($search);
 321        $this->assertEquals($bookListRelTest, $objs, 'BaseQuery overrides findPks() for composite primary keys to make it work');
 322    }
 323
 324    public function testFilterBy()
 325    {
 326        foreach (BookPeer::getFieldNames(BasePeer::TYPE_PHPNAME) as $colName) {
 327            $filterMethod = 'filterBy' . $colName;
 328            $this->assertTrue(method_exists('BookQuery', $filterMethod), 'QueryBuilder adds filterByColumn() methods for every column');
 329            $q = BookQuery::create()->$filterMethod(1);
 330            $this->assertTrue($q instanceof BookQuery, 'filterByColumn() returns the current query instance');
 331        }
 332    }
 333
 334    public function testFilterByPrimaryKeySimpleKey()
 335    {
 336        $q = BookQuery::create()->filterByPrimaryKey(12);
 337        $q1 = BookQuery::create()->add(BookPeer::ID, 12, Criteria::EQUAL);
 338        $this->assertEquals($q1, $q, 'filterByPrimaryKey() translates to a Criteria::EQUAL in the PK column');
 339
 340        $q = BookQuery::create()->setModelAlias('b', true)->filterByPrimaryKey(12);
 341        $q1 = BookQuery::create()->setModelAlias('b', true)->add('b.id', 12, Criteria::EQUAL);
 342        $this->assertEquals($q1, $q, 'filterByPrimaryKey() uses true table alias if set');
 343    }
 344
 345    public function testFilterByPrimaryKeyCompositeKey()
 346    {
 347        BookstoreDataPopulator::depopulate();
 348        BookstoreDataPopulator::populate();
 349
 350        // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved
 351        $c = new ModelCriteria('bookstore', 'Book');
 352        $books = $c->find();
 353        foreach ($books as $book) {
 354            $book->save();
 355        }
 356
 357        BookPeer::clearInstancePool();
 358
 359        // retrieve the test data
 360        $c = new ModelCriteria('bookstore', 'BookListRel');
 361        $bookListRelTest = $c->findOne();
 362        $pk = $bookListRelTest->getPrimaryKey();
 363
 364        $q = new BookListRelQuery();
 365        $q->filterByPrimaryKey($pk);
 366
 367        $q1 = BookListRelQuery::create()
 368            ->add(BookListRelPeer::BOOK_ID, $pk[0], Criteria::EQUAL)
 369            ->add(BookListRelPeer::BOOK_CLUB_LIST_ID, $pk[1], Criteria::EQUAL);
 370        $this->assertEquals($q1, $q, 'filterByPrimaryKey() translates to a Criteria::EQUAL in the PK columns');
 371    }
 372
 373    public function testFilterByPrimaryKeysSimpleKey()
 374    {
 375        $q = BookQuery::create()->filterByPrimaryKeys(array(10, 11, 12));
 376        $q1 = BookQuery::create()->add(BookPeer::ID, array(10, 11, 12), Criteria::IN);
 377        $this->assertEquals($q1, $q, 'filterByPrimaryKeys() translates to a Criteria::IN on the PK column');
 378
 379        $q = BookQuery::create()->setModelAlias('b', true)->filterByPrimaryKeys(array(10, 11, 12));
 380        $q1 = BookQuery::create()->setModelAlias('b', true)->add('b.id', array(10, 11, 12), Criteria::IN);
 381        $this->assertEquals($q1, $q, 'filterByPrimaryKeys() uses true table alias if set');
 382    }
 383
 384    public function testFilterByPrimaryKeysCompositeKey()
 385    {
 386        BookstoreDataPopulator::depopulate();
 387        BookstoreDataPopulator::populate();
 388
 389        // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved
 390        $c = new ModelCriteria('bookstore', 'Book');
 391        $books = $c->find();
 392        foreach ($books as $book) {
 393            $book->save();
 394        }
 395
 396        BookPeer::clearInstancePool();
 397
 398        // retrieve the test data
 399        $c = new ModelCriteria('bookstore', 'BookListRel');
 400        $bookListRelTest = $c->find();
 401        $search = array();
 402        foreach ($bookListRelTest as $obj) {
 403            $search[]= $obj->getPrimaryKey();
 404        }
 405
 406        $q = new BookListRelQuery();
 407        $q->filterByPrimaryKeys($search);
 408
 409        $q1 = BookListRelQuery::create();
 410        foreach ($search as $key) {
 411            $cton0 = $q1->getNewCriterion(BookListRelPeer::BOOK_ID, $key[0], Criteria::EQUAL);
 412            $cton1 = $q1->getNewCriterion(BookListRelPeer::BOOK_CLUB_LIST_ID, $key[1], Criteria::EQUAL);
 413            $cton0->addAnd($cton1);
 414            $q1->addOr($cton0);
 415        }
 416        $this->assertEquals($q1, $q, 'filterByPrimaryKeys() translates to a series of Criteria::EQUAL in the PK columns');
 417
 418        $q = new BookListRelQuery();
 419        $q->filterByPrimaryKeys(array());
 420
 421        $q1 = BookListRelQuery::create();
 422        $q1->add(null, '1<>1', Criteria::CUSTOM);
 423        $this->assertEquals($q1, $q, 'filterByPrimaryKeys() translates to an always failing test on empty arrays');
 424
 425    }
 426
 427    public function testFilterByIntegerPk()
 428    {
 429        $q = BookQuery::create()->filterById(12);
 430        $q1 = BookQuery::create()->add(BookPeer::ID, 12, Criteria::EQUAL);
 431        $this->assertEquals($q1, $q, 'filterByPkColumn() translates to a Criteria::EQUAL by default');
 432
 433        $q = BookQuery::create()->filterById(12, Criteria::NOT_EQUAL);
 434        $q1 = BookQuery::create()->add(BookPeer::ID, 12, Criteria::NOT_EQUAL);
 435        $this->assertEquals($q1, $q, 'filterByPkColumn() accepts an optional comparison operator');
 436
 437        $q = BookQuery::create()->setModelAlias('b', true)->filterById(12);
 438        $q1 = BookQuery::create()->setModelAlias('b', true)->add('b.id', 12, Criteria::EQUAL);
 439        $this->assertEquals($q1, $q, 'filterByPkColumn() uses true table alias if set');
 440
 441        $q = BookQuery::create()->filterById(array(10, 11, 12));
 442        $q1 = BookQuery::create()->add(BookPeer::ID, array(10, 11, 12), Criteria::IN);
 443        $this->assertEquals($q1, $q, 'filterByPkColumn() translates to a Criteria::IN when passed a simple array key');
 444
 445        $q = BookQuery::create()->filterById(array(10, 11, 12), Criteria::NOT_IN);
 446        $q1 = BookQuery::create()->add(BookPeer::ID, array(10, 11, 12), Criteria::NOT_IN);
 447        $this->assertEquals($q1, $q, 'filterByPkColumn() accepts a comparison when passed a simple array key');
 448    }
 449
 450    public function testFilterByNumber()
 451    {
 452        $q = BookQuery::create()->filterByPrice(12);
 453        $q1 = BookQuery::create()->add(BookPeer::PRICE, 12, Criteria::EQUAL);
 454        $this->assertEquals($q1, $q, 'filterByNumColumn() translates to a Criteria::EQUAL by default');
 455
 456        $q = BookQuery::create()->filterByPrice(12, Criteria::NOT_EQUAL);
 457        $q1 = BookQuery::create()->add(BookPeer::PRICE, 12, Criteria::NOT_EQUAL);
 458        $this->assertEquals($q1, $q, 'filterByNumColumn() accepts an optional comparison operator');
 459
 460        $q = BookQuery::create()->setModelAlias('b', true)->filterByPrice(12);
 461        $q1 = BookQuery::create()->setModelAlias('b', true)->add('b.price', 12, Criteria::EQUAL);
 462        $this->assertEquals($q1, $q, 'filterByNumColumn() uses true table alias if set');
 463
 464        $q = BookQuery::create()->filterByPrice(array(10, 11, 12));
 465        $q1 = BookQuery::create()->add(BookPeer::PRICE, array(10, 11, 12), Criteria::IN);
 466        $this->assertEquals($q1, $q, 'filterByNumColumn() translates to a Criteria::IN when passed a simple array key');
 467
 468        $q = BookQuery::create()->filterByPrice(array(10, 11, 12), Criteria::NOT_IN);
 469        $q1 = BookQuery::create()->add(BookPeer::PRICE, array(10, 11, 12), Criteria::NOT_IN);
 470        $this->assertEquals($q1, $q, 'filterByNumColumn() accepts a comparison when passed a simple array key');
 471
 472        $q = BookQuery::create()->filterByPrice(array('min' => 10));
 473        $q1 = BookQuery::create()->add(BookPeer::PRICE, 10, Criteria::GREATER_EQUAL);
 474        $this->assertEquals($q1, $q, 'filterByNumColumn() translates to a Criteria::GREATER_EQUAL when passed a \'min\' key');
 475
 476        $q = BookQuery::create()->filterByPrice(array('max' => 12));
 477        $q1 = BookQuery::create()->add(BookPeer::PRICE, 12, Criteria::LESS_EQUAL);
 478        $this->assertEquals($q1, $q, 'filterByNumColumn() translates to a Criteria::LESS_EQUAL when passed a \'max\' key');
 479
 480        $q = BookQuery::create()->filterByPrice(array('min' => 10, 'max' => 12));
 481        $q1 = BookQuery::create()
 482            ->add(BookPeer::PRICE, 10, Criteria::GREATER_EQUAL)
 483            ->addAnd(BookPeer::PRICE, 12, Criteria::LESS_EQUAL);
 484        $this->assertEquals($q1, $q, 'filterByNumColumn() translates to a between when passed both a \'min\' and a \'max\' key');
 485    }
 486
 487    public function testFilterByTimestamp()
 488    {
 489        $q = BookstoreEmployeeAccountQuery::create()->filterByCreated(12);
 490        $q1 = BookstoreEmployeeAccountQuery::create()->add(BookstoreEmployeeAccountPeer::CREATED, 12, Criteria::EQUAL);
 491        $this->assertEquals($q1, $q, 'filterByDateColumn() translates to a Criteria::EQUAL by default');
 492
 493        $q = BookstoreEmployeeAccountQuery::create()->filterByCreated(12, Criteria::NOT_EQUAL);
 494        $q1 = BookstoreEmployeeAccountQuery::create()->add(BookstoreEmployeeAccountPeer::CREATED, 12, Criteria::NOT_EQUAL);
 495        $this->assertEquals($q1, $q, 'filterByDateColumn() accepts an optional comparison operator');
 496
 497        $q = BookstoreEmployeeAccountQuery::create()->setModelAlias('b', true)->filterByCreated(12);
 498        $q1 = BookstoreEmployeeAccountQuery::create()->setModelAlias('b', true)->add('b.created', 12, Criteria::EQUAL);
 499        $this->assertEquals($q1, $q, 'filterByDateColumn() uses true table alias if set');
 500
 501        $q = BookstoreEmployeeAccountQuery::create()->filterByCreated(array('min' => 10));
 502        $q1 = BookstoreEmployeeAccountQuery::create()->add(BookstoreEmployeeAccountPeer::CREATED, 10, Criteria::GREATER_EQUAL);
 503        $this->assertEquals($q1, $q, 'filterByDateColumn() translates to a Criteria::GREATER_EQUAL when passed a \'min\' key');
 504
 505        $q = BookstoreEmployeeAccountQuery::create()->filterByCreated(array('max' => 12));
 506        $q1 = BookstoreEmployeeAccountQuery::create()->add(BookstoreEmployeeAccountPeer::CREATED, 12, Criteria::LESS_EQUAL);
 507        $this->assertEquals($q1, $q, 'filterByDateColumn() translates to a Criteria::LESS_EQUAL when passed a \'max\' key');
 508
 509        $q = BookstoreEmployeeAccountQuery::create()->filterByCreated(array('min' => 10, 'max' => 12));
 510        $q1 = BookstoreEmployeeAccountQuery::create()
 511            ->add(BookstoreEmployeeAccountPeer::CREATED, 10, Criteria::GREATER_EQUAL)
 512            ->addAnd(BookstoreEmployeeAccountPeer::CREATED, 12, Criteria::LESS_EQUAL);
 513        $this->assertEquals($q1, $q, 'filterByDateColumn() translates to a between when passed both a \'min\' and a \'max\' key');
 514    }
 515
 516    public function testFilterByString()
 517    {
 518        $q = BookQuery::create()->filterByTitle('foo');
 519        $q1 = BookQuery::create()->add(BookPeer::TITLE, 'foo', Criteria::EQUAL);
 520        $this->assertEquals($q1, $q, 'filterByStringColumn() translates to a Criteria::EQUAL by default');
 521
 522        $q = BookQuery::create()->filterByTitle('foo', Criteria::NOT_EQUAL);
 523        $q1 = BookQuery::create()->add(BookPeer::TITLE, 'foo', Criteria::NOT_EQUAL);
 524        $this->assertEquals($q1, $q, 'filterByStringColumn() accepts an optional comparison operator');
 525
 526        $q = BookQuery::create()->setModelAlias('b', true)->filterByTitle('foo');
 527        $q1 = BookQuery::create()->setModelAlias('b', true)->add('b.title', 'foo', Criteria::EQUAL);
 528        $this->assertEquals($q1, $q, 'filterByStringColumn() uses true table alias if set');
 529
 530        $q = BookQuery::create()->filterByTitle(array('foo', 'bar'));
 531        $q1 = BookQuery::create()->add(BookPeer::TITLE, array('foo', 'bar'), Criteria::IN);
 532        $this->assertEquals($q1, $q, 'filterByStringColumn() translates to a Criteria::IN when passed an array');
 533
 534        $q = BookQuery::create()->filterByTitle(array('foo', 'bar'), Criteria::NOT_IN);
 535        $q1 = BookQuery::create()->add(BookPeer::TITLE, array('foo', 'bar'), Criteria::NOT_IN);
 536        $this->assertEquals($q1, $q, 'filterByStringColumn() accepts a comparison when passed an array');
 537
 538        $q = BookQuery::create()->filterByTitle('foo%');
 539        $q1 = BookQuery::create()->add(BookPeer::TITLE, 'foo%', Criteria::LIKE);
 540        $this->assertEquals($q1, $q, 'filterByStringColumn() translates to a Criteria::LIKE when passed a string with a % wildcard');
 541
 542        $q = BookQuery::create()->filterByTitle('foo%', Criteria::NOT_LIKE);
 543        $q1 = BookQuery::create()->add(BookPeer::TITLE, 'foo%', Criteria::NOT_LIKE);
 544        $this->assertEquals($q1, $q, 'filterByStringColumn() accepts a comparison when passed a string with a % wildcard');
 545
 546        $q = BookQuery::create()->filterByTitle('foo%', Criteria::EQUAL);
 547        $q1 = BookQuery::create()->add(BookPeer::TITLE, 'foo%', Criteria::EQUAL);
 548        $this->assertEquals($q1, $q, 'filterByStringColumn() accepts a comparison when passed a string with a % wildcard');
 549
 550        $q = BookQuery::create()->filterByTitle('*foo');
 551        $q1 = BookQuery::create()->add(BookPeer::TITLE, '%foo', Criteria::LIKE);
 552        $this->assertEquals($q1, $q, 'filterByStringColumn() translates to a Criteria::LIKE when passed a string with a * wildcard, and turns * into %');
 553
 554        $q = BookQuery::create()->filterByTitle('*f%o*o%');
 555        $q1 = BookQuery::create()->add(BookPeer::TITLE, '%f%o%o%', Criteria::LIKE);
 556        $this->assertEquals($q1, $q, 'filterByStringColumn() translates to a Criteria::LIKE when passed a string with mixed wildcards, and turns *s into %s');
 557    }
 558
 559    public function testFilterByBoolean()
 560    {
 561        $q = ReviewQuery::create()->filterByRecommended(true);
 562        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, true, Criteria::EQUAL);
 563        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a Criteria::EQUAL by default');
 564
 565        $q = ReviewQuery::create()->filterByRecommended(true, Criteria::NOT_EQUAL);
 566        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, true, Criteria::NOT_EQUAL);
 567        $this->assertEquals($q1, $q, 'filterByBooleanColumn() accepts an optional comparison operator');
 568
 569        $q = ReviewQuery::create()->filterByRecommended(false);
 570        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, false, Criteria::EQUAL);
 571        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a Criteria::EQUAL by default');
 572
 573        $q = ReviewQuery::create()->setModelAlias('b', true)->filterByRecommended(true);
 574        $q1 = ReviewQuery::create()->setModelAlias('b', true)->add('b.recommended', true, Criteria::EQUAL);
 575        $this->assertEquals($q1, $q, 'filterByBooleanColumn() uses true table alias if set');
 576
 577        $q = ReviewQuery::create()->filterByRecommended('true');
 578        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, true, Criteria::EQUAL);
 579        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = true when passed a true string');
 580
 581        $q = ReviewQuery::create()->filterByRecommended('yes');
 582        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, true, Criteria::EQUAL);
 583        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = true when passed a true string');
 584
 585        $q = ReviewQuery::create()->filterByRecommended('1');
 586        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, true, Criteria::EQUAL);
 587        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = true when passed a true string');
 588
 589        $q = ReviewQuery::create()->filterByRecommended('false');
 590        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, false, Criteria::EQUAL);
 591        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = false when passed a false string');
 592
 593        $q = ReviewQuery::create()->filterByRecommended('no');
 594        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, false, Criteria::EQUAL);
 595        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = false when passed a false string');
 596
 597        $q = ReviewQuery::create()->filterByRecommended('0');
 598        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, false, Criteria::EQUAL);
 599        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = false when passed a false string');
 600
 601        $q = ReviewQuery::create()->filterByRecommended('');
 602        $q1 = ReviewQuery::create()->add(ReviewPeer::RECOMMENDED, false, Criteria::EQUAL);
 603        $this->assertEquals($q1, $q, 'filterByBooleanColumn() translates to a = false when passed an empty string');
 604
 605    }
 606
 607    public function testFilterByFk()
 608    {
 609        $this->assertTrue(method_exists('BookQuery', 'filterByAuthor'), 'QueryBuilder adds filterByFk() methods');
 610        $this->assertTrue(method_exists('BookQuery', 'filterByPublisher'), 'QueryBuilder adds filterByFk() methods for all fkeys');
 611
 612        $this->assertTrue(method_exists('EssayQuery', 'filterByAuthorRelatedByFirstAuthor'), 'QueryBuilder adds filterByFk() methods for several fkeys on the same table');
 613        $this->assertTrue(method_exists('EssayQuery', 'filterByAuthorRelatedBySecondAuthor'), 'QueryBuilder adds filterByFk() methods for several fkeys on the same table');
 614    }
 615
 616    public function testFilterByFkSimpleKey()
 617    {
 618        BookstoreDataPopulator::depopulate();
 619        BookstoreDataPopulator::populate();
 620
 621        // prepare the test data
 622        $testBook = BookQuery::create()
 623            ->innerJoin('Book.Author') // just in case there are books with no author
 624            ->findOne();
 625        $testAuthor = $testBook->getAuthor();
 626
 627        $book = BookQuery::create()
 628            ->filterByAuthor($testAuthor)
 629            ->findOne();
 630        $this->assertEquals($testBook, $book, 'Generated query handles filterByFk() methods correctly for simple fkeys');
 631
 632        $q = BookQuery::create()->filterByAuthor($testAuthor);
 633        $q1 = BookQuery::create()->add(BookPeer::AUTHOR_ID, $testAuthor->getId(), Criteria::EQUAL);
 634        $this->assertEquals($q1, $q, 'filterByFk() translates to a Criteria::EQUAL by default');
 635
 636        $q = BookQuery::create()->filterByAuthor($testAuthor, Criteria::NOT_EQUAL);
 637        $q1 = BookQuery::create()->add(BookPeer::AUTHOR_ID, $testAuthor->getId(), Criteria::NOT_EQUAL);
 638        $this->assertEquals($q1, $q, 'filterByFk() accepts an optional comparison operator');
 639    }
 640
 641    public function testFilterByFkCompositeKey()
 642    {
 643        BookstoreDataPopulator::depopulate();
 644        BookstoreDataPopulator::populate();
 645        BookstoreDataPopulator::populateOpinionFavorite();
 646
 647        // prepare the test data
 648        $testOpinion = BookOpinionQuery::create()
 649            ->innerJoin('BookOpinion.ReaderFavorite') // just in case there are books with no author
 650            ->findOne();
 651        $testFavorite = $testOpinion->getReaderFavorite();
 652
 653        $favorite = ReaderFavoriteQuery::create()
 654            ->filterByBookOpinion($testOpinion)
 655            ->findOne();
 656        $this->assertEquals($testFavorite, $favorite, 'Generated query handles filterByFk() methods correctly for composite fkeys');
 657    }
 658
 659    public function testFilterByFkObjectCollection()
 660    {
 661        BookstoreDataPopulator::depopulate($this->con);
 662        BookstoreDataPopulator::populate($this->con);
 663
 664        $authors = AuthorQuery::create()
 665            ->orderByFirstName()
 666            ->limit(2)
 667            ->find($this->con);
 668
 669        $books = BookQuery::create()
 670            ->filterByAuthor($authors)
 671            ->find($this->con);
 672        $q1 = $this->con->getLastExecutedQuery();
 673
 674        $books = BookQuery::create()
 675            ->add(BookPeer::AUTHOR_ID, $authors->getPrimaryKeys(), Criteria::IN)
 676            ->find($this->con);
 677        $q2 = $this->con->getLastExecutedQuery();
 678
 679        $this->assertEquals($q2, $q1, 'filterByFk() accepts a collection and results to an IN query');
 680    }
 681
 682    public function testFilterByRefFk()
 683    {
 684        $this->assertTrue(method_exists('BookQuery', 'filterByReview'), 'QueryBuilder adds filterByRefFk() methods');
 685        $this->assertTrue(method_exists('BookQuery', 'filterByMedia'), 'QueryBuilder adds filterByRefFk() methods for all fkeys');
 686
 687        $this->assertTrue(method_exists('AuthorQuery', 'filterByEssayRelatedByFirstAuthor'), 'QueryBuilder adds filterByRefFk() methods for several fkeys on the same table');
 688        $this->assertTrue(method_exists('AuthorQuery', 'filterByEssayRelatedBySecondAuthor'), 'QueryBuilder adds filterByRefFk() methods for several fkeys on the same table');
 689    }
 690
 691    public function testFilterByRefFkSimpleKey()
 692    {
 693        BookstoreDataPopulator::depopulate();
 694        BookstoreDataPopulator::populate();
 695
 696        // prepare the test data
 697        $testBook = BookQuery::create()
 698            ->innerJoin('Book.Author') // just in case there are books with no author
 699            ->findOne();
 700        $testAuthor = $testBook->getAuthor();
 701
 702        $author = AuthorQuery::create()
 703            ->filterByBook($testBook)
 704            ->findOne();
 705        $this->assertEquals($testAuthor, $author, 'Generated query handles filterByRefFk() methods correctly for simple fkeys');
 706
 707        $q = AuthorQuery::create()->filterByBook($testBook);
 708        $q1 = AuthorQuery::create()->add(AuthorPeer::ID, $testBook->getAuthorId(), Criteria::EQUAL);
 709        $this->assertEquals($q1, $q, 'filterByRefFk() translates to a Criteria::EQUAL by default');
 710
 711        $q = AuthorQuery::create()->filterByBook($testBook, Criteria::NOT_EQUAL);
 712        $q1 = AuthorQuery::create()->add(AuthorPeer::ID, $testBook->getAuthorId(), Criteria::NOT_EQUAL);
 713        $this->assertEquals($q1, $q, 'filterByRefFk() accepts an optional comparison operator');
 714    }
 715
 716    public function testFilterByRelationNameCompositePk()
 717    {
 718        BookstoreDataPopulator::depopulate();
 719        BookstoreDataPopulator::populate();
 720
 721        $testLabel = RecordLabelQuery::create()
 722            ->limit(2)
 723            ->find($this->con);
 724
 725        $testRelease = ReleasePoolQuery::create()
 726            ->addJoin(ReleasePoolPeer::RECORD_LABEL_ID, RecordLabelPeer::ID)
 727            ->filterByRecordLabel($testLabel)
 728            ->find($this->con);
 729        $q1 = $this->con->getLastExecutedQuery();
 730
 731        $releasePool = ReleasePoolQuery::create()
 732            ->addJoin(ReleasePoolPeer::RECORD_LABEL_ID, RecordLabelPeer::ID)
 733            ->add(ReleasePoolPeer::RECORD_LABEL_ID, $testLabel->toKeyValue('Id', 'Id'), Criteria::IN)
 734            ->find($this->con);
 735        $q2 = $this->con->getLastExecutedQuery();
 736
 737        $this->assertEquals($q2, $q1, 'filterBy{RelationName}() only accepts arguments of type {RelationName} or PropelCollection');
 738        $this->assertEquals($releasePool, $testRelease);
 739    }
 740
 741    public function testFilterByRefFkCompositeKey()
 742    {
 743        BookstoreDataPopulator::depopulate();
 744        BookstoreDataPopulator::populate();
 745        BookstoreDataPopulator::populateOpinionFavorite();
 746
 747        // prepare the test data
 748        $testOpinion = BookOpinionQuery::create()
 749            ->innerJoin('BookOpinion.ReaderFavorite') // just in case there are books with no author
 750            ->findOne();
 751        $testFavorite = $testOpinion->getReaderFavorite();
 752
 753        $opinion = BookOpinionQuery::create()
 754            ->filterByReaderFavorite($testFavorite)
 755            ->findOne();
 756        $this->assertEquals($testOpinion, $opinion, 'Generated query handles filterByRefFk() methods correctly for composite fkeys');
 757    }
 758
 759    public function testFilterByRefFkObjectCollection()
 760    {
 761        BookstoreDataPopulator::depopulate($this->con);
 762        BookstoreDataPopulator::populate($this->con);
 763
 764        $books = BookQuery::create()
 765            ->orderByTitle()
 766            ->limit(2)
 767            ->find($this->con);
 768
 769        $authors = AuthorQuery::create()
 770            ->filterByBook($books)
 771            ->find($this->con);
 772        $q1 = $this->con->getLastExecutedQuery();
 773
 774        $authors = AuthorQuery::create()
 775            ->addJoin(AuthorPeer::ID, BookPeer::AUTHOR_ID, Criteria::LEFT_JOIN)
 776            ->add(BookPeer::ID, $books->getPrimaryKeys(), Criteria::IN)
 777            ->find($this->con);
 778        $q2 = $this->con->getLastExecutedQuery();
 779
 780        $this->assertEquals($q2, $q1, 'filterByRefFk() accepts a collection and results to an IN query in the joined table');
 781    }
 782
 783    public function testFilterByCrossFK()
 784    {
 785        $this->assertTrue(method_exists('BookQuery', 'filterByBookClubList'), 'Generated query handles filterByCrossRefFK() for many-to-many relationships');
 786        $this->assertFalse(method_exists('BookQuery', 'filterByBook'), 'Generated query handles filterByCrossRefFK() for many-to-many relationships');
 787        BookstoreDataPopulator::depopulate();
 788        BookstoreDataPopulator::populate();
 789        $blc1 = BookClubListQuery::create()->findOneByGroupLeader('Crazyleggs');
 790        $nbBooks = BookQuery::create()
 791            ->filterByBookClubList($blc1)
 792            ->count();
 793        $this->assertEquals(2, $nbBooks, 'Generated query handles filterByCrossRefFK() methods correctly');
 794    }
 795
 796    public function testJoinFk()
 797    {
 798        $q = BookQuery::create()
 799            ->joinAuthor();
 800        $q1 = BookQuery::create()
 801            ->join('Book.Author', Criteria::LEFT_JOIN);
 802        $this->assertTrue($q->equals($q1), 'joinFk() translates to a left join on non-required columns');
 803
 804        $q = BookSummaryQuery::create()
 805            ->joinSummarizedBook();
 806        $q1 = BookSummaryQuery::create()
 807            ->join('BookSummary.SummarizedBook', Criteria::INNER_JOIN);
 808        $this->assertTrue($q->equals($q1), 'joinFk() translates to an inner join on required columns');
 809
 810        $q = BookQuery::create()
 811            ->joinAuthor('a');
 812        $q1 = BookQuery::create()
 813            ->join('Book.Author a', Criteria::LEFT_JOIN);
 814        $this->assertTrue($q->equals($q1), 'joinFk() accepts a relation alias as first parameter');
 815
 816        $q = BookQuery::create()
 817            ->joinAuthor('', Criteria::INNER_JOIN);
 818        $q1 = BookQuery::create()
 819            ->join('Book.Author', Criteria::INNER_JOIN);
 820        $this->assertTrue($q->equals($q1), 'joinFk() accepts a join type as second parameter');
 821
 822        $q = EssayQuery::create()
 823            ->joinAuthorRelatedBySecondAuthor();
 824        $q1 = EssayQuery::create()
 825            ->join('Essay.AuthorRelatedBySecondAuthor', "INNER JOIN");
 826        $this->assertTrue($q->equals($q1), 'joinFk() translates to a "INNER JOIN" when this is defined as defaultJoin in the schema');
 827    }
 828
 829    public function testJoinFkAlias()
 830    {
 831        $q = BookQuery::create('b')
 832            ->joinAuthor('a');
 833        $q1 = BookQuery::create('b')
 834            ->join('b.Author a', Criteria::LEFT_JOIN);
 835        $this->assertTrue($q->equals($q1), 'joinFk() works fine with table aliases');
 836
 837        $q = BookQuery::create()
 838            ->setModelAlias('b', true)
 839            ->joinAuthor('a');
 840        $q1 = BookQuery::create()
 841            ->setModelAlias('b', true)
 842            ->join('b.Author a', Criteria::LEFT_JOIN);
 843        $this->assertTrue($q->equals($q1), 'joinFk() works fine with true table aliases');
 844    }
 845
 846    public function testJoinRefFk()
 847    {
 848        $q = AuthorQuery::create()
 849            ->joinBook();
 850        $q1 = AuthorQuery::create()
 851            ->join('Author.Book', Criteria::LEFT_JOIN);
 852        $this->assertTrue($q->equals($q1), 'joinRefFk() translates to a left join on non-required columns');
 853
 854        $q = BookQuery::create()
 855            ->joinBookSummary();
 856        $q1 = BookQuery::create()
 857            ->join('Book.BookSummary', Criteria::INNER_JOIN);
 858        $this->assertTrue($q->equals($q1), 'joinRefFk() translates to an inner join on required columns');
 859
 860        $q = AuthorQuery::create()
 861            ->joinBook('b');
 862        $q1 = AuthorQuery::create()
 863            ->join('Author.Book b', Criteria::LEFT_JOIN);
 864        $this->assertTrue($q->equals($q1), 'joinRefFk() accepts a relation alias as first parameter');
 865
 866        $q = AuthorQuery::create()
 867            ->joinBook('', Criteria::INNER_JOIN);
 868        $q1 = AuthorQuery::create()
 869            ->join('Author.Book', Criteria::INNER_JOIN);
 870        $this->assertTrue($q->equals($q1), 'joinRefFk() accepts a join type as second parameter');
 871
 872        $q = AuthorQuery::create()
 873            ->joinEssayRelatedBySecondAuthor();
 874        $q1 = AuthorQuery::create()
 875            ->join('Author.EssayRelatedBySecondAuthor', Criteria::INNER_JOIN);
 876        $this->assertTrue($q->equals($q1), 'joinRefFk() translates to a "INNER JOIN" when this is defined as defaultJoin in the schema');
 877    }
 878
 879    public function testUseFkQuerySimple()
 880    {
 881        $q = BookQuery::create()
 882            ->useAuthorQuery()
 883                ->filterByFirstName('Leo')
 884            ->endUse();
 885        $q1 = BookQuery::create()
 886            ->join('Book.Author', Criteria::LEFT_JOIN)
 887            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL);
 888        $this->assertTrue($q->equals($q1), 'useFkQuery() translates to a condition on a left join on non-required columns');
 889
 890        $q = BookSummaryQuery::create()
 891            ->useSummarizedBookQuery()
 892                ->filterByTitle('War And Peace')
 893            ->endUse();
 894        $q1 = BookSummaryQuery::create()
 895            ->join('BookSummary.SummarizedBook', Criteria::INNER_JOIN)
 896            ->add(BookPeer::TITLE, 'War And Peace', Criteria::EQUAL);
 897        $this->assertTrue($q->equals($q1), 'useFkQuery() translates to a condition on an inner join on required columns');
 898    }
 899
 900    public function testUseFkQueryJoinType()
 901    {
 902        $q = BookQuery::create()
 903            ->useAuthorQuery(null, Criteria::LEFT_JOIN)
 904                ->filterByFirstName('Leo')
 905            ->endUse();
 906        $q1 = BookQuery::create()
 907            ->join('Book.Author', Criteria::LEFT_JOIN)
 908            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL);
 909        $this->assertTrue($q->equals($q1), 'useFkQuery() accepts a join type as second parameter');
 910    }
 911
 912    public function testUseFkQueryAlias()
 913    {
 914        $q = BookQuery::create()
 915            ->useAuthorQuery('a')
 916                ->filterByFirstName('Leo')
 917            ->endUse();
 918        $join = new ModelJoin();
 919        $join->setJoinType(Criteria::LEFT_JOIN);
 920        $join->setTableMap(AuthorPeer::getTableMap());
 921        $join->setRelationMap(BookPeer::getTableMap()->getRelation('Author'), null, 'a');
 922        $join->setRelationAlias('a');
 923        $q1 = BookQuery::create()
 924            ->addAlias('a', AuthorPeer::TABLE_NAME)
 925            ->addJoinObject($join, 'a')
 926            ->add('a.first_name', 'Leo', Criteria::EQUAL);
 927        $this->assertTrue($q->equals($q1), 'useFkQuery() uses the first argument as a table alias');
 928    }
 929
 930    public function testUseFkQueryMixed()
 931    {
 932        $q = BookQuery::create()
 933            ->useAuthorQuery()
 934                ->filterByFirstName('Leo')
 935            ->endUse()
 936            ->filterByTitle('War And Peace');
 937        $q1 = BookQuery::create()
 938            ->join('Book.Author', Criteria::LEFT_JOIN)
 939            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL)
 940            ->add(BookPeer::TITLE, 'War And Peace', Criteria::EQUAL);
 941        $this->assertTrue($q->equals($q1), 'useFkQuery() allows combining conditions on main and related query');
 942    }
 943
 944    public function testUseFkQueryTwice()
 945    {
 946        $q = BookQuery::create()
 947            ->useAuthorQuery()
 948                ->filterByFirstName('Leo')
 949            ->endUse()
 950            ->useAuthorQuery()
 951                ->filterByLastName('Tolstoi')
 952            ->endUse();
 953        $q1 = BookQuery::create()
 954            ->join('Book.Author', Criteria::LEFT_JOIN)
 955            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL)
 956            ->add(AuthorPeer::LAST_NAME, 'Tolstoi', Criteria::EQUAL);
 957        $this->assertTrue($q->equals($q1), 'useFkQuery() called twice on the same relation does not create two joins');
 958    }
 959
 960    public function testUseFkQueryTwiceTwoAliases()
 961    {
 962        $q = BookQuery::create()
 963            ->useAuthorQuery('a')
 964                ->filterByFirstName('Leo')
 965            ->endUse()
 966            ->useAuthorQuery('b')
 967                ->filterByLastName('Tolstoi')
 968            ->endUse();
 969        $join1 = new ModelJoin();
 970        $join1->setJoinType(Criteria::LEFT_JOIN);
 971        $join1->setTableMap(AuthorPeer::getTableMap());
 972        $join1->setRelationMap(BookPeer::getTableMap()->getRelation('Author'), null, 'a');
 973        $join1->setRelationAlias('a');
 974        $join2 = new ModelJoin();
 975        $join2->setJoinType(Criteria::LEFT_JOIN);
 976        $join2->setTableMap(AuthorPeer::getTableMap());
 977        $join2->setRelationMap(BookPeer::getTableMap()->getRelation('Author'), null, 'b');
 978        $join2->setRelationAlias('b');
 979        $q1 = BookQuery::create()
 980            ->addAlias('a', AuthorPeer::TABLE_NAME)
 981            ->addJoinObject($join1, 'a')
 982            ->add('a.first_name', 'Leo', Criteria::EQUAL)
 983            ->addAlias('b', AuthorPeer::TABLE_NAME)
 984            ->addJoinObject($join2, 'b')
 985            ->add('b.last_name', 'Tolstoi', Criteria::EQUAL);
 986        $this->assertTrue($q->equals($q1), 'useFkQuery() called twice on the same relation with two aliases creates two joins');
 987    }
 988
 989    public function testUseFkQueryNested()
 990    {
 991        $q = ReviewQuery::create()
 992            ->useBookQuery()
 993                ->useAuthorQuery()
 994                    ->filterByFirstName('Leo')
 995                ->endUse()
 996            ->endUse();
 997        $q1 = ReviewQuery::create()
 998            ->join('Review.Book', Criteria::LEFT_JOIN)
 999            ->join('Book.Author', Criteria::LEFT_JOIN)
1000            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL);
1001        // embedded queries create joins that keep a relation to the parent
1002        // as this is not testable, we need to use another testing technique
1003        $params = array();
1004        $result = BasePeer::createSelectSql($q, $params);
1005        $expectedParams = array();
1006        $expectedResult = BasePeer::createSelectSql($q1, $expectedParams);
1007        $this->assertEquals($expectedParams, $params, 'useFkQuery() called nested creates two joins');
1008        $this->assertEquals($expectedResult, $result, 'useFkQuery() called nested creates two joins');
1009    }
1010
1011    public function testUseFkQueryTwoRelations()
1012    {
1013        $q = BookQuery::create()
1014            ->useAuthorQuery()
1015                ->filterByFirstName('Leo')
1016            ->endUse()
1017            ->usePublisherQuery()
1018                ->filterByName('Penguin')
1019            ->endUse();
1020        $q1 = BookQuery::create()
1021            ->join('Book.Author', Criteria::LEFT_JOIN)
1022            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL)
1023            ->join('Book.Publisher', Criteria::LEFT_JOIN)
1024            ->add(PublisherPeer::NAME, 'Penguin', Criteria::EQUAL);
1025        $this->assertTrue($q->equals($q1), 'useFkQuery() called twice on two relations creates two joins');
1026    }
1027
1028    public function testUseFkQueryNoAliasThenWith()
1029    {
1030        $con = Propel::getConnection();
1031        $books = BookQuery::create()
1032            ->useAuthorQuery()
1033                ->filterByFirstName('Leo')
1034            ->endUse()
1035            ->with('Author')
1036            ->find($con);
1037        $q1 = $con->getLastExecutedQuery();
1038        $books = BookQuery::create()
1039            ->leftJoinWithAuthor()
1040            ->add(AuthorPeer::FIRST_NAME, 'Leo', Criteria::EQUAL)
1041            ->find($con);
1042        $q2 = $con->getLastExecutedQuery();
1043        $this->assertEquals($q1, $q2, 'with() can be used after a call to useFkQuery() with no alias');
1044    }
1045
1046    public function testPrune()
1047    {
1048        $q = BookQuery::create()->prune();
1049        $this->assertTrue($q instanceof BookQuery, 'prune() returns the current Query object');
1050    }
1051
1052    public function testPruneSimpleKey()
1053    {
1054        BookstoreDataPopulator::depopulate();
1055        BookstoreDataPopulator::populate();
1056
1057        $nbBooks = BookQuery::create()->prune()->count();
1058        $this->assertEquals(4, $nbBooks, 'prune() does nothing when passed a null object');
1059
1060        $testBook = BookQuery::create()->findOne();
1061        $nbBooks = BookQuery::create()->prune($testBook)->count();
1062        $this->assertEquals(3, $nbBooks, 'prune() removes an object from the result');
1063    }
1064
1065    public function testPruneCompositeKey()
1066    {
1067        BookstoreDataPopulator::depopulate();
1068        BookstoreDataPopulator::populate();
1069
1070        // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved
1071        $c = new ModelCriteria('bookstore', 'Book');
1072        $books = $c->find();
1073        foreach ($books as $book) {
1074            $book->save();
1075        }
1076
1077        BookPeer::clearInstancePool();
1078
1079        $nbBookListRel = BookListRelQuery::create()->prune()->count();
1080        $this->assertEquals(2, $nbBookListRel, 'prune() does nothing when passed a null object');
1081
1082        $testBookListRel = BookListRelQuery::create()->findOne();
1083        $nbBookListRel = BookListRelQuery::create()->prune($testBookListRel)->count();
1084        $this->assertEquals(1, $nbBookListRel, 'prune() removes an object from the result');
1085    }
1086}
1087
1088class myCustomBookQuery extends BookQuery
1089{
1090    public static function create($modelAlias = null, $criteria = null)
1091    {
1092        if ($criteria instanceof myCustomBookQuery) {
1093            return $criteria;
1094        }
1095        $query = new myCustomBookQuery();
1096        if (null !== $modelAlias) {
1097            $query->setModelAlias($modelAlias);
1098        }
1099        if ($criteria instanceof Criteria) {
1100            $query->mergeWith($criteria);
1101        }
1102
1103        return $query;
1104    }
1105
1106}
1107
1108class mySecondBook…

Large files files are truncated, but you can click here to view the full file