/
TimestampableBehaviorTest.php
247 lines (223 loc) · 10.3 KB
/
TimestampableBehaviorTest.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
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
<?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
*/
require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/config/GeneratorConfig.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/model/Behavior.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/behavior/TimestampableBehavior.php';
require_once dirname(__FILE__) . '/../../../../generator/lib/util/PropelQuickBuilder.php';
/**
* Tests for TimestampableBehavior class
*
* @author François Zaninotto
* @version $Revision$
* @package generator.behavior
*/
class TimestampableBehaviorTest extends BookstoreTestBase
{
public function testParameters()
{
$table2 = Table2Peer::getTableMap();
$this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default');
$this->assertTrue(method_exists('Table2', 'getCreatedAt'), 'Timestamplable adds a created_at column by default');
$this->assertTrue(method_exists('Table2', 'getUpdatedAt'), 'Timestamplable adds an updated_at column by default');
$table1 = Table1Peer::getTableMap();
$this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist');
$this->assertTrue(method_exists('Table1', 'getCreatedOn'), 'Timestamplable allows customization of create_column name');
$this->assertTrue(method_exists('Table1', 'getUpdatedOn'), 'Timestamplable allows customization of update_column name');
}
public function testPreSave()
{
$t1 = new Table2();
$this->assertNull($t1->getUpdatedAt());
$tsave = time();
$t1->save();
$this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable sets updated_column to time() on creation');
sleep(1);
$t1->setTitle('foo');
$tupdate = time();
$t1->save();
$this->assertEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable changes updated_column to time() on update');
}
public function testPreSaveNoChange()
{
$t1 = new Table2();
$this->assertNull($t1->getUpdatedAt());
$tsave = time();
$t1->save();
$this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable sets updated_column to time() on creation');
sleep(1);
$tupdate = time();
$t1->save();
$this->assertEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable only changes updated_column if the object was modified');
}
public function testPreSaveManuallyUpdated()
{
$t1 = new Table2();
$t1->setUpdatedAt(time() - 10);
$tsave = time();
$t1->save();
$this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable does not set updated_column to time() on creation when it is set by the user');
// tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value
// and skips the update, therefore the updated_at is not in the list of modified columns,
// and the behavior changes it to the current date... let's say it's an edge case
$t1->setUpdatedAt(time() - 15);
$tupdate = time();
$t1->save();
$this->assertNotEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable does not change updated_column to time() on update when it is set by the user');
}
public function testPreInsert()
{
$t1 = new Table2();
$this->assertNull($t1->getCreatedAt());
$tsave = time();
$t1->save();
$this->assertEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable sets created_column to time() on creation');
sleep(1);
$t1->setTitle('foo');
$tupdate = time();
$t1->save();
$this->assertEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable does not update created_column on update');
}
public function testPreInsertManuallyUpdated()
{
$t1 = new Table2();
$t1->setCreatedAt(time() - 10);
$tsave = time();
$t1->save();
$this->assertNotEquals($t1->getCreatedAt('U'), $tsave, 'Timestampable does not set created_column to time() on creation when it is set by the user');
}
public function testObjectKeepUpdateDateUnchanged()
{
$t1 = new Table2();
$t1->setUpdatedAt(time() - 10);
$tsave = time();
$t1->save();
$this->assertNotEquals($t1->getUpdatedAt('U'), $tsave);
// let's save it a second time; the updated_at should be changed
$t1->setTitle('foo');
$tsave = time();
$t1->save();
$this->assertEquals($t1->getUpdatedAt('U'), $tsave);
// now let's do this a second time
$t1 = new Table2();
$t1->setUpdatedAt(time() - 10);
$tsave = time();
$t1->save();
$this->assertNotEquals($t1->getUpdatedAt('U'), $tsave);
// let's save it a second time; the updated_at should be changed
$t1->keepUpdateDateUnchanged();
$t1->setTitle('foo');
$tsave = time();
$t1->save();
$this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'keepUpdateDateUnchanged() prevents the behavior from updating the update date');
}
protected function populateUpdatedAt()
{
Table2Query::create()->deleteAll();
$ts = new PropelObjectCollection();
$ts->setModel('Table2');
for ($i=0; $i < 10; $i++) {
$t = new Table2();
$t->setTitle('UpdatedAt' . $i);
/* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/
$t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30);
$ts[]= $t;
}
$ts->save();
}
protected function populateCreatedAt()
{
Table2Query::create()->deleteAll();
$ts = new PropelObjectCollection();
$ts->setModel('Table2');
for ($i=0; $i < 10; $i++) {
$t = new Table2();
$t->setTitle('CreatedAt' . $i);
$t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
$ts[]= $t;
}
$ts->save();
}
public function testQueryRecentlyUpdated()
{
$q = Table2Query::create()->recentlyUpdated();
$this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
$this->populateUpdatedAt();
$ts = Table2Query::create()->recentlyUpdated()->count();
$this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
$ts = Table2Query::create()->recentlyUpdated(5)->count();
$this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter');
}
public function testQueryRecentlyCreated()
{
$q = Table2Query::create()->recentlyCreated();
$this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
$this->populateCreatedAt();
$ts = Table2Query::create()->recentlyCreated()->count();
$this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
$ts = Table2Query::create()->recentlyCreated(5)->count();
$this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter');
}
public function testQueryLastUpdatedFirst()
{
$q = Table2Query::create()->lastUpdatedFirst();
$this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
$this->populateUpdatedAt();
$t = Table2Query::create()->lastUpdatedFirst()->findOne();
$this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
}
public function testQueryFirstUpdatedFirst()
{
$q = Table2Query::create()->firstUpdatedFirst();
$this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
$this->populateUpdatedAt();
$t = Table2Query::create()->firstUpdatedFirst()->findOne();
$this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
}
public function testQueryLastCreatedFirst()
{
$q = Table2Query::create()->lastCreatedFirst();
$this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
$this->populateCreatedAt();
$t = Table2Query::create()->lastCreatedFirst()->findOne();
$this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
}
public function testQueryFirstCreatedFirst()
{
$q = Table2Query::create()->firstCreatedFirst();
$this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
$this->populateCreatedAt();
$t = Table2Query::create()->firstCreatedFirst()->findOne();
$this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
}
public function testDisableUpdatedAt()
{
$schema = <<<EOF
<database name="timestampable_database">
<table name="table_without_updated_at">
<column name="id" type="INTEGER" primaryKey="true" />
<behavior name="timestampable">
<parameter name="disable_updated_at" value="true" />
</behavior>
</table>
</database>
EOF;
$builder = new PropelQuickBuilder();
$builder->setSchema($schema);
$builder->build();
$this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt'));
$this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt'));
$this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt'));
$this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt'));
$obj = new TableWithoutUpdatedAt();
$this->assertNull($obj->getCreatedAt());
$this->assertEquals(1, $obj->save());
$this->assertNotNull($obj->getCreatedAt());
}
}