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

Language PHP Lines 255
MD5 Hash 6016d84516cfeeba02da6d1ce48bd8bb
Repository https://github.com/mattleff/propel.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php

/*
 *	$Id: SoftDeleteBehaviorTest.php 1612 2010-03-16 22:56:21Z francois $
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license    MIT License
 */

require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';

/**
 * Tests for AggregateColumnBehavior class
 *
 * @author		 Franรงois Zaninotto
 * @version		$Revision: 1834 $
 * @package		generator.behavior.aggregate_column
 */
class AggregateColumnBehaviorTest extends BookstoreTestBase 
{
	
	public function testParameters()
	{
		$postTable = AggregatePostPeer::getTableMap();
		$this->assertEquals(count($postTable->getColumns()), 2, 'AggregateColumn adds one column by default');
		$this->assertTrue(method_exists('AggregatePost', 'getNbComments'));
	}
	
	public function testCompute()
	{
		AggregateCommentQuery::create()->deleteAll($this->con);
		AggregatePostQuery::create()->deleteAll($this->con);
		$post = new AggregatePost();
		$post->save($this->con);
		$this->assertEquals(0, $post->computeNbComments($this->con), 'The compute method returns 0 for objects with no related objects');
		$comment1 = new AggregateComment();
		$comment1->setAggregatePost($post);
		$comment1->save($this->con);
		$this->assertEquals(1, $post->computeNbComments($this->con), 'The compute method computes the aggregate function on related objects');
		$comment2 = new AggregateComment();
		$comment2->setAggregatePost($post);
		$comment2->save($this->con);
		$this->assertEquals(2, $post->computeNbComments($this->con), 'The compute method computes the aggregate function on related objects');
		$comment1->delete($this->con);
		$this->assertEquals(1, $post->computeNbComments($this->con), 'The compute method computes the aggregate function on related objects');
	}

	public function testUpdate()
	{
		AggregateCommentQuery::create()->deleteAll($this->con);
		AggregatePostQuery::create()->deleteAll($this->con);
		$post = new AggregatePost();
		$post->save($this->con);
		$comment = new TestableComment();
		$comment->setAggregatePost($post);
		$comment->save($this->con);
		$this->assertNull($post->getNbComments());
		$post->updateNbComments($this->con);
		$this->assertEquals(1, $post->getNbComments(), 'The update method updates the aggregate column');
		$comment->delete($this->con);
		$this->assertEquals(1, $post->getNbComments());
		$post->updateNbComments($this->con);
		$this->assertEquals(0, $post->getNbComments(), 'The update method updates the aggregate column');
	}
	
	public function testCreateRelated()
	{
		AggregateCommentQuery::create()->deleteAll($this->con);
		AggregatePostQuery::create()->deleteAll($this->con);
		$post = new AggregatePost();
		$post->save($this->con);
		$comment1 = new AggregateComment();
		$comment1->save($this->con);
		$this->assertNull($post->getNbComments(), 'Adding a new foreign object does not update the aggregate column');
		$comment2 = new AggregateComment();
		$comment2->setAggregatePost($post);
		$comment2->save($this->con);
		$this->assertEquals(1, $post->getNbComments(), 'Adding a new related object updates the aggregate column');
		$comment3 = new AggregateComment();
		$comment3->setAggregatePost($post);
		$comment3->save($this->con);
		$this->assertEquals(2, $post->getNbComments(), 'Adding a new related object updates the aggregate column');
	}
	
	public function testUpdateRelated()
	{
		list($poll, $item1, $item2) = $this->populatePoll();
		$this->assertEquals(19, $poll->getTotalScore());
		$item1->setScore(10);
		$item1->save($this->con);
		$this->assertEquals(17, $poll->getTotalScore(), 'Updating a related object updates the aggregate column');
	}

	public function testDeleteRelated()
	{
		list($poll, $item1, $item2) = $this->populatePoll();
		$this->assertEquals(19, $poll->getTotalScore());
		$item1->delete($this->con);
		$this->assertEquals(7, $poll->getTotalScore(), 'Deleting a related object updates the aggregate column');
		$item2->delete($this->con);
		$this->assertNull($poll->getTotalScore(), 'Deleting a related object updates the aggregate column');
	}
	
	public function testUpdateRelatedWithQuery()
	{
		list($poll, $item1, $item2) = $this->populatePoll();
		$this->assertEquals(19, $poll->getTotalScore());
		AggregateItemQuery::create()
			->update(array('Score' => 4), $this->con);
		$this->assertEquals(8, $poll->getTotalScore(), 'Updating related objects with a query updates the aggregate column');
	}

	public function testUpdateRelatedWithQueryUsingAlias()
	{
		list($poll, $item1, $item2) = $this->populatePoll();
		$this->assertEquals(19, $poll->getTotalScore());
		AggregateItemQuery::create()
			->setModelAlias('foo', true)
			->update(array('Score' => 4), $this->con);
		$this->assertEquals(8, $poll->getTotalScore(), 'Updating related objects with a query using alias updates the aggregate column');
	}

	public function testDeleteRelatedWithQuery()
	{
		list($poll, $item1, $item2) = $this->populatePoll();
		$this->assertEquals(19, $poll->getTotalScore());
		AggregateItemQuery::create()
			->deleteAll($this->con);
		$this->assertNull($poll->getTotalScore(), 'Deleting related objects with a query updates the aggregate column');
	}

	public function testDeleteRelatedWithQueryUsingAlias()
	{
		list($poll, $item1, $item2) = $this->populatePoll();
		$this->assertEquals(19, $poll->getTotalScore());
		AggregateItemQuery::create()
			->setModelAlias('foo', true)
			->filterById($item1->getId())
			->delete($this->con);
		$this->assertEquals(7, $poll->getTotalScore(), 'Deleting related objects with a query using alias updates the aggregate column');
	}
	
	public function testRemoveRelation()
	{
		AggregateCommentQuery::create()->deleteAll($this->con);
		AggregatePostQuery::create()->deleteAll($this->con);
		$post = new AggregatePost();
		$post->save($this->con);
		$comment1 = new AggregateComment();
		$comment1->setAggregatePost($post);
		$comment1->save($this->con);
		$comment2 = new AggregateComment();
		$comment2->setAggregatePost($post);
		$comment2->save($this->con);
		$this->assertEquals(2, $post->getNbComments());
		$comment2->setAggregatePost(null);
		$comment2->save($this->con);
		$this->assertEquals(1, $post->getNbComments(), 'Removing a relation changes the related object aggregate column');
	}
	
	public function testReplaceRelation()
	{
		AggregateCommentQuery::create()->deleteAll($this->con);
		AggregatePostQuery::create()->deleteAll($this->con);
		$post1 = new AggregatePost();
		$post1->save($this->con);
		$post2 = new AggregatePost();
		$post2->save($this->con);
		$comment = new AggregateComment();
		$comment->setAggregatePost($post1);
		$comment->save($this->con);
		$this->assertEquals(1, $post1->getNbComments());
		$this->assertNull($post2->getNbComments());
		$comment->setAggregatePost($post2);
		$comment->save($this->con);
		$this->assertEquals(0, $post1->getNbComments(), 'Replacing a relation changes the related object aggregate column');
		$this->assertEquals(1, $post2->getNbComments(), 'Replacing a relation changes the related object aggregate column');
	}
	
	protected function populatePoll()
	{
		AggregateItemQuery::create()->deleteAll($this->con);
		AggregatePollQuery::create()->deleteAll($this->con);
		$poll = new AggregatePoll();
		$poll->save($this->con);
		$item1 = new AggregateItem();
		$item1->setScore(12);
		$item1->setAggregatePoll($poll);
		$item1->save($this->con);
		$item2 = new AggregateItem();
		$item2->setScore(7);
		$item2->setAggregatePoll($poll);
		$item2->save($this->con);
		return array($poll, $item1, $item2);
	}

}

class TestableComment extends AggregateComment
{
	// overrides the parent save() to bypass behavior hooks
	public function save(PropelPDO $con = null)
	{
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			AggregateCommentPeer::addInstanceToPool($this);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}
	
	// overrides the parent delete() to bypass behavior hooks
	public function delete(PropelPDO $con = null)
	{
		$con->beginTransaction();
		try {
			TestableAggregateCommentQuery::create()
				->filterByPrimaryKey($this->getPrimaryKey())
				->delete($con);
			$con->commit();
			$this->setDeleted(true);
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}
	
}

class TestableAggregateCommentQuery extends AggregateCommentQuery
{
	public static function create($modelAlias = null, $criteria = null)
	{
		return new TestableAggregateCommentQuery();
	}
	
	// overrides the parent basePreDelete() to bypass behavior hooks
	protected function basePreDelete(PropelPDO $con)
	{
		return $this->preDelete($con);
	}

	// overrides the parent basePostDelete() to bypass behavior hooks
	protected function basePostDelete($affectedRows, PropelPDO $con)
	{
		return $this->postDelete($affectedRows, $con);
	}
	
}
Back to Top