PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/test/testsuite/generator/behavior/aggregate_column/AggregateColumnBehaviorTest.php

https://github.com/mattleff/propel
PHP | 255 lines | 212 code | 24 blank | 19 comment | 0 complexity | edd8ae3b6102d4199936a194629cb978 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: SoftDeleteBehaviorTest.php 1612 2010-03-16 22:56:21Z francois $
  4. * This file is part of the Propel package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';
  11. /**
  12. * Tests for AggregateColumnBehavior class
  13. *
  14. * @author François Zaninotto
  15. * @version $Revision: 1834 $
  16. * @package generator.behavior.aggregate_column
  17. */
  18. class AggregateColumnBehaviorTest extends BookstoreTestBase
  19. {
  20. public function testParameters()
  21. {
  22. $postTable = AggregatePostPeer::getTableMap();
  23. $this->assertEquals(count($postTable->getColumns()), 2, 'AggregateColumn adds one column by default');
  24. $this->assertTrue(method_exists('AggregatePost', 'getNbComments'));
  25. }
  26. public function testCompute()
  27. {
  28. AggregateCommentQuery::create()->deleteAll($this->con);
  29. AggregatePostQuery::create()->deleteAll($this->con);
  30. $post = new AggregatePost();
  31. $post->save($this->con);
  32. $this->assertEquals(0, $post->computeNbComments($this->con), 'The compute method returns 0 for objects with no related objects');
  33. $comment1 = new AggregateComment();
  34. $comment1->setAggregatePost($post);
  35. $comment1->save($this->con);
  36. $this->assertEquals(1, $post->computeNbComments($this->con), 'The compute method computes the aggregate function on related objects');
  37. $comment2 = new AggregateComment();
  38. $comment2->setAggregatePost($post);
  39. $comment2->save($this->con);
  40. $this->assertEquals(2, $post->computeNbComments($this->con), 'The compute method computes the aggregate function on related objects');
  41. $comment1->delete($this->con);
  42. $this->assertEquals(1, $post->computeNbComments($this->con), 'The compute method computes the aggregate function on related objects');
  43. }
  44. public function testUpdate()
  45. {
  46. AggregateCommentQuery::create()->deleteAll($this->con);
  47. AggregatePostQuery::create()->deleteAll($this->con);
  48. $post = new AggregatePost();
  49. $post->save($this->con);
  50. $comment = new TestableComment();
  51. $comment->setAggregatePost($post);
  52. $comment->save($this->con);
  53. $this->assertNull($post->getNbComments());
  54. $post->updateNbComments($this->con);
  55. $this->assertEquals(1, $post->getNbComments(), 'The update method updates the aggregate column');
  56. $comment->delete($this->con);
  57. $this->assertEquals(1, $post->getNbComments());
  58. $post->updateNbComments($this->con);
  59. $this->assertEquals(0, $post->getNbComments(), 'The update method updates the aggregate column');
  60. }
  61. public function testCreateRelated()
  62. {
  63. AggregateCommentQuery::create()->deleteAll($this->con);
  64. AggregatePostQuery::create()->deleteAll($this->con);
  65. $post = new AggregatePost();
  66. $post->save($this->con);
  67. $comment1 = new AggregateComment();
  68. $comment1->save($this->con);
  69. $this->assertNull($post->getNbComments(), 'Adding a new foreign object does not update the aggregate column');
  70. $comment2 = new AggregateComment();
  71. $comment2->setAggregatePost($post);
  72. $comment2->save($this->con);
  73. $this->assertEquals(1, $post->getNbComments(), 'Adding a new related object updates the aggregate column');
  74. $comment3 = new AggregateComment();
  75. $comment3->setAggregatePost($post);
  76. $comment3->save($this->con);
  77. $this->assertEquals(2, $post->getNbComments(), 'Adding a new related object updates the aggregate column');
  78. }
  79. public function testUpdateRelated()
  80. {
  81. list($poll, $item1, $item2) = $this->populatePoll();
  82. $this->assertEquals(19, $poll->getTotalScore());
  83. $item1->setScore(10);
  84. $item1->save($this->con);
  85. $this->assertEquals(17, $poll->getTotalScore(), 'Updating a related object updates the aggregate column');
  86. }
  87. public function testDeleteRelated()
  88. {
  89. list($poll, $item1, $item2) = $this->populatePoll();
  90. $this->assertEquals(19, $poll->getTotalScore());
  91. $item1->delete($this->con);
  92. $this->assertEquals(7, $poll->getTotalScore(), 'Deleting a related object updates the aggregate column');
  93. $item2->delete($this->con);
  94. $this->assertNull($poll->getTotalScore(), 'Deleting a related object updates the aggregate column');
  95. }
  96. public function testUpdateRelatedWithQuery()
  97. {
  98. list($poll, $item1, $item2) = $this->populatePoll();
  99. $this->assertEquals(19, $poll->getTotalScore());
  100. AggregateItemQuery::create()
  101. ->update(array('Score' => 4), $this->con);
  102. $this->assertEquals(8, $poll->getTotalScore(), 'Updating related objects with a query updates the aggregate column');
  103. }
  104. public function testUpdateRelatedWithQueryUsingAlias()
  105. {
  106. list($poll, $item1, $item2) = $this->populatePoll();
  107. $this->assertEquals(19, $poll->getTotalScore());
  108. AggregateItemQuery::create()
  109. ->setModelAlias('foo', true)
  110. ->update(array('Score' => 4), $this->con);
  111. $this->assertEquals(8, $poll->getTotalScore(), 'Updating related objects with a query using alias updates the aggregate column');
  112. }
  113. public function testDeleteRelatedWithQuery()
  114. {
  115. list($poll, $item1, $item2) = $this->populatePoll();
  116. $this->assertEquals(19, $poll->getTotalScore());
  117. AggregateItemQuery::create()
  118. ->deleteAll($this->con);
  119. $this->assertNull($poll->getTotalScore(), 'Deleting related objects with a query updates the aggregate column');
  120. }
  121. public function testDeleteRelatedWithQueryUsingAlias()
  122. {
  123. list($poll, $item1, $item2) = $this->populatePoll();
  124. $this->assertEquals(19, $poll->getTotalScore());
  125. AggregateItemQuery::create()
  126. ->setModelAlias('foo', true)
  127. ->filterById($item1->getId())
  128. ->delete($this->con);
  129. $this->assertEquals(7, $poll->getTotalScore(), 'Deleting related objects with a query using alias updates the aggregate column');
  130. }
  131. public function testRemoveRelation()
  132. {
  133. AggregateCommentQuery::create()->deleteAll($this->con);
  134. AggregatePostQuery::create()->deleteAll($this->con);
  135. $post = new AggregatePost();
  136. $post->save($this->con);
  137. $comment1 = new AggregateComment();
  138. $comment1->setAggregatePost($post);
  139. $comment1->save($this->con);
  140. $comment2 = new AggregateComment();
  141. $comment2->setAggregatePost($post);
  142. $comment2->save($this->con);
  143. $this->assertEquals(2, $post->getNbComments());
  144. $comment2->setAggregatePost(null);
  145. $comment2->save($this->con);
  146. $this->assertEquals(1, $post->getNbComments(), 'Removing a relation changes the related object aggregate column');
  147. }
  148. public function testReplaceRelation()
  149. {
  150. AggregateCommentQuery::create()->deleteAll($this->con);
  151. AggregatePostQuery::create()->deleteAll($this->con);
  152. $post1 = new AggregatePost();
  153. $post1->save($this->con);
  154. $post2 = new AggregatePost();
  155. $post2->save($this->con);
  156. $comment = new AggregateComment();
  157. $comment->setAggregatePost($post1);
  158. $comment->save($this->con);
  159. $this->assertEquals(1, $post1->getNbComments());
  160. $this->assertNull($post2->getNbComments());
  161. $comment->setAggregatePost($post2);
  162. $comment->save($this->con);
  163. $this->assertEquals(0, $post1->getNbComments(), 'Replacing a relation changes the related object aggregate column');
  164. $this->assertEquals(1, $post2->getNbComments(), 'Replacing a relation changes the related object aggregate column');
  165. }
  166. protected function populatePoll()
  167. {
  168. AggregateItemQuery::create()->deleteAll($this->con);
  169. AggregatePollQuery::create()->deleteAll($this->con);
  170. $poll = new AggregatePoll();
  171. $poll->save($this->con);
  172. $item1 = new AggregateItem();
  173. $item1->setScore(12);
  174. $item1->setAggregatePoll($poll);
  175. $item1->save($this->con);
  176. $item2 = new AggregateItem();
  177. $item2->setScore(7);
  178. $item2->setAggregatePoll($poll);
  179. $item2->save($this->con);
  180. return array($poll, $item1, $item2);
  181. }
  182. }
  183. class TestableComment extends AggregateComment
  184. {
  185. // overrides the parent save() to bypass behavior hooks
  186. public function save(PropelPDO $con = null)
  187. {
  188. $con->beginTransaction();
  189. try {
  190. $affectedRows = $this->doSave($con);
  191. AggregateCommentPeer::addInstanceToPool($this);
  192. $con->commit();
  193. return $affectedRows;
  194. } catch (PropelException $e) {
  195. $con->rollBack();
  196. throw $e;
  197. }
  198. }
  199. // overrides the parent delete() to bypass behavior hooks
  200. public function delete(PropelPDO $con = null)
  201. {
  202. $con->beginTransaction();
  203. try {
  204. TestableAggregateCommentQuery::create()
  205. ->filterByPrimaryKey($this->getPrimaryKey())
  206. ->delete($con);
  207. $con->commit();
  208. $this->setDeleted(true);
  209. } catch (PropelException $e) {
  210. $con->rollBack();
  211. throw $e;
  212. }
  213. }
  214. }
  215. class TestableAggregateCommentQuery extends AggregateCommentQuery
  216. {
  217. public static function create($modelAlias = null, $criteria = null)
  218. {
  219. return new TestableAggregateCommentQuery();
  220. }
  221. // overrides the parent basePreDelete() to bypass behavior hooks
  222. protected function basePreDelete(PropelPDO $con)
  223. {
  224. return $this->preDelete($con);
  225. }
  226. // overrides the parent basePostDelete() to bypass behavior hooks
  227. protected function basePostDelete($affectedRows, PropelPDO $con)
  228. {
  229. return $this->postDelete($affectedRows, $con);
  230. }
  231. }