PageRenderTime 392ms CodeModel.GetById 90ms app.highlight 150ms RepoModel.GetById 61ms app.codeStats 1ms

/tests/Propel/Tests/Generator/Builder/Om/QueryBuilderTest.php

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

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