PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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