PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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