PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/propelorm/Propel
PHP | 1000 lines | 707 code | 194 blank | 99 comment | 3 complexity | 1deab95922b31fad41592087cd65c06d 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 relationships between generated Object classes.
  12. *
  13. * This test uses generated Bookstore classes to test the behavior of various
  14. * object operations. The _idea_ here is to test every possible generated method
  15. * from Object.tpl; if necessary, bookstore will be expanded to accommodate this.
  16. *
  17. * The database is reloaded before every test and flushed after every test. This
  18. * means that you can always rely on the contents of the databases being the same
  19. * for each test method in this class. See the BookstoreDataPopulator::populate()
  20. * method for the exact contents of the database.
  21. *
  22. * @see BookstoreDataPopulator
  23. * @author Hans Lellelid <hans@xmpl.org>
  24. * @package generator.builder.om
  25. */
  26. class GeneratedObjectRelTest extends BookstoreEmptyTestBase
  27. {
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. }
  32. /**
  33. * Tests one side of a bi-directional setting of many-to-many relationships.
  34. */
  35. public function testManyToMany_Dir1()
  36. {
  37. $list = new BookClubList();
  38. $list->setGroupLeader('Archimedes Q. Porter');
  39. // No save ...
  40. $book = new Book();
  41. $book->setTitle( "Jungle Expedition Handbook" );
  42. $book->setIsbn('TEST');
  43. // No save ...
  44. $this->assertEquals(0, count($list->getBookListRels()) );
  45. $this->assertEquals(0, count($book->getBookListRels()) );
  46. $this->assertEquals(0, count(BookListRelPeer::doSelect(new Criteria())) );
  47. $xref = new BookListRel();
  48. $xref->setBook($book);
  49. $list->addBookListRel($xref);
  50. $this->assertEquals(1, count($list->getBookListRels()));
  51. $this->assertEquals(1, count($book->getBookListRels()) );
  52. $this->assertEquals(0, count(BookListRelPeer::doSelect(new Criteria())) );
  53. $list->save();
  54. $this->assertEquals(1, count($list->getBookListRels()) );
  55. $this->assertEquals(1, count($book->getBookListRels()) );
  56. $this->assertEquals(1, count(BookListRelPeer::doSelect(new Criteria())) );
  57. }
  58. /**
  59. * Tests reverse setting of one of many-to-many relationship, with all saves cascaded.
  60. */
  61. public function testManyToMany_Dir2_Unsaved()
  62. {
  63. $list = new BookClubList();
  64. $list->setGroupLeader('Archimedes Q. Porter');
  65. // No save ...
  66. $book = new Book();
  67. $book->setTitle( "Jungle Expedition Handbook" );
  68. $book->setIsbn('TEST');
  69. // No save (yet) ...
  70. $this->assertEquals(0, count($list->getBookListRels()) );
  71. $this->assertEquals(0, count($book->getBookListRels()) );
  72. $this->assertEquals(0, count(BookListRelPeer::doSelect(new Criteria())) );
  73. $xref = new BookListRel();
  74. $xref->setBookClubList($list);
  75. $book->addBookListRel($xref);
  76. $this->assertEquals(1, count($list->getBookListRels()) );
  77. $this->assertEquals(1, count($book->getBookListRels()) );
  78. $this->assertEquals(0, count(BookListRelPeer::doSelect(new Criteria())) );
  79. $book->save();
  80. $this->assertEquals(1, count($list->getBookListRels()) );
  81. $this->assertEquals(1, count($book->getBookListRels()) );
  82. $this->assertEquals(1, count(BookListRelPeer::doSelect(new Criteria())) );
  83. }
  84. /**
  85. * Tests reverse setting of relationships, saving one of the objects first.
  86. * @link http://trac.propelorm.org/ticket/508
  87. */
  88. public function testManyToMany_Dir2_Saved()
  89. {
  90. $list = new BookClubList();
  91. $list->setGroupLeader('Archimedes Q. Porter');
  92. $list->save();
  93. $book = new Book();
  94. $book->setTitle( "Jungle Expedition Handbook" );
  95. $book->setIsbn('TEST');
  96. // No save (yet) ...
  97. $this->assertEquals(0, count($list->getBookListRels()) );
  98. $this->assertEquals(0, count($book->getBookListRels()) );
  99. $this->assertEquals(0, count(BookListRelPeer::doSelect(new Criteria())) );
  100. // Now set the relationship from the opposite direction.
  101. $xref = new BookListRel();
  102. $xref->setBookClubList($list);
  103. $book->addBookListRel($xref);
  104. $this->assertEquals(1, count($list->getBookListRels()) );
  105. $this->assertEquals(1, count($book->getBookListRels()) );
  106. $this->assertEquals(0, count(BookListRelPeer::doSelect(new Criteria())) );
  107. $book->save();
  108. $this->assertEquals(1, count($list->getBookListRels()) );
  109. $this->assertEquals(1, count($book->getBookListRels()) );
  110. $this->assertEquals(1, count(BookListRelPeer::doSelect(new Criteria())) );
  111. }
  112. public function testManyToManyGetterExists()
  113. {
  114. $this->assertTrue(method_exists('BookClubList', 'getBooks'), 'Object generator correctly adds getter for the crossRefFk');
  115. $this->assertFalse(method_exists('BookClubList', 'getBookClubLists'), 'Object generator correctly adds getter for the crossRefFk');
  116. }
  117. public function testManyToManyGetterNewObject()
  118. {
  119. $blc1 = new BookClubList();
  120. $books = $blc1->getBooks();
  121. $this->assertTrue($books instanceof PropelObjectCollection, 'getCrossRefFK() returns a Propel collection');
  122. $this->assertEquals('Book', $books->getModel(), 'getCrossRefFK() returns a collection of the correct model');
  123. $this->assertEquals(0, count($books), 'getCrossRefFK() returns an empty list for new objects');
  124. $query = BookQuery::create()
  125. ->filterByTitle('Harry Potter and the Order of the Phoenix');
  126. $books = $blc1->getBooks($query);
  127. $this->assertEquals(0, count($books), 'getCrossRefFK() accepts a query as first parameter');
  128. }
  129. public function testManyToManyGetter()
  130. {
  131. BookstoreDataPopulator::populate();
  132. $blc1 = BookClubListQuery::create()->findOneByGroupLeader('Crazyleggs');
  133. $books = $blc1->getBooks();
  134. $this->assertTrue($books instanceof PropelObjectCollection, 'getCrossRefFK() returns a Propel collection');
  135. $this->assertEquals('Book', $books->getModel(), 'getCrossRefFK() returns a collection of the correct model');
  136. $this->assertEquals(2, count($books), 'getCrossRefFK() returns the correct list of objects');
  137. $query = BookQuery::create()
  138. ->filterByTitle('Harry Potter and the Order of the Phoenix');
  139. $books = $blc1->getBooks($query);
  140. $this->assertEquals(1, count($books), 'getCrossRefFK() accepts a query as first parameter');
  141. }
  142. public function testOneToManyGetter()
  143. {
  144. BookstoreDataPopulator::populate(null, true);
  145. $author = AuthorQuery::create()->findOneByLastName('Grass');
  146. $books = $author->getBooks(new Criteria());
  147. $this->assertNotNull($books->getCurrent(), 'getRelCol() initialize the internal iterator at the beginning');
  148. }
  149. /**
  150. * @group issue677
  151. */
  152. public function testManyToManySetterIsNotLoosingAnyReference()
  153. {
  154. $list1 = new BookClubList();
  155. $list2 = new BookClubList();
  156. $book = new Book();
  157. $book->addBookClubList($list1);
  158. $book->addBookClubList($list2);
  159. $lists = $book->getBookClubLists();
  160. $this->assertCount(2, $lists, 'setRelCol is losing references to referenced object');
  161. $rels = $book->getBookListRels();
  162. $this->assertCount(2, $rels, 'setRelCol is losing references to relation object');
  163. foreach ($rels as $rel) {
  164. $this->assertNotNull($rel->getBook(), 'setRelCol is losing backreference on set relation to local object');
  165. $this->assertNotNull($rel->getBookClubList(), 'setRelCol is losing backreference on set relation to referenced object');
  166. }
  167. foreach ($lists as $list) {
  168. $this->assertCount(1, $list->getBooks(), 'setRelCol is losing backreference on set objects');
  169. }
  170. }
  171. public function testManyToManyCounterExists()
  172. {
  173. $this->assertTrue(method_exists('BookClubList', 'countBooks'), 'Object generator correcly adds counter for the crossRefFk');
  174. $this->assertFalse(method_exists('BookClubList', 'countBookClubLists'), 'Object generator correcly adds counter for the crossRefFk');
  175. }
  176. public function testManyToManyCounterNewObject()
  177. {
  178. $blc1 = new BookClubList();
  179. $nbBooks = $blc1->countBooks();
  180. $this->assertEquals(0, $nbBooks, 'countCrossRefFK() returns 0 for new objects');
  181. $query = BookQuery::create()
  182. ->filterByTitle('Harry Potter and the Order of the Phoenix');
  183. $nbBooks = $blc1->countBooks($query);
  184. $this->assertEquals(0, $nbBooks, 'countCrossRefFK() accepts a query as first parameter');
  185. }
  186. public function testManyToManyCounter()
  187. {
  188. BookstoreDataPopulator::populate();
  189. $blc1 = BookClubListQuery::create()->findOneByGroupLeader('Crazyleggs');
  190. $nbBooks = $blc1->countBooks();
  191. $this->assertEquals(2, $nbBooks, 'countCrossRefFK() returns the correct list of objects');
  192. $query = BookQuery::create()
  193. ->filterByTitle('Harry Potter and the Order of the Phoenix');
  194. $nbBooks = $blc1->countBooks($query);
  195. $this->assertEquals(1, $nbBooks, 'countCrossRefFK() accepts a query as first parameter');
  196. }
  197. public function testManyToManyAdd()
  198. {
  199. $list = new BookClubList();
  200. $list->setGroupLeader('Archimedes Q. Porter');
  201. $book = new Book();
  202. $book->setTitle( "Jungle Expedition Handbook" );
  203. $book->setIsbn('TEST');
  204. $list->addBook($book);
  205. $this->assertEquals(1, $list->countBooks(), 'addCrossFk() sets the internal collection properly');
  206. $this->assertEquals(1, $list->countBookListRels(), 'addCrossFk() sets the internal cross reference collection properly');
  207. $list->save();
  208. $this->assertFalse($book->isNew(), 'related object is saved if added');
  209. $rels = $list->getBookListRels();
  210. $rel = $rels[0];
  211. $this->assertFalse($rel->isNew(), 'cross object is saved if added');
  212. $list->clearBookListRels();
  213. $list->clearBooks();
  214. $books = $list->getBooks();
  215. $expected = new PropelObjectCollection(array($book));
  216. $expected->setModel('Book');
  217. $this->assertEquals($expected, $books, 'addCrossFk() adds the object properly');
  218. $this->assertEquals(1, $list->countBookListRels());
  219. }
  220. /**
  221. * Test behavior of columns that are implicated in multiple foreign keys.
  222. * @link http://trac.propelorm.org/ticket/228
  223. */
  224. public function testMultiFkImplication()
  225. {
  226. BookstoreDataPopulator::populate();
  227. // Create a new bookstore, contest, bookstore_contest, and bookstore_contest_entry
  228. $b = new Bookstore();
  229. $b->setStoreName("Foo!");
  230. $b->save();
  231. $c = new Contest();
  232. $c->setName("Bookathon Contest");
  233. $c->save();
  234. $bc = new BookstoreContest();
  235. $bc->setBookstore($b);
  236. $bc->setContest($c);
  237. $bc->save();
  238. $c = new Customer();
  239. $c->setName("Happy Customer");
  240. $c->save();
  241. $bce = new BookstoreContestEntry();
  242. $bce->setBookstore($b);
  243. $bce->setBookstoreContest($bc);
  244. $bce->setCustomer($c);
  245. $bce->save();
  246. $bce->setBookstoreId(null);
  247. $this->assertNull($bce->getBookstoreContest());
  248. $this->assertNull($bce->getBookstore());
  249. }
  250. /**
  251. * Test the clearing of related object collection.
  252. * @link http://www.propelorm.org/ticket/529
  253. */
  254. public function testClearRefFk()
  255. {
  256. BookstoreDataPopulator::populate();
  257. $book = new Book();
  258. $book->setIsbn("Foo-bar-baz");
  259. $book->setTitle("The book title");
  260. // No save ...
  261. $r = new Review();
  262. $r->setReviewedBy('Me');
  263. $r->setReviewDate(new DateTime("now"));
  264. $book->addReview($r);
  265. // No save (yet) ...
  266. $this->assertEquals(1, count($book->getReviews()) );
  267. $book->clearReviews();
  268. $this->assertEquals(0, count($book->getReviews()));
  269. }
  270. /**
  271. * Test the clearing of related object collection via a many-to-many association.
  272. * @link http://www.propelorm.org/ticket/1374
  273. */
  274. public function testClearCrossFk()
  275. {
  276. $book = new Book();
  277. $bookClub = new BookClubList();
  278. $book->addBookClubList($bookClub);
  279. $this->assertEquals(1, count($book->getBookClubLists()));
  280. $book->clear();
  281. $this->assertEquals(0, count($book->getBookClubLists()));
  282. }
  283. /**
  284. * This tests to see whether modified objects are being silently overwritten by calls to fk accessor methods.
  285. * @link http://trac.propelorm.org/ticket/509#comment:5
  286. */
  287. public function testModifiedObjectOverwrite()
  288. {
  289. BookstoreDataPopulator::populate();
  290. $author = new Author();
  291. $author->setFirstName("John");
  292. $author->setLastName("Public");
  293. $books = $author->getBooks(); // empty, of course
  294. $this->assertEquals(0, count($books), "Expected empty collection.");
  295. $book = new Book();
  296. $book->setTitle("A sample book");
  297. $book->setIsbn("INITIAL ISBN");
  298. $author->addBook($book);
  299. $author->save();
  300. $book->setIsbn("MODIFIED ISBN");
  301. $books = $author->getBooks();
  302. $this->assertEquals(1, count($books), "Expected 1 book.");
  303. $this->assertSame($book, $books[0], "Expected the same object to be returned by fk accessor.");
  304. $this->assertEquals("MODIFIED ISBN", $books[0]->getISBN(), "Expected the modified value NOT to have been overwritten.");
  305. }
  306. public function testFKGetterUseInstancePool()
  307. {
  308. BookstoreDataPopulator::populate();
  309. BookPeer::clearInstancePool();
  310. AuthorPeer::clearInstancePool();
  311. $con = Propel::getConnection(BookPeer::DATABASE_NAME);
  312. $author = AuthorPeer::doSelectOne(new Criteria(), $con);
  313. // populate book instance pool
  314. $books = $author->getBooks(null, $con);
  315. $sql = $con->getLastExecutedQuery();
  316. $author = $books[0]->getAuthor($con);
  317. $this->assertEquals($sql, $con->getLastExecutedQuery(), 'refFK getter uses instance pool if possible');
  318. }
  319. public function testRefFKGetJoin()
  320. {
  321. BookstoreDataPopulator::populate();
  322. BookPeer::clearInstancePool();
  323. AuthorPeer::clearInstancePool();
  324. PublisherPeer::clearInstancePool();
  325. $con = Propel::getConnection(BookPeer::DATABASE_NAME);
  326. $author = AuthorPeer::doSelectOne(new Criteria(), $con);
  327. // populate book instance pool
  328. $books = $author->getBooksJoinPublisher(null, $con);
  329. $sql = $con->getLastExecutedQuery();
  330. $publisher = $books[0]->getPublisher($con);
  331. $this->assertEquals($sql, $con->getLastExecutedQuery(), 'refFK getter uses instance pool if possible');
  332. }
  333. public function testRefFKAddReturnsCurrentObject()
  334. {
  335. $author = new Author();
  336. $author->setFirstName('Leo');
  337. $ret = $author->addBook(new Book());
  338. $this->assertSame($author, $ret);
  339. }
  340. public function testSetterCollection()
  341. {
  342. // Ensure no data
  343. BookQuery::create()->deleteAll();
  344. BookClubListQuery::create()->deleteAll();
  345. BookListRelQuery::create()->deleteAll();
  346. $books = new PropelObjectCollection();
  347. for ($i = 0; $i < 10; $i++) {
  348. $b = new Book();
  349. $b->setTitle('My Book ' . $i);
  350. $b->setIsbn($i);
  351. $books[] = $b;
  352. }
  353. $this->assertEquals(10, $books->count());
  354. // Basic usage
  355. $bookClubList1 = new BookClubList();
  356. $bookClubList1->setGroupLeader('BookClubList1 Leader');
  357. $bookClubList1->setBooks($books);
  358. $bookClubList1->save();
  359. $this->assertEquals(10, $bookClubList1->getBooks()->count());
  360. $this->assertEquals(1, BookClubListQuery::create()->count());
  361. $this->assertEquals(10, BookQuery::create()->count());
  362. $this->assertEquals(10, BookListRelQuery::create()->count());
  363. $i = 0;
  364. foreach ($bookClubList1->getBooks() as $book) {
  365. $this->assertEquals('My Book ' . $i, $book->getTitle());
  366. $this->assertEquals($i++, $book->getIsbn());
  367. }
  368. // Remove an element
  369. $books->shift();
  370. $this->assertEquals(9, $books->count());
  371. $bookClubList1->setBooks($books);
  372. $bookClubList1->save();
  373. $this->assertEquals(9, $bookClubList1->getBooks()->count());
  374. $this->assertEquals(1, BookClubListQuery::create()->count());
  375. $this->assertEquals(9, BookListRelQuery::create()->count());
  376. $this->assertEquals(10, BookQuery::create()->count());
  377. // Add a new object
  378. $newBook = new Book();
  379. $newBook->setTitle('My New Book');
  380. $newBook->setIsbn(1234);
  381. // Kind of new collection
  382. $books = clone $books;
  383. $books[] = $newBook;
  384. $bookClubList1->setBooks($books);
  385. $bookClubList1->save();
  386. $this->assertEquals(10, $books->count());
  387. $this->assertEquals(10, $bookClubList1->getBooks()->count());
  388. $this->assertEquals(1, BookClubListQuery::create()->count());
  389. $this->assertEquals(10, BookListRelQuery::create()->count());
  390. $this->assertEquals(11, BookQuery::create()->count());
  391. // Add a new object
  392. $newBook1 = new Book();
  393. $newBook1->setTitle('My New Book1');
  394. $newBook1->setIsbn(1256);
  395. // Existing collection - The fix around reference is tested here.
  396. // Ths `$books` collection has ever been setted to the `$bookClubList1` object.
  397. // Here we are adding a new object into the collection but, in this process, it
  398. // added the new object in the internal `collBooks` of the `$bookClubList1`
  399. // object too.
  400. // That's why the new object is not tracked and the cross object is not created,
  401. // in `addBook()` we consider the `collBooks` ever contains this new object. It's
  402. // not true but this is the "reference" process.
  403. // By saying "all new objects have to be added", we solve this issue. To know if
  404. // it's the best solution is the question.
  405. $books[] = $newBook1;
  406. $bookClubList1->setBooks($books);
  407. $bookClubList1->save();
  408. $this->assertEquals(11, $books->count());
  409. $this->assertEquals(11, $bookClubList1->getBooks()->count());
  410. $this->assertEquals(1, BookClubListQuery::create()->count());
  411. $this->assertEquals(11, BookListRelQuery::create()->count());
  412. $this->assertEquals(12, BookQuery::create()->count());
  413. // Add the same collection
  414. $books = $bookClubList1->getBooks();
  415. $bookClubList1->setBooks($books);
  416. $bookClubList1->save();
  417. $this->assertEquals(11, $books->count());
  418. $this->assertEquals(11, $bookClubList1->getBooks()->count());
  419. $this->assertEquals(1, BookClubListQuery::create()->count());
  420. $this->assertEquals(11, BookListRelQuery::create()->count());
  421. $this->assertEquals(12, BookQuery::create()->count());
  422. }
  423. public function testSetterCollectionWithNoData()
  424. {
  425. // Ensure no data
  426. BookQuery::create()->deleteAll();
  427. BookClubListQuery::create()->deleteAll();
  428. BookListRelQuery::create()->deleteAll();
  429. $books = new PropelObjectCollection();
  430. $this->assertEquals(0, $books->count());
  431. // Basic usage
  432. $bookClubList1 = new BookClubList();
  433. $bookClubList1->setGroupLeader('BookClubList1 Leader');
  434. $bookClubList1->setBooks($books);
  435. $bookClubList1->save();
  436. $this->assertEquals(0, $bookClubList1->getBooks()->count());
  437. $this->assertEquals(1, BookClubListQuery::create()->count());
  438. $this->assertEquals(0, BookQuery::create()->count());
  439. $this->assertEquals(0, BookListRelQuery::create()->count());
  440. }
  441. public function testSetterCollectionSavesForeignObjects()
  442. {
  443. // Ensure no data
  444. BookQuery::create()->deleteAll();
  445. BookClubListQuery::create()->deleteAll();
  446. BookListRelQuery::create()->deleteAll();
  447. $book = new Book();
  448. $book->setTitle('My Book');
  449. $book->setIsbn('123452');
  450. $book->save();
  451. // Modify it but don't save it
  452. $book->setTitle('My Title');
  453. $coll = new PropelObjectCollection();
  454. $coll[] = $book;
  455. BookPeer::clearInstancePool();
  456. $book = BookQuery::create()->findPk($book->getPrimaryKey());
  457. $bookClubList1 = new BookClubList();
  458. $bookClubList1->setGroupLeader('Something');
  459. $bookClubList1->setBooks($coll);
  460. $bookClubList1->save();
  461. $this->assertEquals(1, $bookClubList1->getBooks()->count());
  462. $this->assertEquals(1, BookClubListQuery::create()->count());
  463. $this->assertEquals(1, BookQuery::create()->count());
  464. $this->assertEquals(1, BookListRelQuery::create()->count());
  465. $result = BookQuery::create()
  466. ->filterById($book->getId())
  467. ->select('Title')
  468. ->findOne();
  469. $this->assertSame('My Title', $result);
  470. }
  471. public function testSetterCollectionWithNewObjects()
  472. {
  473. // Ensure no data
  474. BookQuery::create()->deleteAll();
  475. BookClubListQuery::create()->deleteAll();
  476. BookListRelQuery::create()->deleteAll();
  477. $coll = new PropelObjectCollection();
  478. $coll->setModel('Book');
  479. for ($i = 0; $i < 3; $i++) {
  480. $b = new Book();
  481. $b->setTitle('Title ' . $i);
  482. $b->setIsbn('1245' . $i);
  483. $coll[] = $b;
  484. }
  485. $bookClubList = new BookClubList();
  486. $bookClubList->setGroupLeader('Something');
  487. $bookClubList->setBooks($coll);
  488. $bookClubList->save();
  489. $this->assertEquals(3, $coll->count());
  490. $this->assertEquals(3, count($bookClubList->getBooks()));
  491. $this->assertSame($coll, $bookClubList->getBooks());
  492. $this->assertEquals(3, BookQuery::create()->count());
  493. $this->assertEquals(1, BookClubListQuery::create()->count());
  494. $this->assertEquals(3, BookListRelQuery::create()->count());
  495. }
  496. public function testSetterCollectionWithExistingObjects()
  497. {
  498. // Ensure no data
  499. BookQuery::create()->deleteAll();
  500. BookClubListQuery::create()->deleteAll();
  501. BookListRelQuery::create()->deleteAll();
  502. for ($i = 0; $i < 3; $i++) {
  503. $b = new Book();
  504. $b->setTitle('Book ' . $i);
  505. $b->setIsbn('123445' . $i);
  506. $b->save();
  507. }
  508. BookPeer::clearInstancePool();
  509. $books = BookQuery::create()->find();
  510. $bookClubList = new BookClubList();
  511. $bookClubList->setGroupLeader('Something');
  512. $bookClubList->setBooks($books);
  513. $bookClubList->save();
  514. $this->assertEquals(3, count($bookClubList->getBooks()));
  515. $this->assertEquals(3, BookQuery::create()->count());
  516. $this->assertEquals(1, BookClubListQuery::create()->count());
  517. $this->assertEquals(3, BookListRelQuery::create()->count());
  518. $i = 0;
  519. foreach ($bookClubList->getBooks() as $book) {
  520. $this->assertEquals('Book ' . $i++, $book->getTitle());
  521. }
  522. }
  523. public function testSetterCollectionWithEmptyCollection()
  524. {
  525. // Ensure no data
  526. BookQuery::create()->deleteAll();
  527. BookClubListQuery::create()->deleteAll();
  528. BookListRelQuery::create()->deleteAll();
  529. $bookClubList = new BookClubList();
  530. $bookClubList->setGroupLeader('Something');
  531. $bookClubList->setBooks(new PropelObjectCollection());
  532. $bookClubList->save();
  533. $this->assertEquals(0, count($bookClubList->getBooks()));
  534. $this->assertEquals(0, BookQuery::create()->count());
  535. $this->assertEquals(1, BookClubListQuery::create()->count());
  536. $this->assertEquals(0, BookListRelQuery::create()->count());
  537. }
  538. public function testSetterCollectionReplacesOldObjectsByNewObjects()
  539. {
  540. // Ensure no data
  541. BookQuery::create()->deleteAll();
  542. BookClubListQuery::create()->deleteAll();
  543. BookListRelQuery::create()->deleteAll();
  544. $books = new PropelObjectCollection();
  545. foreach (array('foo', 'bar') as $title) {
  546. $b = new Book();
  547. $b->setTitle($title);
  548. $b->setIsbn('12553');
  549. $books[] = $b;
  550. }
  551. $bookClubList = new BookClubList();
  552. $bookClubList->setGroupLeader('Something');
  553. $bookClubList->setBooks($books);
  554. $bookClubList->save();
  555. $books = $bookClubList->getBooks();
  556. $this->assertEquals('foo', $books[0]->getTitle());
  557. $this->assertEquals('bar', $books[1]->getTitle());
  558. $books = new PropelObjectCollection();
  559. foreach (array('bam', 'bom') as $title) {
  560. $b = new Book();
  561. $b->setTitle($title);
  562. $b->setIsbn('1345');
  563. $books[] = $b;
  564. }
  565. $bookClubList->setBooks($books);
  566. $bookClubList->save();
  567. $books = $bookClubList->getBooks();
  568. $this->assertEquals('bam', $books[0]->getTitle());
  569. $this->assertEquals('bom', $books[1]->getTitle());
  570. $this->assertEquals(1, BookClubListQuery::create()->count());
  571. $this->assertEquals(2, BookListRelQuery::create()->count());
  572. // ensure we have valid "association" objects
  573. $this->assertEquals(1, BookListRelQuery::create()
  574. ->filterByBookClubList($bookClubList)
  575. ->filterByBook($books[0])
  576. ->count()
  577. );
  578. $this->assertEquals(1, BookListRelQuery::create()
  579. ->filterByBookClubList($bookClubList)
  580. ->filterByBook($books[1])
  581. ->count()
  582. );
  583. $this->assertEquals(4, BookQuery::create()->count());
  584. }
  585. public function testSetterCollectionWithManyToManyModifiedByReferenceWithANewObject()
  586. {
  587. // Ensure no data
  588. BookQuery::create()->deleteAll();
  589. BookClubListQuery::create()->deleteAll();
  590. BookListRelQuery::create()->deleteAll();
  591. $book = new Book();
  592. $book->setTitle('foo');
  593. $book->setIsbn('12345');
  594. // The object is "new"
  595. $this->assertTrue($book->isNew());
  596. $bookClubList = new BookClubList();
  597. $bookClubList->setGroupLeader('Something');
  598. $books = $bookClubList->getBooks();
  599. // Add the object by reference
  600. $books[] = $book;
  601. $bookClubList->setBooks($books);
  602. $bookClubList->save();
  603. $this->assertEquals(1, BookQuery::create()->count());
  604. $this->assertEquals(1, BookListRelQuery::create()->count());
  605. $this->assertEquals(1, BookClubListQuery::create()->count());
  606. }
  607. public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExistingObject()
  608. {
  609. // Ensure no data
  610. BookQuery::create()->deleteAll();
  611. BookClubListQuery::create()->deleteAll();
  612. BookListRelQuery::create()->deleteAll();
  613. $book = new Book();
  614. $book->setTitle('foo');
  615. $book->setIsbn('124');
  616. $book->save();
  617. // The object isn't "new"
  618. $this->assertFalse($book->isNew());
  619. $bookClubList = new BookClubList();
  620. $bookClubList->setGroupLeader('Something');
  621. $books = $bookClubList->getBooks();
  622. // Add the object by reference
  623. $books[] = $book;
  624. $bookClubList->setBooks($books);
  625. $bookClubList->save();
  626. $this->assertEquals(1, BookQuery::create()->count());
  627. $this->assertEquals(1, BookListRelQuery::create()->count());
  628. $this->assertEquals(1, BookClubListQuery::create()->count());
  629. }
  630. public function testRemoveObjectFromCollection()
  631. {
  632. $list = new BookClubList();
  633. $list->setGroupLeader('Archimedes Q. Porter');
  634. $list2 = new BookClubList();
  635. $list2->setGroupLeader('FooBar group');
  636. // No save ...
  637. $book = new Book();
  638. $book->setTitle( "Jungle Expedition Handbook" );
  639. $book->setIsbn('TEST');
  640. // No save ...
  641. $this->assertCount(0, $book->getBookClubLists(), 'No BookClubList');
  642. $book->addBookClubList($list);
  643. $book->addBookClubList($list2);
  644. $this->assertCount(2, $book->getBookClubLists(), 'Two BookClubList');
  645. $book->removeBookClubList($list);
  646. $this->assertCount(1, $book->getBookClubLists(), 'One BookClubList has been remove');
  647. }
  648. public function testRemoveObjectStoredInDBFromCollection()
  649. {
  650. BookQuery::create()->deleteAll();
  651. BookClubListQuery::create()->deleteAll();
  652. $list = new BookClubList();
  653. $list->setGroupLeader('Archimedes Q. Porter');
  654. $list2 = new BookClubList();
  655. $list2->setGroupLeader('FooBar group');
  656. // No save ...
  657. $book = new Book();
  658. $book->setTitle( "Jungle Expedition Handbook" );
  659. $book->setIsbn('TEST');
  660. $book->addBookClubList($list);
  661. $book->addBookClubList($list2);
  662. $book->save();
  663. $this->assertEquals(2, BookClubListQuery::create()->count(), 'Two BookClubList');
  664. $this->assertEquals(2, BookListRelQuery::create()->count(), 'Two BookClubList');
  665. $book->removeBookClubList($list);
  666. $this->assertEquals(2, BookListRelQuery::create()->count(), 'still Two BookClubList in db before save()');
  667. $this->assertCount(1, $book->getBookClubLists(), 'One BookClubList has been remove');
  668. $book->save();
  669. $this->assertCount(1, $book->getBookClubLists(), 'One BookClubList has been remove');
  670. $this->assertEquals(1, BookListRelQuery::create()->count(), 'One BookClubList has been remove');
  671. }
  672. public function testRemoveObjectOneToMany()
  673. {
  674. BookQuery::create()->deleteAll();
  675. AuthorQuery::create()->deleteAll();
  676. $book = new Book();
  677. $book->setTitle('Propel Book');
  678. $book->setIsbn('14');
  679. $book2 = new Book();
  680. $book2->setTitle('Propel2 Book');
  681. $book2->setIsbn('124');
  682. $author = new Author();
  683. $author->setFirstName('François');
  684. $author->setLastName('Z');
  685. $author->addBook($book);
  686. $author->addBook($book2);
  687. $this->assertCount(2, $author->getBooks());
  688. $author->removeBook($book);
  689. $books = $author->getBooks();
  690. $this->assertCount(1, $books);
  691. $this->assertEquals('Propel2 Book', reset($books)->getTitle());
  692. $author->save();
  693. $book->save();
  694. $book2->save();
  695. $this->assertEquals(2, BookQuery::create()->count(), 'Two Book');
  696. $this->assertEquals(1, AuthorQuery::create()->count(), 'One Author');
  697. $this->assertEquals(1, BookQuery::create()->filterByAuthor($author)->count());
  698. $author->addBook($book);
  699. $author->save();
  700. $this->assertEquals(2, BookQuery::create()->filterByAuthor($author)->count());
  701. $author->removeBook($book2);
  702. $author->save();
  703. $this->assertEquals(1, BookQuery::create()->filterByAuthor($author)->count());
  704. $this->assertEquals(2, BookQuery::create()->count(), 'Two Book because FK is not required so book is not delete when removed from author\'s book collection');
  705. }
  706. public function testRemoveObjectOneToManyWithFkRequired()
  707. {
  708. BookSummaryQuery::create()->deleteAll();
  709. BookQuery::create()->deleteAll();
  710. $bookSummary = new BookSummary();
  711. $bookSummary->setSummary('summary Propel Book');
  712. $bookSummary2 = new BookSummary();
  713. $bookSummary2->setSummary('summary2 Propel Book');
  714. $book = new Book();
  715. $book->setTitle('Propel Book');
  716. $book->setIsbn('1235');
  717. $book->addBookSummary($bookSummary);
  718. $book->addBookSummary($bookSummary2);
  719. $this->assertCount(2, $book->getBookSummarys());
  720. $book->removeBookSummary($bookSummary);
  721. $bookSummaries = $book->getBookSummarys();
  722. $this->assertCount(1, $bookSummaries);
  723. $this->assertEquals('summary2 Propel Book', reset($bookSummaries)->getSummary());
  724. $book->save();
  725. $bookSummary2->save();
  726. $this->assertEquals(1, BookQuery::create()->count(), 'One Book');
  727. $this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Summary');
  728. $this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
  729. $book->addBookSummary($bookSummary);
  730. $bookSummary->save();
  731. $book->save();
  732. $this->assertEquals(2, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
  733. $book->removeBookSummary($bookSummary2);
  734. $book->save();
  735. $this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
  736. $this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Book summary because FK is required so book summary is deleted when book is saved');
  737. }
  738. public function testRefPhpNameCrossMany()
  739. {
  740. $book = new Book();
  741. $bookClubList = new BookClubList();
  742. $bookClubList->addFavoriteBookRelated($book);
  743. $this->assertCount(1, $bookClubList->getFavoriteBookRelateds(), 'there should be one book in the bookClubList');
  744. }
  745. public function testRefIsOnlySavedWhenRequired()
  746. {
  747. BookQuery::create()->deleteAll();
  748. $book = new Book();
  749. $book->setTitle('Propel Book');
  750. $book->setISBN('TEST');
  751. $book->save();
  752. $bookId = $book->getId();
  753. BookPeer::clearInstancePool();
  754. $summary = $this->getMock('BookSummary');
  755. $summary
  756. ->expects($this->once())
  757. ->method('isDeleted')
  758. ->will($this->returnValue(false))
  759. ;
  760. $summary
  761. ->expects($this->once())
  762. ->method('isNew')
  763. ->will($this->returnValue(false))
  764. ;
  765. $summary
  766. ->expects($this->once())
  767. ->method('isModified')
  768. ->will($this->returnValue(false))
  769. ;
  770. $summary
  771. ->expects($this->never())
  772. ->method('save')
  773. ;
  774. $coll = new PropelObjectCollection();
  775. $coll->append($summary);
  776. $book = BookQuery::create()->findOneById($bookId);
  777. // In conjunction with the mock above, this simulates loading those entries prior saving the book.
  778. $book->setBookSummarys($coll);
  779. $book->setTitle('Propel2 Book');
  780. $book->save();
  781. }
  782. public function testAddAfterRemoveKeepsReferences()
  783. {
  784. $list = new BookClubList();
  785. $list->setGroupLeader('Archimedes Q. Porter');
  786. $book = new Book();
  787. $book->setTitle( "Jungle Expedition Handbook" );
  788. $book->setIsbn('TEST');
  789. $xref = new BookListRel();
  790. $xref->setBook($book);
  791. $xref->setBookClubList($list);
  792. $xref->save();
  793. $book->removeBookListRel($xref);
  794. $book->addBookListRel($xref);
  795. $book->save();
  796. $this->assertCount(1, $list->getBookListRels());
  797. $this->assertCount(1, $book->getBookListRels());
  798. $this->assertCount(1, BookListRelPeer::doSelect(new Criteria()));
  799. $book->removeBookClubList($list);
  800. $book->addBookClubList($list);
  801. $book->save();
  802. $this->assertCount(1, $list->getBookListRels());
  803. $this->assertCount(1, $book->getBookListRels());
  804. $this->assertCount(1, BookListRelPeer::doSelect(new Criteria()));
  805. }
  806. }