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

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

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