PageRenderTime 58ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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