/
AggregateColumnBehaviorTest.php
220 lines (200 loc) · 8.99 KB
/
AggregateColumnBehaviorTest.php
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
<?php
/**
* 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
*/
namespace Propel\Tests\Generator\Behavior\AggregateColumn;
use Propel\Runtime\Propel;
use Propel\Tests\Bookstore\Behavior\AggregateColumn;
use Propel\Tests\Bookstore\Behavior\AggregateComment;
use Propel\Tests\Bookstore\Behavior\AggregateCommentQuery;
use Propel\Tests\Bookstore\Behavior\AggregatePost;
use Propel\Tests\Bookstore\Behavior\AggregatePostQuery;
use Propel\Tests\Bookstore\Behavior\Map\AggregatePostTableMap;
use Propel\Tests\Bookstore\Behavior\AggregateItem;
use Propel\Tests\Bookstore\Behavior\AggregateItemQuery;
use Propel\Tests\Bookstore\Behavior\AggregatePoll;
use Propel\Tests\Bookstore\Behavior\AggregatePollQuery;
use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
/**
* Tests for AggregateColumnBehavior class
*
* @author François Zaninotto
*
* @group database
*/
class AggregateColumnBehaviorTest extends BookstoreTestBase
{
protected function setUp()
{
parent::setUp();
include_once(__DIR__.'/AggregateColumnsBehaviorTestClasses.php');
}
public function testParameters()
{
$postTable = AggregatePostTableMap::getTableMap();
$this->assertEquals(count($postTable->getColumns()), 2, 'AggregateColumn adds one column by default');
$this->assertTrue(method_exists('Propel\Tests\Bookstore\Behavior\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());
if (!$this->runningOnSQLite()) {
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);
}
}