PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jbroadway/php-dbal-bench
PHP | 869 lines | 602 code | 172 blank | 95 comment | 2 complexity | 4751ad89f8a7df77a09a0b8049a28691 MD5 | raw file
Possible License(s): LGPL-2.1
  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 relaoded 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://propel.phpdb.org/trac/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 correcly adds getter for the crossRefFk');
  115. $this->assertFalse(method_exists('BookClubList', 'getBookClubLists'), 'Object generator correcly 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 testManyToManyCounterExists()
  143. {
  144. $this->assertTrue(method_exists('BookClubList', 'countBooks'), 'Object generator correcly adds counter for the crossRefFk');
  145. $this->assertFalse(method_exists('BookClubList', 'countBookClubLists'), 'Object generator correcly adds counter for the crossRefFk');
  146. }
  147. public function testManyToManyCounterNewObject()
  148. {
  149. $blc1 = new BookClubList();
  150. $nbBooks = $blc1->countBooks();
  151. $this->assertEquals(0, $nbBooks, 'countCrossRefFK() returns 0 for new objects');
  152. $query = BookQuery::create()
  153. ->filterByTitle('Harry Potter and the Order of the Phoenix');
  154. $nbBooks = $blc1->countBooks($query);
  155. $this->assertEquals(0, $nbBooks, 'countCrossRefFK() accepts a query as first parameter');
  156. }
  157. public function testManyToManyCounter()
  158. {
  159. BookstoreDataPopulator::populate();
  160. $blc1 = BookClubListQuery::create()->findOneByGroupLeader('Crazyleggs');
  161. $nbBooks = $blc1->countBooks();
  162. $this->assertEquals(2, $nbBooks, 'countCrossRefFK() returns the correct list of objects');
  163. $query = BookQuery::create()
  164. ->filterByTitle('Harry Potter and the Order of the Phoenix');
  165. $nbBooks = $blc1->countBooks($query);
  166. $this->assertEquals(1, $nbBooks, 'countCrossRefFK() accepts a query as first parameter');
  167. }
  168. public function testManyToManyAdd()
  169. {
  170. $list = new BookClubList();
  171. $list->setGroupLeader('Archimedes Q. Porter');
  172. $book = new Book();
  173. $book->setTitle( "Jungle Expedition Handbook" );
  174. $book->setISBN('TEST');
  175. $list->addBook($book);
  176. $this->assertEquals(1, $list->countBooks(), 'addCrossFk() sets the internal collection properly');
  177. $this->assertEquals(1, $list->countBookListRels(), 'addCrossFk() sets the internal cross reference collection properly');
  178. $list->save();
  179. $this->assertFalse($book->isNew(), 'related object is saved if added');
  180. $rels = $list->getBookListRels();
  181. $rel = $rels[0];
  182. $this->assertFalse($rel->isNew(), 'cross object is saved if added');
  183. $list->clearBookListRels();
  184. $list->clearBooks();
  185. $books = $list->getBooks();
  186. $expected = new PropelObjectCollection(array($book));
  187. $expected->setModel('Book');
  188. $this->assertEquals($expected, $books, 'addCrossFk() adds the object properly');
  189. $this->assertEquals(1, $list->countBookListRels());
  190. }
  191. /**
  192. * Test behavior of columns that are implicated in multiple foreign keys.
  193. * @link http://propel.phpdb.org/trac/ticket/228
  194. */
  195. public function testMultiFkImplication()
  196. {
  197. BookstoreDataPopulator::populate();
  198. // Create a new bookstore, contest, bookstore_contest, and bookstore_contest_entry
  199. $b = new Bookstore();
  200. $b->setStoreName("Foo!");
  201. $b->save();
  202. $c = new Contest();
  203. $c->setName("Bookathon Contest");
  204. $c->save();
  205. $bc = new BookstoreContest();
  206. $bc->setBookstore($b);
  207. $bc->setContest($c);
  208. $bc->save();
  209. $c = new Customer();
  210. $c->setName("Happy Customer");
  211. $c->save();
  212. $bce = new BookstoreContestEntry();
  213. $bce->setBookstore($b);
  214. $bce->setBookstoreContest($bc);
  215. $bce->setCustomer($c);
  216. $bce->save();
  217. $bce->setBookstoreId(null);
  218. $this->assertNull($bce->getBookstoreContest());
  219. $this->assertNull($bce->getBookstore());
  220. }
  221. /**
  222. * Test the clearing of related object collection.
  223. * @link http://www.propelorm.org/ticket/529
  224. */
  225. public function testClearRefFk()
  226. {
  227. BookstoreDataPopulator::populate();
  228. $book = new Book();
  229. $book->setISBN("Foo-bar-baz");
  230. $book->setTitle("The book title");
  231. // No save ...
  232. $r = new Review();
  233. $r->setReviewedBy('Me');
  234. $r->setReviewDate(new DateTime("now"));
  235. $book->addReview($r);
  236. // No save (yet) ...
  237. $this->assertEquals(1, count($book->getReviews()) );
  238. $book->clearReviews();
  239. $this->assertEquals(0, count($book->getReviews()));
  240. }
  241. /**
  242. * Test the clearing of related object collection via a many-to-many association.
  243. * @link http://www.propelorm.org/ticket/1374
  244. */
  245. public function testClearCrossFk()
  246. {
  247. $book = new Book();
  248. $bookClub = new BookClubList();
  249. $book->addBookClubList($bookClub);
  250. $this->assertEquals(1, count($book->getBookClubLists()));
  251. $book->clear();
  252. $this->assertEquals(0, count($book->getBookClubLists()));
  253. }
  254. /**
  255. * This tests to see whether modified objects are being silently overwritten by calls to fk accessor methods.
  256. * @link http://propel.phpdb.org/trac/ticket/509#comment:5
  257. */
  258. public function testModifiedObjectOverwrite()
  259. {
  260. BookstoreDataPopulator::populate();
  261. $author = new Author();
  262. $author->setFirstName("John");
  263. $author->setLastName("Public");
  264. $books = $author->getBooks(); // empty, of course
  265. $this->assertEquals(0, count($books), "Expected empty collection.");
  266. $book = new Book();
  267. $book->setTitle("A sample book");
  268. $book->setISBN("INITIAL ISBN");
  269. $author->addBook($book);
  270. $author->save();
  271. $book->setISBN("MODIFIED ISBN");
  272. $books = $author->getBooks();
  273. $this->assertEquals(1, count($books), "Expected 1 book.");
  274. $this->assertSame($book, $books[0], "Expected the same object to be returned by fk accessor.");
  275. $this->assertEquals("MODIFIED ISBN", $books[0]->getISBN(), "Expected the modified value NOT to have been overwritten.");
  276. }
  277. public function testFKGetterUseInstancePool()
  278. {
  279. BookstoreDataPopulator::populate();
  280. BookPeer::clearInstancePool();
  281. AuthorPeer::clearInstancePool();
  282. $con = Propel::getConnection(BookPeer::DATABASE_NAME);
  283. $author = AuthorPeer::doSelectOne(new Criteria(), $con);
  284. // populate book instance pool
  285. $books = $author->getBooks(null, $con);
  286. $sql = $con->getLastExecutedQuery();
  287. $author = $books[0]->getAuthor($con);
  288. $this->assertEquals($sql, $con->getLastExecutedQuery(), 'refFK getter uses instance pool if possible');
  289. }
  290. public function testRefFKGetJoin()
  291. {
  292. BookstoreDataPopulator::populate();
  293. BookPeer::clearInstancePool();
  294. AuthorPeer::clearInstancePool();
  295. PublisherPeer::clearInstancePool();
  296. $con = Propel::getConnection(BookPeer::DATABASE_NAME);
  297. $author = AuthorPeer::doSelectOne(new Criteria(), $con);
  298. // populate book instance pool
  299. $books = $author->getBooksJoinPublisher(null, $con);
  300. $sql = $con->getLastExecutedQuery();
  301. $publisher = $books[0]->getPublisher($con);
  302. $this->assertEquals($sql, $con->getLastExecutedQuery(), 'refFK getter uses instance pool if possible');
  303. }
  304. public function testRefFKAddReturnsCurrentObject()
  305. {
  306. $author = new Author();
  307. $author->setFirstName('Leo');
  308. $ret = $author->addBook(new Book());
  309. $this->assertSame($author, $ret);
  310. }
  311. public function testSetterCollection()
  312. {
  313. // Ensure no data
  314. BookQuery::create()->deleteAll();
  315. BookClubListQuery::create()->deleteAll();
  316. BookListRelQuery::create()->deleteAll();
  317. $books = new PropelObjectCollection();
  318. for ($i = 0; $i < 10; $i++) {
  319. $b = new Book();
  320. $b->setTitle('My Book ' . $i);
  321. $b->setIsbn($i);
  322. $books[] = $b;
  323. }
  324. $this->assertEquals(10, $books->count());
  325. // Basic usage
  326. $bookClubList1 = new BookClubList();
  327. $bookClubList1->setGroupLeader('BookClubList1 Leader');
  328. $bookClubList1->setBooks($books);
  329. $bookClubList1->save();
  330. $this->assertEquals(10, $bookClubList1->getBooks()->count());
  331. $this->assertEquals(1, BookClubListQuery::create()->count());
  332. $this->assertEquals(10, BookQuery::create()->count());
  333. $this->assertEquals(10, BookListRelQuery::create()->count());
  334. $i = 0;
  335. foreach ($bookClubList1->getBooks() as $book) {
  336. $this->assertEquals('My Book ' . $i, $book->getTitle());
  337. $this->assertEquals($i++, $book->getIsbn());
  338. }
  339. // Remove an element
  340. $books->shift();
  341. $this->assertEquals(9, $books->count());
  342. $bookClubList1->setBooks($books);
  343. $bookClubList1->save();
  344. $this->assertEquals(9, $bookClubList1->getBooks()->count());
  345. $this->assertEquals(1, BookClubListQuery::create()->count());
  346. $this->assertEquals(9, BookListRelQuery::create()->count());
  347. $this->assertEquals(10, BookQuery::create()->count());
  348. // Add a new object
  349. $newBook = new Book();
  350. $newBook->setTitle('My New Book');
  351. $newBook->setIsbn(1234);
  352. // Kind of new collection
  353. $books = clone $books;
  354. $books[] = $newBook;
  355. $bookClubList1->setBooks($books);
  356. $bookClubList1->save();
  357. $this->assertEquals(10, $books->count());
  358. $this->assertEquals(10, $bookClubList1->getBooks()->count());
  359. $this->assertEquals(1, BookClubListQuery::create()->count());
  360. $this->assertEquals(10, BookListRelQuery::create()->count());
  361. $this->assertEquals(11, BookQuery::create()->count());
  362. // Add a new object
  363. $newBook1 = new Book();
  364. $newBook1->setTitle('My New Book1');
  365. $newBook1->setIsbn(1256);
  366. // Existing collection - The fix around reference is tested here.
  367. // Ths `$books` collection has ever been setted to the `$bookClubList1` object.
  368. // Here we are adding a new object into the collection but, in this process, it
  369. // added the new object in the internal `collBooks` of the `$bookClubList1`
  370. // object too.
  371. // That's why the new object is not tracked and the cross object is not created,
  372. // in `addBook()` we consider the `collBooks` ever contains this new object. It's
  373. // not true but this is the "reference" process.
  374. // By saying "all new objects have to be added", we solve this issue. To know if
  375. // it's the best solution is the question.
  376. $books[] = $newBook1;
  377. $bookClubList1->setBooks($books);
  378. $bookClubList1->save();
  379. $this->assertEquals(11, $books->count());
  380. $this->assertEquals(11, $bookClubList1->getBooks()->count());
  381. $this->assertEquals(1, BookClubListQuery::create()->count());
  382. $this->assertEquals(11, BookListRelQuery::create()->count());
  383. $this->assertEquals(12, BookQuery::create()->count());
  384. // Add the same collection
  385. $books = $bookClubList1->getBooks();
  386. $bookClubList1->setBooks($books);
  387. $bookClubList1->save();
  388. $this->assertEquals(11, $books->count());
  389. $this->assertEquals(11, $bookClubList1->getBooks()->count());
  390. $this->assertEquals(1, BookClubListQuery::create()->count());
  391. $this->assertEquals(11, BookListRelQuery::create()->count());
  392. $this->assertEquals(12, BookQuery::create()->count());
  393. }
  394. public function testSetterCollectionWithNoData()
  395. {
  396. // Ensure no data
  397. BookQuery::create()->deleteAll();
  398. BookClubListQuery::create()->deleteAll();
  399. BookListRelQuery::create()->deleteAll();
  400. $books = new PropelObjectCollection();
  401. $this->assertEquals(0, $books->count());
  402. // Basic usage
  403. $bookClubList1 = new BookClubList();
  404. $bookClubList1->setGroupLeader('BookClubList1 Leader');
  405. $bookClubList1->setBooks($books);
  406. $bookClubList1->save();
  407. $this->assertEquals(0, $bookClubList1->getBooks()->count());
  408. $this->assertEquals(1, BookClubListQuery::create()->count());
  409. $this->assertEquals(0, BookQuery::create()->count());
  410. $this->assertEquals(0, BookListRelQuery::create()->count());
  411. }
  412. public function testSetterCollectionSavesForeignObjects()
  413. {
  414. // Ensure no data
  415. BookQuery::create()->deleteAll();
  416. BookClubListQuery::create()->deleteAll();
  417. BookListRelQuery::create()->deleteAll();
  418. $book = new Book();
  419. $book->setTitle('My Book');
  420. $book->save();
  421. // Modify it but don't save it
  422. $book->setTitle('My Title');
  423. $coll = new PropelObjectCollection();
  424. $coll[] = $book;
  425. BookPeer::clearInstancePool();
  426. $book = BookQuery::create()->findPk($book->getPrimaryKey());
  427. $bookClubList1 = new BookClubList();
  428. $bookClubList1->setBooks($coll);
  429. $bookClubList1->save();
  430. $this->assertEquals(1, $bookClubList1->getBooks()->count());
  431. $this->assertEquals(1, BookClubListQuery::create()->count());
  432. $this->assertEquals(1, BookQuery::create()->count());
  433. $this->assertEquals(1, BookListRelQuery::create()->count());
  434. $result = BookQuery::create()
  435. ->filterById($book->getId())
  436. ->select('Title')
  437. ->findOne();
  438. $this->assertSame('My Title', $result);
  439. }
  440. public function testSetterCollectionWithNewObjects()
  441. {
  442. // Ensure no data
  443. BookQuery::create()->deleteAll();
  444. BookClubListQuery::create()->deleteAll();
  445. BookListRelQuery::create()->deleteAll();
  446. $coll = new PropelObjectCollection();
  447. $coll->setModel('Book');
  448. $coll[] = new Book();
  449. $coll[] = new Book();
  450. $coll[] = new Book();
  451. $bookClubList = new BookClubList();
  452. $bookClubList->setBooks($coll);
  453. $bookClubList->save();
  454. $this->assertEquals(3, $coll->count());
  455. $this->assertEquals(3, count($bookClubList->getBooks()));
  456. $this->assertSame($coll, $bookClubList->getBooks());
  457. $this->assertEquals(3, BookQuery::create()->count());
  458. $this->assertEquals(1, BookClubListQuery::create()->count());
  459. $this->assertEquals(3, BookListRelQuery::create()->count());
  460. }
  461. public function testSetterCollectionWithExistingObjects()
  462. {
  463. // Ensure no data
  464. BookQuery::create()->deleteAll();
  465. BookClubListQuery::create()->deleteAll();
  466. BookListRelQuery::create()->deleteAll();
  467. for ($i = 0; $i < 3; $i++) {
  468. $b = new Book();
  469. $b->setTitle('Book ' . $i);
  470. $b->save();
  471. }
  472. BookPeer::clearInstancePool();
  473. $books = BookQuery::create()->find();
  474. $bookClubList = new BookClubList();
  475. $bookClubList->setBooks($books);
  476. $bookClubList->save();
  477. $this->assertEquals(3, count($bookClubList->getBooks()));
  478. $this->assertEquals(3, BookQuery::create()->count());
  479. $this->assertEquals(1, BookClubListQuery::create()->count());
  480. $this->assertEquals(3, BookListRelQuery::create()->count());
  481. $i = 0;
  482. foreach ($bookClubList->getBooks() as $book) {
  483. $this->assertEquals('Book ' . $i++, $book->getTitle());
  484. }
  485. }
  486. public function testSetterCollectionWithEmptyCollection()
  487. {
  488. // Ensure no data
  489. BookQuery::create()->deleteAll();
  490. BookClubListQuery::create()->deleteAll();
  491. BookListRelQuery::create()->deleteAll();
  492. $bookClubList = new BookClubList();
  493. $bookClubList->setBooks(new PropelObjectCollection());
  494. $bookClubList->save();
  495. $this->assertEquals(0, count($bookClubList->getBooks()));
  496. $this->assertEquals(0, BookQuery::create()->count());
  497. $this->assertEquals(1, BookClubListQuery::create()->count());
  498. $this->assertEquals(0, BookListRelQuery::create()->count());
  499. }
  500. public function testSetterCollectionReplacesOldObjectsByNewObjects()
  501. {
  502. // Ensure no data
  503. BookQuery::create()->deleteAll();
  504. BookClubListQuery::create()->deleteAll();
  505. BookListRelQuery::create()->deleteAll();
  506. $books = new PropelObjectCollection();
  507. foreach (array('foo', 'bar') as $title) {
  508. $b = new Book();
  509. $b->setTitle($title);
  510. $books[] = $b;
  511. }
  512. $bookClubList = new BookClubList();
  513. $bookClubList->setBooks($books);
  514. $bookClubList->save();
  515. $books = $bookClubList->getBooks();
  516. $this->assertEquals('foo', $books[0]->getTitle());
  517. $this->assertEquals('bar', $books[1]->getTitle());
  518. $books = new PropelObjectCollection();
  519. foreach (array('bam', 'bom') as $title) {
  520. $b = new Book();
  521. $b->setTitle($title);
  522. $books[] = $b;
  523. }
  524. $bookClubList->setBooks($books);
  525. $bookClubList->save();
  526. $books = $bookClubList->getBooks();
  527. $this->assertEquals('bam', $books[0]->getTitle());
  528. $this->assertEquals('bom', $books[1]->getTitle());
  529. $this->assertEquals(1, BookClubListQuery::create()->count());
  530. $this->assertEquals(2, BookListRelQuery::create()->count());
  531. // ensure we have valid "association" objects
  532. $this->assertEquals(1, BookListRelQuery::create()
  533. ->filterByBookClubList($bookClubList)
  534. ->filterByBook($books[0])
  535. ->count()
  536. );
  537. $this->assertEquals(1, BookListRelQuery::create()
  538. ->filterByBookClubList($bookClubList)
  539. ->filterByBook($books[1])
  540. ->count()
  541. );
  542. $this->assertEquals(4, BookQuery::create()->count());
  543. }
  544. public function testSetterCollectionWithManyToManyModifiedByReferenceWithANewObject()
  545. {
  546. // Ensure no data
  547. BookQuery::create()->deleteAll();
  548. BookClubListQuery::create()->deleteAll();
  549. BookListRelQuery::create()->deleteAll();
  550. $book = new Book();
  551. $book->setTitle('foo');
  552. // The object is "new"
  553. $this->assertTrue($book->isNew());
  554. $bookClubList = new BookClubList();
  555. $books = $bookClubList->getBooks();
  556. // Add the object by reference
  557. $books[] = $book;
  558. $bookClubList->setBooks($books);
  559. $bookClubList->save();
  560. $this->assertEquals(1, BookQuery::create()->count());
  561. $this->assertEquals(1, BookListRelQuery::create()->count());
  562. $this->assertEquals(1, BookClubListQuery::create()->count());
  563. }
  564. public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExistingObject()
  565. {
  566. // Ensure no data
  567. BookQuery::create()->deleteAll();
  568. BookClubListQuery::create()->deleteAll();
  569. BookListRelQuery::create()->deleteAll();
  570. $book = new Book();
  571. $book->setTitle('foo');
  572. $book->save();
  573. // The object isn't "new"
  574. $this->assertFalse($book->isNew());
  575. $bookClubList = new BookClubList();
  576. $books = $bookClubList->getBooks();
  577. // Add the object by reference
  578. $books[] = $book;
  579. $bookClubList->setBooks($books);
  580. $bookClubList->save();
  581. $this->assertEquals(1, BookQuery::create()->count());
  582. $this->assertEquals(1, BookListRelQuery::create()->count());
  583. $this->assertEquals(1, BookClubListQuery::create()->count());
  584. }
  585. public function testRemoveObjectFromCollection()
  586. {
  587. $list = new BookClubList();
  588. $list->setGroupLeader('Archimedes Q. Porter');
  589. $list2 = new BookClubList();
  590. $list2->setGroupLeader('FooBar group');
  591. // No save ...
  592. $book = new Book();
  593. $book->setTitle( "Jungle Expedition Handbook" );
  594. $book->setISBN('TEST');
  595. // No save ...
  596. $this->assertCount(0, $book->getBookClubLists(), 'No BookClubList');
  597. $book->addBookClubList($list);
  598. $book->addBookClubList($list2);
  599. $this->assertCount(2, $book->getBookClubLists(), 'Two BookClubList');
  600. $book->removeBookClubList($list);
  601. $this->assertCount(1, $book->getBookClubLists(), 'One BookClubList has been remove');
  602. }
  603. public function testRemoveObjectStoredInDBFromCollection()
  604. {
  605. BookQuery::create()->deleteAll();
  606. BookClubListQuery::create()->deleteAll();
  607. $list = new BookClubList();
  608. $list->setGroupLeader('Archimedes Q. Porter');
  609. $list2 = new BookClubList();
  610. $list2->setGroupLeader('FooBar group');
  611. // No save ...
  612. $book = new Book();
  613. $book->setTitle( "Jungle Expedition Handbook" );
  614. $book->setISBN('TEST');
  615. $book->addBookClubList($list);
  616. $book->addBookClubList($list2);
  617. $book->save();
  618. $this->assertEquals(2, BookClubListQuery::create()->count(), 'Two BookClubList');
  619. $this->assertEquals(2, BookListRelQuery::create()->count(), 'Two BookClubList');
  620. $book->removeBookClubList($list);
  621. $this->assertEquals(2, BookListRelQuery::create()->count(), 'still Two BookClubList in db before save()');
  622. $this->assertCount(1, $book->getBookClubLists(), 'One BookClubList has been remove');
  623. $book->save();
  624. $this->assertCount(1, $book->getBookClubLists(), 'One BookClubList has been remove');
  625. $this->assertEquals(1, BookListRelQuery::create()->count(), 'One BookClubList has been remove');
  626. }
  627. public function testRemoveObjectOneToMany()
  628. {
  629. BookQuery::create()->deleteAll();
  630. AuthorQuery::create()->deleteAll();
  631. $book = new Book();
  632. $book->setTitle('Propel Book');
  633. $book2 = new Book();
  634. $book2->setTitle('Propel2 Book');
  635. $author = new Author();
  636. $author->setFirstName('Franรงois');
  637. $author->setLastName('Z');
  638. $author->addBook($book);
  639. $author->addBook($book2);
  640. $this->assertCount(2, $author->getBooks());
  641. $author->removeBook($book);
  642. $books = $author->getBooks();
  643. $this->assertCount(1, $books);
  644. $this->assertEquals('Propel2 Book', reset($books)->getTitle());
  645. $author->save();
  646. $book->save();
  647. $book2->save();
  648. $this->assertEquals(2, BookQuery::create()->count(), 'Two Book');
  649. $this->assertEquals(1, AuthorQuery::create()->count(), 'One Author');
  650. $this->assertEquals(1, BookQuery::create()->filterByAuthor($author)->count());
  651. $author->addBook($book);
  652. $author->save();
  653. $this->assertEquals(2, BookQuery::create()->filterByAuthor($author)->count());
  654. $author->removeBook($book2);
  655. $author->save();
  656. $this->assertEquals(1, BookQuery::create()->filterByAuthor($author)->count());
  657. $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');
  658. }
  659. public function testRemoveObjectOneToManyWithFkRequired()
  660. {
  661. BookSummaryQuery::create()->deleteAll();
  662. BookQuery::create()->deleteAll();
  663. $bookSummary = new BookSummary();
  664. $bookSummary->setSummary('summary Propel Book');
  665. $bookSummary2 = new BookSummary();
  666. $bookSummary2->setSummary('summary2 Propel Book');
  667. $book = new Book();
  668. $book->setTitle('Propel Book');
  669. $book->addBookSummary($bookSummary);
  670. $book->addBookSummary($bookSummary2);
  671. $this->assertCount(2, $book->getBookSummarys());
  672. $book->removeBookSummary($bookSummary);
  673. $bookSummaries = $book->getBookSummarys();
  674. $this->assertCount(1, $bookSummaries);
  675. $this->assertEquals('summary2 Propel Book', reset($bookSummaries)->getSummary());
  676. $book->save();
  677. $bookSummary2->save();
  678. $this->assertEquals(1, BookQuery::create()->count(), 'One Book');
  679. $this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Summary');
  680. $this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
  681. $book->addBookSummary($bookSummary);
  682. $bookSummary->save();
  683. $book->save();
  684. $this->assertEquals(2, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
  685. $book->removeBookSummary($bookSummary2);
  686. $book->save();
  687. $this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
  688. $this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Book summary because FK is required so book summary is deleted when book is saved');
  689. }
  690. public function testRefPhpNameCrossMany()
  691. {
  692. $book = new Book();
  693. $bookClubList = new BookClubList();
  694. $bookClubList->addFavoriteBookRelated($book);
  695. $this->assertCount(1, $bookClubList->getFavoriteBookRelateds(), 'there should be one book in the bookClubList');
  696. }
  697. }