PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/1989gaurav/Propel
PHP | 439 lines | 273 code | 95 blank | 71 comment | 6 complexity | 650b681ed03d774f37bb7f87a92d3d6c MD5 | raw 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/BookstoreEmptyTestBase.php';
  10. /**
  11. * Tests the generated Peer classes.
  12. *
  13. * This test uses generated Bookstore classes to test the behavior of various
  14. * peer operations.
  15. *
  16. * The database is relaoded before every test and flushed after every test. This
  17. * means that you can always rely on the contents of the databases being the same
  18. * for each test method in this class. See the BookstoreDataPopulator::populate()
  19. * method for the exact contents of the database.
  20. *
  21. * @see BookstoreDataPopulator
  22. * @author Hans Lellelid <hans@xmpl.org>
  23. * @package generator.builder.om
  24. */
  25. class GeneratedPeerDoSelectTest extends BookstoreEmptyTestBase
  26. {
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. BookstoreDataPopulator::populate();
  31. }
  32. public function testDoSelect()
  33. {
  34. $books = BookPeer::doSelect(new Criteria());
  35. $this->assertEquals(4, count($books), 'doSelect() with an empty Criteria returns all results');
  36. $book1 = $books[0];
  37. $c = new Criteria();
  38. $c->add(BookPeer::ID, $book1->getId());
  39. $res = BookPeer::doSelect($c);
  40. $this->assertEquals(array($book1), $res, 'doSelect() accepts a Criteria object with a condition');
  41. $c = new Criteria();
  42. $c->add(BookPeer::ID, $book1->getId());
  43. $c->add(BookPeer::TITLE, $book1->getTitle());
  44. $res = BookPeer::doSelect($c);
  45. $this->assertEquals(array($book1), $res, 'doSelect() accepts a Criteria object with several condition');
  46. $c = new Criteria();
  47. $c->add(BookPeer::ID, 'foo');
  48. $res = BookPeer::doSelect($c);
  49. $this->assertEquals(array(), $res, 'doSelect() accepts an incorrect Criteria');
  50. }
  51. /**
  52. * Tests performing doSelect() and doSelectJoin() using LIMITs.
  53. */
  54. public function testDoSelect_Limit() {
  55. // 1) get the total number of items in a particular table
  56. $count = BookPeer::doCount(new Criteria());
  57. $this->assertTrue($count > 1, "Need more than 1 record in books table to perform this test.");
  58. $limitcount = $count - 1;
  59. $lc = new Criteria();
  60. $lc->setLimit($limitcount);
  61. $results = BookPeer::doSelect($lc);
  62. $this->assertEquals($limitcount, count($results), "Expected $limitcount results from BookPeer::doSelect()");
  63. // re-create it just to avoid side-effects
  64. $lc2 = new Criteria();
  65. $lc2->setLimit($limitcount);
  66. $results2 = BookPeer::doSelectJoinAuthor($lc2);
  67. $this->assertEquals($limitcount, count($results2), "Expected $limitcount results from BookPeer::doSelectJoinAuthor()");
  68. }
  69. /**
  70. * Test the basic functionality of the doSelectJoin*() methods.
  71. */
  72. public function testDoSelectJoin()
  73. {
  74. BookPeer::clearInstancePool();
  75. $c = new Criteria();
  76. $books = BookPeer::doSelect($c);
  77. $obj = $books[0];
  78. // $size = strlen(serialize($obj));
  79. BookPeer::clearInstancePool();
  80. $joinBooks = BookPeer::doSelectJoinAuthor($c);
  81. $obj2 = $joinBooks[0];
  82. $obj2Array = $obj2->toArray(BasePeer::TYPE_PHPNAME, true, array(), true);
  83. // $joinSize = strlen(serialize($obj2));
  84. $this->assertEquals(count($books), count($joinBooks), "Expected to find same number of rows in doSelectJoin*() call as doSelect() call.");
  85. // $this->assertTrue($joinSize > $size, "Expected a serialized join object to be larger than a non-join object.");
  86. $this->assertTrue(array_key_exists('Author', $obj2Array));
  87. }
  88. /**
  89. * Test the doSelectJoin*() methods when the related object is NULL.
  90. */
  91. public function testDoSelectJoin_NullFk()
  92. {
  93. $b1 = new Book();
  94. $b1->setTitle("Test NULLFK 1");
  95. $b1->setISBN("NULLFK-1");
  96. $b1->save();
  97. $b2 = new Book();
  98. $b2->setTitle("Test NULLFK 2");
  99. $b2->setISBN("NULLFK-2");
  100. $b2->setAuthor(new Author());
  101. $b2->getAuthor()->setFirstName("Hans")->setLastName("L");
  102. $b2->save();
  103. BookPeer::clearInstancePool();
  104. AuthorPeer::clearInstancePool();
  105. $c = new Criteria();
  106. $c->add(BookPeer::ISBN, 'NULLFK-%', Criteria::LIKE);
  107. $c->addAscendingOrderByColumn(BookPeer::ISBN);
  108. $matches = BookPeer::doSelectJoinAuthor($c);
  109. $this->assertEquals(2, count($matches), "Expected 2 matches back from new books; got back " . count($matches));
  110. $this->assertNull($matches[0]->getAuthor(), "Expected first book author to be null");
  111. $this->assertInstanceOf('Author', $matches[1]->getAuthor(), "Expected valid Author object for second book.");
  112. }
  113. public function testDoSelectJoinOneToOne()
  114. {
  115. $con = Propel::getConnection();
  116. $count = $con->getQueryCount();
  117. Propel::disableInstancePooling();
  118. $c = new Criteria();
  119. $accs = BookstoreEmployeeAccountPeer::doSelectJoinBookstoreEmployee($c);
  120. Propel::enableInstancePooling();
  121. $this->assertEquals(1, $con->getQueryCount() - $count, 'doSelectJoin() makes only one query in a one-to-one relationship');
  122. }
  123. public function testDoSelectOne()
  124. {
  125. $books = BookPeer::doSelect(new Criteria());
  126. $book1 = $books[0];
  127. $c = new Criteria();
  128. $c->add(BookPeer::ID, $book1->getId());
  129. $res = BookPeer::doSelectOne($c);
  130. $this->assertEquals($book1, $res, 'doSelectOne() returns a single object');
  131. $c = new Criteria();
  132. $c->add(BookPeer::ID, 'foo');
  133. $res = BookPeer::doSelectOne($c);
  134. $this->assertNull($res, 'doSelectOne() returns null if the Criteria matches no record');
  135. }
  136. public function testObjectInstances()
  137. {
  138. $sample = BookPeer::doSelectOne(new Criteria());
  139. $samplePk = $sample->getPrimaryKey();
  140. // 1) make sure consecutive calls to retrieveByPK() return the same object.
  141. $b1 = BookPeer::retrieveByPK($samplePk);
  142. $b2 = BookPeer::retrieveByPK($samplePk);
  143. $sampleval = md5(microtime());
  144. $this->assertTrue($b1 === $b2, "Expected object instances to match for calls with same retrieveByPK() method signature.");
  145. // 2) make sure that calls to doSelect also return references to the same objects.
  146. $allbooks = BookPeer::doSelect(new Criteria());
  147. foreach ($allbooks as $testb) {
  148. if ($testb->getPrimaryKey() == $b1->getPrimaryKey()) {
  149. $this->assertTrue($testb === $b1, "Expected same object instance from doSelect() as from retrieveByPK()");
  150. }
  151. }
  152. // 3) test fetching related objects
  153. $book = BookPeer::retrieveByPK($samplePk);
  154. $bookauthor = $book->getAuthor();
  155. $author = AuthorPeer::retrieveByPK($bookauthor->getId());
  156. $this->assertTrue($bookauthor === $author, "Expected same object instance when calling fk object accessor as retrieveByPK()");
  157. // 4) test a doSelectJoin()
  158. $morebooks = BookPeer::doSelectJoinAuthor(new Criteria());
  159. for ($i=0,$j=0; $j < count($morebooks); $i++, $j++) {
  160. $testb1 = $allbooks[$i];
  161. $testb2 = $allbooks[$j];
  162. $this->assertTrue($testb1 === $testb2, "Expected the same objects from consecutive doSelect() calls.");
  163. // we could probably also test this by just verifying that $book & $testb are the same
  164. if ($testb1->getPrimaryKey() === $book) {
  165. $this->assertTrue($book->getAuthor() === $testb1->getAuthor(), "Expected same author object in calls to pkey-matching books.");
  166. }
  167. }
  168. // 5) test creating a new object, saving it, and then retrieving that object (should all be same instance)
  169. $b = new BookstoreEmployee();
  170. $b->setName("Testing");
  171. $b->setJobTitle("Testing");
  172. $b->save();
  173. $empId = $b->getId();
  174. $this->assertSame($b, BookstoreEmployeePeer::retrieveByPK($empId), "Expected newly saved object to be same instance as pooled.");
  175. }
  176. /**
  177. * Test inheritance features.
  178. */
  179. public function testInheritance()
  180. {
  181. $manager = new BookstoreManager();
  182. $manager->setName("Manager 1");
  183. $manager->setJobTitle("Warehouse Manager");
  184. $manager->save();
  185. $managerId = $manager->getId();
  186. $employee = new BookstoreEmployee();
  187. $employee->setName("Employee 1");
  188. $employee->setJobTitle("Janitor");
  189. $employee->setSupervisorId($managerId);
  190. $employee->save();
  191. $empId = $employee->getId();
  192. $cashier = new BookstoreCashier();
  193. $cashier->setName("Cashier 1");
  194. $cashier->setJobTitle("Cashier");
  195. $cashier->save();
  196. $cashierId = $cashier->getId();
  197. // 1) test the pooled instances'
  198. $c = new Criteria();
  199. $c->add(BookstoreEmployeePeer::ID, array($managerId, $empId, $cashierId), Criteria::IN);
  200. $c->addAscendingOrderByColumn(BookstoreEmployeePeer::ID);
  201. $objects = BookstoreEmployeePeer::doSelect($c);
  202. $this->assertEquals(3, count($objects), "Expected 3 objects to be returned.");
  203. list($o1, $o2, $o3) = $objects;
  204. $this->assertSame($o1, $manager);
  205. $this->assertSame($o2, $employee);
  206. $this->assertSame($o3, $cashier);
  207. // 2) test a forced reload from database
  208. BookstoreEmployeePeer::clearInstancePool();
  209. list($o1,$o2,$o3) = BookstoreEmployeePeer::doSelect($c);
  210. $this->assertTrue($o1 instanceof BookstoreManager, "Expected BookstoreManager object, got " . get_class($o1));
  211. $this->assertTrue($o2 instanceof BookstoreEmployee, "Expected BookstoreEmployee object, got " . get_class($o2));
  212. $this->assertTrue($o3 instanceof BookstoreCashier, "Expected BookstoreCashier object, got " . get_class($o3));
  213. }
  214. /**
  215. * Test hydration of joined rows that contain lazy load columns.
  216. * @link http://propel.phpdb.org/trac/ticket/464
  217. */
  218. public function testHydrationJoinLazyLoad()
  219. {
  220. BookstoreEmployeeAccountPeer::doDeleteAll();
  221. BookstoreEmployeePeer::doDeleteAll();
  222. AcctAccessRolePeer::doDeleteAll();
  223. $bemp2 = new BookstoreEmployee();
  224. $bemp2->setName("Pieter");
  225. $bemp2->setJobTitle("Clerk");
  226. $bemp2->save();
  227. $role = new AcctAccessRole();
  228. $role->setName("Admin");
  229. $bempacct = new BookstoreEmployeeAccount();
  230. $bempacct->setBookstoreEmployee($bemp2);
  231. $bempacct->setAcctAccessRole($role);
  232. $bempacct->setLogin("john");
  233. $bempacct->setPassword("johnp4ss");
  234. $bempacct->save();
  235. $c = new Criteria();
  236. $results = BookstoreEmployeeAccountPeer::doSelectJoinAll($c);
  237. $o = $results[0];
  238. $this->assertEquals('Admin', $o->getAcctAccessRole()->getName());
  239. }
  240. /**
  241. * Testing foreign keys with multiple referrer columns.
  242. * @link http://propel.phpdb.org/trac/ticket/606
  243. */
  244. public function testMultiColFk()
  245. {
  246. $con = Propel::getConnection(BookPeer::DATABASE_NAME);
  247. ReaderFavoritePeer::doDeleteAll();
  248. $b1 = new Book();
  249. $b1->setTitle("Book1");
  250. $b1->setISBN("ISBN-1");
  251. $b1->save();
  252. $r1 = new BookReader();
  253. $r1-> setName("Me");
  254. $r1->save();
  255. $bo1 = new BookOpinion();
  256. $bo1->setBookId($b1->getId());
  257. $bo1->setReaderId($r1->getId());
  258. $bo1->setRating(9);
  259. $bo1->setRecommendToFriend(true);
  260. $bo1->save();
  261. $rf1 = new ReaderFavorite();
  262. $rf1->setReaderId($r1->getId());
  263. $rf1->setBookId($b1->getId());
  264. $rf1->save();
  265. $c = new Criteria(ReaderFavoritePeer::DATABASE_NAME);
  266. $c->add(ReaderFavoritePeer::BOOK_ID, $b1->getId());
  267. $c->add(ReaderFavoritePeer::READER_ID, $r1->getId());
  268. $results = ReaderFavoritePeer::doSelectJoinBookOpinion($c);
  269. $this->assertEquals(1, count($results), "Expected 1 result");
  270. }
  271. /**
  272. * Testing foreign keys with multiple referrer columns.
  273. * @link http://propel.phpdb.org/trac/ticket/606
  274. */
  275. public function testMultiColJoin()
  276. {
  277. BookstoreContestPeer::doDeleteAll();
  278. BookstoreContestEntryPeer::doDeleteAll();
  279. $bs = new Bookstore();
  280. $bs->setStoreName("Test1");
  281. $bs->setPopulationServed(5);
  282. $bs->save();
  283. $bs1Id = $bs->getId();
  284. $bs2 = new Bookstore();
  285. $bs2->setStoreName("Test2");
  286. $bs2->setPopulationServed(5);
  287. $bs2->save();
  288. $bs2Id = $bs2->getId();
  289. $ct1 = new Contest();
  290. $ct1->setName("Contest1!");
  291. $ct1->save();
  292. $ct1Id = $ct1->getId();
  293. $ct2 = new Contest();
  294. $ct2->setName("Contest2!");
  295. $ct2->save();
  296. $ct2Id = $ct2->getId();
  297. $cmr = new Customer();
  298. $cmr->setName("Customer1");
  299. $cmr->save();
  300. $cmr1Id = $cmr->getId();
  301. $cmr2 = new Customer();
  302. $cmr2->setName("Customer2");
  303. $cmr2->save();
  304. $cmr2Id = $cmr2->getId();
  305. $contest = new BookstoreContest();
  306. $contest->setBookstoreId($bs1Id);
  307. $contest->setContestId($ct1Id);
  308. $contest->save();
  309. $contest = new BookstoreContest();
  310. $contest->setBookstoreId($bs2Id);
  311. $contest->setContestId($ct1Id);
  312. $contest->save();
  313. $entry = new BookstoreContestEntry();
  314. $entry->setBookstoreId($bs1Id);
  315. $entry->setContestId($ct1Id);
  316. $entry->setCustomerId($cmr1Id);
  317. $entry->save();
  318. $entry = new BookstoreContestEntry();
  319. $entry->setBookstoreId($bs1Id);
  320. $entry->setContestId($ct1Id);
  321. $entry->setCustomerId($cmr2Id);
  322. $entry->save();
  323. // Note: this test isn't really working very well. We setup fkeys that
  324. // require that the BookstoreContest rows exist and then try to violate
  325. // the rules ... :-/ This may work in some lenient databases, but an error
  326. // is expected here.
  327. /*
  328. * Commented out for now ... though without it, this test may not really be testing anything
  329. $entry = new BookstoreContestEntry();
  330. $entry->setBookstoreId($bs1Id);
  331. $entry->setContestId($ct2Id);
  332. $entry->setCustomerId($cmr2Id);
  333. $entry->save();
  334. */
  335. $c = new Criteria();
  336. $c->addJoin(array(BookstoreContestEntryPeer::BOOKSTORE_ID, BookstoreContestEntryPeer::CONTEST_ID), array(BookstoreContestPeer::BOOKSTORE_ID, BookstoreContestPeer::CONTEST_ID) );
  337. $results = BookstoreContestEntryPeer::doSelect($c);
  338. $this->assertEquals(2, count($results) );
  339. foreach ($results as $result) {
  340. $this->assertEquals($bs1Id, $result->getBookstoreId() );
  341. $this->assertEquals($ct1Id, $result->getContestId() );
  342. }
  343. }
  344. }