PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/mattleff/propel
PHP | 366 lines | 244 code | 62 blank | 60 comment | 0 complexity | 9c53f7e9d68ea0400296f98b52a0e84a 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 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. }