Skip to content

Commit 99c1a4b

Browse files
authored
Merge pull request #8249 from kenjis/test-add-tests-model-timestamp
test: add tests for Model timestamp
2 parents 14b4350 + b09f85e commit 99c1a4b

File tree

2 files changed

+322
-0
lines changed

2 files changed

+322
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Models;
13+
14+
use CodeIgniter\Model;
15+
16+
class UserTimestampModel extends Model
17+
{
18+
protected $table = 'user';
19+
protected $allowedFields = [
20+
'name',
21+
'email',
22+
'country',
23+
];
24+
protected $returnType = 'array';
25+
protected $useSoftDeletes = true;
26+
protected $useTimestamps = true;
27+
protected $dateFormat = 'datetime';
28+
}
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Models;
13+
14+
use CodeIgniter\I18n\Time;
15+
use Tests\Support\Entity\User;
16+
use Tests\Support\Models\UserTimestampModel;
17+
18+
/**
19+
* @group DatabaseLive
20+
*
21+
* @internal
22+
*/
23+
final class TimestampModelTest extends LiveModelTestCase
24+
{
25+
protected $migrate = true;
26+
protected $migrateOnce = true;
27+
protected $refresh = false;
28+
protected $seed = '';
29+
30+
protected function tearDown(): void
31+
{
32+
parent::tearDown();
33+
34+
// Reset current time.
35+
Time::setTestNow();
36+
}
37+
38+
/**
39+
* @return int|string Insert ID
40+
*/
41+
private function allowDatesPrepareOneRecord(array $data)
42+
{
43+
$this->createModel(UserTimestampModel::class);
44+
$this->db->table('user')->truncate();
45+
46+
$this->model->setAllowedFields([
47+
'name',
48+
'email',
49+
'country',
50+
'created_at',
51+
'updated_at',
52+
'deleted_at',
53+
]);
54+
55+
return $this->model->insert($data, true);
56+
}
57+
58+
/**
59+
* @return int|string Insert ID
60+
*/
61+
private function doNotAllowDatesPrepareOneRecord(array $data)
62+
{
63+
$this->createModel(UserTimestampModel::class);
64+
$this->db->table('user')->truncate();
65+
66+
$this->model->setAllowedFields([
67+
'name',
68+
'email',
69+
'country',
70+
// no 'created_at',
71+
// no 'updated_at',
72+
'deleted_at',
73+
]);
74+
75+
return $this->model->insert($data, true);
76+
}
77+
78+
public function testDoNotAllowDatesInsertArrayWithoutDatesSetsTimestamp(): void
79+
{
80+
Time::setTestNow('2023-11-25 12:00:00');
81+
82+
$data = [
83+
'name' => 'John Smith',
84+
'email' => '[email protected]',
85+
'country' => 'US',
86+
// no created_at
87+
// no updated_at
88+
];
89+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
90+
91+
$user = $this->model->find($id);
92+
93+
$expected = '2023-11-25 12:00:00';
94+
if ($this->db->DBDriver === 'SQLSRV') {
95+
$expected .= '.000';
96+
}
97+
$this->assertSame($expected, $user['created_at']);
98+
$this->assertSame($expected, $user['updated_at']);
99+
}
100+
101+
public function testDoNotAllowDatesInsertArrayWithDatesSetsTimestamp(): void
102+
{
103+
Time::setTestNow('2023-11-25 12:00:00');
104+
105+
$data = [
106+
'name' => 'John Smith',
107+
'email' => '[email protected]',
108+
'country' => 'US',
109+
'created_at' => '2000-01-01 12:00:00',
110+
'updated_at' => '2000-01-01 12:00:00',
111+
];
112+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
113+
114+
$user = $this->model->find($id);
115+
116+
$expected = '2023-11-25 12:00:00';
117+
if ($this->db->DBDriver === 'SQLSRV') {
118+
$expected .= '.000';
119+
}
120+
$this->assertSame($expected, $user['created_at']);
121+
$this->assertSame($expected, $user['updated_at']);
122+
}
123+
124+
public function testDoNotAllowDatesUpdateArrayUpdatesUpdatedAt(): void
125+
{
126+
Time::setTestNow('2023-11-25 12:00:00');
127+
128+
$data = [
129+
'name' => 'John Smith',
130+
'email' => '[email protected]',
131+
'country' => 'US',
132+
'created_at' => '2000-01-01 12:00:00',
133+
'updated_at' => '2000-01-01 12:00:00',
134+
];
135+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
136+
137+
$user = $this->model->find($id);
138+
139+
$user['country'] = 'CA';
140+
$this->model->update($user['id'], $user);
141+
142+
$user = $this->model->find($id);
143+
144+
$expected = '2023-11-25 12:00:00';
145+
if ($this->db->DBDriver === 'SQLSRV') {
146+
$expected .= '.000';
147+
}
148+
$this->assertSame($expected, $user['created_at']);
149+
$this->assertSame($expected, $user['updated_at']);
150+
}
151+
152+
public function testDoNotAllowDatesUpdateEntityUpdatesUpdatedAt(): void
153+
{
154+
Time::setTestNow('2023-11-25 12:00:00');
155+
156+
$data = [
157+
'name' => 'John Smith',
158+
'email' => '[email protected]',
159+
'country' => 'US',
160+
'created_at' => '2000-01-01 12:00:00',
161+
'updated_at' => '2000-01-01 12:00:00',
162+
];
163+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
164+
$this->setPrivateProperty($this->model, 'returnType', User::class);
165+
$this->setPrivateProperty($this->model, 'tempReturnType', User::class);
166+
167+
$user = $this->model->find($id);
168+
169+
$user->country = 'CA';
170+
$this->model->update($user->id, $user);
171+
172+
$user = $this->model->find($id);
173+
174+
$this->assertSame('2023-11-25 12:00:00', (string) $user->created_at);
175+
$this->assertSame('2023-11-25 12:00:00', (string) $user->updated_at);
176+
}
177+
178+
/**
179+
* We do not recommend to add timestamp fields to $allowedFields.
180+
* If you want to add old data to these fields, use Query Builder.
181+
*/
182+
public function testAllowDatesInsertArrayWithoutDatesSetsTimestamp(): void
183+
{
184+
Time::setTestNow('2023-11-25 12:00:00');
185+
186+
$data = [
187+
'name' => 'John Smith',
188+
'email' => '[email protected]',
189+
'country' => 'US',
190+
// no created_at
191+
// no updated_at
192+
];
193+
$id = $this->allowDatesPrepareOneRecord($data);
194+
195+
$user = $this->model->find($id);
196+
197+
$expected = '2023-11-25 12:00:00';
198+
if ($this->db->DBDriver === 'SQLSRV') {
199+
$expected .= '.000';
200+
}
201+
$this->assertSame($expected, $user['created_at']);
202+
$this->assertSame($expected, $user['updated_at']);
203+
}
204+
205+
/**
206+
* We do not recommend to add timestamp fields to $allowedFields.
207+
* If you want to add old data to these fields, use Query Builder.
208+
*/
209+
public function testAllowDatesInsertArrayWithDatesSetsTimestamp(): void
210+
{
211+
Time::setTestNow('2023-11-25 12:00:00');
212+
213+
$data = [
214+
'name' => 'John Smith',
215+
'email' => '[email protected]',
216+
'country' => 'US',
217+
'created_at' => '2000-01-01 12:00:00',
218+
'updated_at' => '2000-01-01 12:00:00',
219+
];
220+
$id = $this->allowDatesPrepareOneRecord($data);
221+
222+
$user = $this->model->find($id);
223+
224+
$expected = '2000-01-01 12:00:00';
225+
if ($this->db->DBDriver === 'SQLSRV') {
226+
$expected .= '.000';
227+
}
228+
$this->assertSame($expected, $user['created_at']);
229+
$this->assertSame($expected, $user['updated_at']);
230+
}
231+
232+
/**
233+
* We do not recommend to add timestamp fields to $allowedFields.
234+
* If you want to add old data to these fields, use Query Builder.
235+
*/
236+
public function testAllowDatesUpdateArrayUpdatesUpdatedAt(): void
237+
{
238+
Time::setTestNow('2023-11-25 12:00:00');
239+
240+
$data = [
241+
'name' => 'John Smith',
242+
'email' => '[email protected]',
243+
'country' => 'US',
244+
'created_at' => '2000-01-01 12:00:00',
245+
'updated_at' => '2000-01-01 12:00:00',
246+
];
247+
$id = $this->allowDatesPrepareOneRecord($data);
248+
249+
$user = $this->model->find($id);
250+
251+
$user['country'] = 'CA';
252+
$this->model->update($user['id'], $user);
253+
254+
$user = $this->model->find($id);
255+
256+
$expected = '2000-01-01 12:00:00';
257+
if ($this->db->DBDriver === 'SQLSRV') {
258+
$expected .= '.000';
259+
}
260+
$this->assertSame($expected, $user['created_at']);
261+
$this->assertSame($expected, $user['updated_at']);
262+
}
263+
264+
/**
265+
* We do not recommend to add timestamp fields to $allowedFields.
266+
* If you want to add old data to these fields, use Query Builder.
267+
*/
268+
public function testAllowDatesUpdateEntityUpdatesUpdatedAt(): void
269+
{
270+
Time::setTestNow('2023-11-25 12:00:00');
271+
272+
$data = [
273+
'name' => 'John Smith',
274+
'email' => '[email protected]',
275+
'country' => 'US',
276+
'created_at' => '2000-01-01 12:00:00',
277+
'updated_at' => '2000-01-01 12:00:00',
278+
];
279+
$id = $this->allowDatesPrepareOneRecord($data);
280+
$this->setPrivateProperty($this->model, 'returnType', User::class);
281+
$this->setPrivateProperty($this->model, 'tempReturnType', User::class);
282+
283+
$user = $this->model->find($id);
284+
285+
$user->country = 'CA';
286+
$this->model->update($user->id, $user);
287+
288+
$user = $this->model->find($id);
289+
290+
$this->assertSame('2000-01-01 12:00:00', (string) $user->created_at);
291+
// The Entity has `updated_at` value, but it will be discarded because of onlyChanged.
292+
$this->assertSame('2023-11-25 12:00:00', (string) $user->updated_at);
293+
}
294+
}

0 commit comments

Comments
 (0)