PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/propelorm/Propel
PHP | 336 lines | 267 code | 46 blank | 23 comment | 0 complexity | a783f744867e83658bb8f4c0d0d50629 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$
  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. public function testAddMultipleComments()
  167. {
  168. AggregateCommentQuery::create()->deleteAll($this->con);
  169. AggregatePostQuery::create()->deleteAll($this->con);
  170. $post1 = new AggregatePost();
  171. $comment = new AggregateComment();
  172. $comment->setAggregatePost($post1);
  173. $comment2 = new AggregateComment();
  174. $comment2->setAggregatePost($post1);
  175. $comment3 = new AggregateComment();
  176. $comment3->setAggregatePost($post1);
  177. $this->assertNull($post1->getNbComments(), 'The post start with null aggregate column');
  178. $post1->save($this->con);
  179. $this->assertEquals(3, $post1->getNbComments(), 'the post has 3 comments');
  180. }
  181. public function testQueryCountOnUpdate()
  182. {
  183. AggregateCommentQuery::create()->deleteAll($this->con);
  184. AggregatePostQuery::create()->deleteAll($this->con);
  185. $post1 = new TestablePost();
  186. $comment = new AggregateComment();
  187. $comment->setAggregatePost($post1);
  188. $comment2 = new AggregateComment();
  189. $comment2->setAggregatePost($post1);
  190. $comment3 = new AggregateComment();
  191. $comment3->setAggregatePost($post1);
  192. $post1->save($this->con);
  193. $this->assertEquals(3, $post1->getNbComments(), 'the post has 3 comments');
  194. $this->assertEquals(2, $post1->countComputeCall, 'Only two call to count nbComment');
  195. $post1->countComputeCall = 0;
  196. $comment4 = new AggregateComment();
  197. $comment4->setAggregatePost($post1);
  198. $comment4->save($this->con);
  199. $this->assertEquals(4, $post1->getNbComments(), 'the post has 4 comments');
  200. $this->assertEquals(2, $post1->countComputeCall, 'Only two call to count nbComment');
  201. $post1->countComputeCall = 0;
  202. $comment5 = new AggregateComment();
  203. $comment5->setAggregatePost($post1);
  204. $post1->save($this->con);
  205. $this->assertEquals(5, $post1->getNbComments(), 'the post has 5 comments');
  206. $this->assertEquals(2, $post1->countComputeCall, 'Only two call to count nbComment');
  207. $post1->countComputeCall = 0;
  208. $post1->save($this->con);
  209. $this->assertEquals(5, $post1->getNbComments(), 'the post has 5 comments');
  210. $this->assertEquals(1, $post1->countComputeCall, 'Only one call to count nbComment');
  211. }
  212. protected function populatePoll()
  213. {
  214. AggregateItemQuery::create()->deleteAll($this->con);
  215. AggregatePollQuery::create()->deleteAll($this->con);
  216. $poll = new AggregatePoll();
  217. $poll->save($this->con);
  218. $item1 = new AggregateItem();
  219. $item1->setScore(12);
  220. $item1->setAggregatePoll($poll);
  221. $item1->save($this->con);
  222. $item2 = new AggregateItem();
  223. $item2->setScore(7);
  224. $item2->setAggregatePoll($poll);
  225. $item2->save($this->con);
  226. return array($poll, $item1, $item2);
  227. }
  228. }
  229. class TestableComment extends AggregateComment
  230. {
  231. // overrides the parent save() to bypass behavior hooks
  232. public function save(PropelPDO $con = null)
  233. {
  234. $con->beginTransaction();
  235. try {
  236. $affectedRows = $this->doSave($con);
  237. AggregateCommentPeer::addInstanceToPool($this);
  238. $con->commit();
  239. return $affectedRows;
  240. } catch (Exception $e) {
  241. $con->rollBack();
  242. throw $e;
  243. }
  244. }
  245. // overrides the parent delete() to bypass behavior hooks
  246. public function delete(PropelPDO $con = null)
  247. {
  248. $con->beginTransaction();
  249. try {
  250. TestableAggregateCommentQuery::create()
  251. ->filterByPrimaryKey($this->getPrimaryKey())
  252. ->delete($con);
  253. $con->commit();
  254. $this->setDeleted(true);
  255. } catch (Exception $e) {
  256. $con->rollBack();
  257. throw $e;
  258. }
  259. }
  260. }
  261. class TestablePost extends AggregatePost
  262. {
  263. public $countComputeCall = 0;
  264. /**
  265. * @param PropelPDO $con
  266. * @return string
  267. */
  268. public function computeNbComments(PropelPDO $con)
  269. {
  270. $this->countComputeCall++;
  271. return parent::computeNbComments($con);
  272. }
  273. }
  274. class TestableAggregateCommentQuery extends AggregateCommentQuery
  275. {
  276. public static function create($modelAlias = null, $criteria = null)
  277. {
  278. return new TestableAggregateCommentQuery();
  279. }
  280. // overrides the parent basePreDelete() to bypass behavior hooks
  281. protected function basePreDelete(PropelPDO $con)
  282. {
  283. return $this->preDelete($con);
  284. }
  285. // overrides the parent basePostDelete() to bypass behavior hooks
  286. protected function basePostDelete($affectedRows, PropelPDO $con)
  287. {
  288. return $this->postDelete($affectedRows, $con);
  289. }
  290. }