Ticket520Test.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 'tools/helpers/bookstore/BookstoreTestBase.php';
  10. /* It's only fair to admit that these tests were carefully crafted
  11. after studying the current implementation to make it look as bad as
  12. possible. I am really sorry. :-( */
  13. /**
  14. * @package misc
  15. */
  16. class Ticket520Test extends BookstoreTestBase
  17. {
  18. public function testNewObjectsAvailableWhenSaveNotCalled()
  19. {
  20. $a = new Author();
  21. $a->setFirstName("Douglas");
  22. $a->setLastName("Adams");
  23. $b1 = new Book();
  24. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  25. $a->addBook($b1);
  26. $b2 = new Book();
  27. $b2->setTitle("The Restaurant At The End Of The Universe");
  28. $a->addBook($b2);
  29. // Passing no Criteria means "use the internal collection or query the database"
  30. // in that case two objects are added, so it should return 2
  31. $books = $a->getBooks();
  32. $this->assertEquals(2, count($books));
  33. }
  34. public function testNewObjectsNotAvailableWithCriteria()
  35. {
  36. $a = new Author();
  37. $a->setFirstName("Douglas");
  38. $a->setLastName("Adams");
  39. $b1 = new Book();
  40. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  41. $a->addBook($b1);
  42. $b2 = new Book();
  43. $b2->setTitle("The Restaurant At The End Of The Universe");
  44. $a->addBook($b2);
  45. $c = new Criteria();
  46. $c->add(BookPeer::TITLE, "%Hitchhiker%", Criteria::LIKE);
  47. $guides = $a->getBooks($c);
  48. $this->assertEquals(0, count($guides), 'Passing a Criteria means "force a database query"');
  49. }
  50. public function testNewObjectsAvailableAfterCriteria()
  51. {
  52. $a = new Author();
  53. $a->setFirstName("Douglas");
  54. $a->setLastName("Adams");
  55. $b1 = new Book();
  56. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  57. $a->addBook($b1);
  58. $b2 = new Book();
  59. $b2->setTitle("The Restaurant At The End Of The Universe");
  60. $a->addBook($b2);
  61. $c = new Criteria();
  62. $c->add(BookPeer::TITLE, "%Hitchhiker%", Criteria::LIKE);
  63. $guides = $a->getBooks($c);
  64. $books = $a->getBooks();
  65. $this->assertEquals(2, count($books), 'A previous query with a Criteria does not erase the internal collection');
  66. }
  67. public function testSavedObjectsWithCriteria()
  68. {
  69. $a = new Author();
  70. $a->setFirstName("Douglas");
  71. $a->setLastName("Adams");
  72. $b1 = new Book();
  73. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  74. $a->addBook($b1);
  75. $b2 = new Book();
  76. $b2->setTitle("The Restaurant At The End Of The Universe");
  77. $a->addBook($b2);
  78. $c = new Criteria();
  79. $c->add(BookPeer::TITLE, "%Hitchhiker%", Criteria::LIKE);
  80. $guides = $a->getBooks($c);
  81. $a->save();
  82. $booksAfterSave = $a->getBooks($c);
  83. $this->assertEquals(1, count($booksAfterSave), 'A previous query with a Criteria is not cached');
  84. }
  85. public function testAddNewObjectAfterSave()
  86. {
  87. $a = new Author();
  88. $a->setFirstName("Douglas");
  89. $a->setLastName("Adams");
  90. $a->save();
  91. $b1 = new Book();
  92. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  93. $a->addBook($b1);
  94. $books = $a->getBooks();
  95. $this->assertEquals(1, count($books));
  96. $this->assertTrue($books->contains($b1));
  97. /* Now this is the initial ticket 520: If we have a saved author,
  98. add a new book but happen to call getBooks() before we call save() again,
  99. the book used to be lost. */
  100. $a->save();
  101. $this->assertFalse($b1->isNew(), 'related objects are also saved after fetching them');
  102. }
  103. public function testAddNewObjectAfterSaveWithPoisonedCache()
  104. {
  105. /* This is like testAddNewObjectAfterSave(),
  106. but this time we "poison" the author's $colBooks cache
  107. before adding the book by calling getBooks(). */
  108. $a = new Author();
  109. $a->setFirstName("Douglas");
  110. $a->setLastName("Adams");
  111. $a->save();
  112. $a->getBooks();
  113. $b1 = new Book();
  114. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  115. $a->addBook($b1);
  116. $books = $a->getBooks();
  117. $this->assertEquals(1, count($books));
  118. $this->assertTrue($books->contains($b1), 'new related objects not deleted after fetching them');
  119. }
  120. public function testCachePoisoning()
  121. {
  122. /* Like testAddNewObjectAfterSaveWithPoisonedCache, emphasizing
  123. cache poisoning. */
  124. $a = new Author();
  125. $a->setFirstName("Douglas");
  126. $a->setLastName("Adams");
  127. $a->save();
  128. $c = new Criteria();
  129. $c->add(BookPeer::TITLE, "%Restaurant%", Criteria::LIKE);
  130. $this->assertEquals(0, count($a->getBooks($c)));
  131. $b1 = new Book();
  132. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  133. $a->addBook($b1);
  134. /* Like testAddNewObjectAfterSaveWithPoisonedCache, but this time
  135. with a real criteria. */
  136. $this->assertEquals(0, count($a->getBooks($c)));
  137. $a->save();
  138. $this->assertFalse($b1->isNew());
  139. $this->assertEquals(0, count($a->getBooks($c)));
  140. }
  141. public function testDeletedBookDisappears()
  142. {
  143. $this->markTestSkipped();
  144. $a = new Author();
  145. $a->setFirstName("Douglas");
  146. $a->setLastName("Adams");
  147. $b1 = new Book();
  148. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  149. $a->addBook($b1);
  150. $b2 = new Book();
  151. $b2->setTitle("The Restaurant At The End Of The Universe");
  152. $a->addBook($b2);
  153. /* As you cannot write $a->remove($b2), you have to delete $b2
  154. directly. */
  155. /* All objects unsaved. As of revision 851, this circumvents the
  156. $colBooks cache. Anyway, fails because getBooks() never checks if
  157. a colBooks entry has been deleted. */
  158. $this->assertEquals(2, count($a->getBooks()));
  159. $b2->delete();
  160. $this->assertEquals(1, count($a->getBooks()));
  161. /* Even if we had saved everything before and the delete() had
  162. actually updated the DB, the $b2 would still be a "zombie" in
  163. $a's $colBooks field. */
  164. }
  165. public function testNewObjectsGetLostOnJoin() {
  166. /* While testNewObjectsAvailableWhenSaveNotCalled passed as of
  167. revision 851, in this case we call getBooksJoinPublisher() instead
  168. of just getBooks(). get...Join...() does not contain the check whether
  169. the current object is new, it will always consult the DB and lose the
  170. new objects entirely. Thus the test fails. (At least for Propel 1.2 ?!?) */
  171. $this->markTestSkipped();
  172. $a = new Author();
  173. $a->setFirstName("Douglas");
  174. $a->setLastName("Adams");
  175. $p = new Publisher();
  176. $p->setName('Pan Books Ltd.');
  177. $b1 = new Book();
  178. $b1->setTitle("The Hitchhikers Guide To The Galaxy");
  179. $b1->setPublisher($p); // uh... did not check that :^)
  180. $a->addBook($b1);
  181. $b2 = new Book();
  182. $b2->setTitle("The Restaurant At The End Of The Universe");
  183. $b2->setPublisher($p);
  184. $a->addBook($b2);
  185. $books = $a->getBooksJoinPublisher();
  186. $this->assertEquals(2, count($books));
  187. $this->assertContains($b1, $books);
  188. $this->assertContains($b2, $books);
  189. $a->save();
  190. $this->assertFalse($b1->isNew());
  191. $this->assertFalse($b2->isNew());
  192. }
  193. }