Skip to content

Commit f3fd116

Browse files
committed
test: add tests for Model timestamp
1 parent e346a6f commit f3fd116

2 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
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+
'deleted_at',
24+
];
25+
protected $returnType = 'array';
26+
protected $useSoftDeletes = true;
27+
protected $useTimestamps = true;
28+
protected $dateFormat = 'datetime';
29+
}
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
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' => 'john@example.com',
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+
$this->assertSame('2023-11-25 12:00:00', $user['created_at']);
94+
$this->assertSame('2023-11-25 12:00:00', $user['updated_at']);
95+
}
96+
97+
public function testDoNotAllowDatesInsertArrayWithDatesSetsTimestamp(): void
98+
{
99+
Time::setTestNow('2023-11-25 12:00:00');
100+
101+
$data = [
102+
'name' => 'John Smith',
103+
'email' => 'john@example.com',
104+
'country' => 'US',
105+
'created_at' => '2000-01-01 12:00:00',
106+
'updated_at' => '2000-01-01 12:00:00',
107+
];
108+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
109+
110+
$user = $this->model->find($id);
111+
112+
$this->assertSame('2023-11-25 12:00:00', $user['created_at']);
113+
$this->assertSame('2023-11-25 12:00:00', $user['updated_at']);
114+
}
115+
116+
public function testDoNotAllowDatesUpdateArrayUpdatesUpdatedAt(): void
117+
{
118+
Time::setTestNow('2023-11-25 12:00:00');
119+
120+
$data = [
121+
'name' => 'John Smith',
122+
'email' => 'john@example.com',
123+
'country' => 'US',
124+
'created_at' => '2000-01-01 12:00:00',
125+
'updated_at' => '2000-01-01 12:00:00',
126+
];
127+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
128+
129+
$user = $this->model->find($id);
130+
131+
$user['country'] = 'CA';
132+
$this->model->update($user['id'], $user);
133+
134+
$user = $this->model->find($id);
135+
136+
$this->assertSame('2023-11-25 12:00:00', $user['created_at']);
137+
$this->assertSame('2023-11-25 12:00:00', $user['updated_at']);
138+
}
139+
140+
public function testDoNotAllowDatesUpdateEntityUpdatesUpdatedAt(): void
141+
{
142+
Time::setTestNow('2023-11-25 12:00:00');
143+
144+
$data = [
145+
'name' => 'John Smith',
146+
'email' => 'john@example.com',
147+
'country' => 'US',
148+
'created_at' => '2000-01-01 12:00:00',
149+
'updated_at' => '2000-01-01 12:00:00',
150+
];
151+
$id = $this->doNotAllowDatesPrepareOneRecord($data);
152+
$this->setPrivateProperty($this->model, 'returnType', User::class);
153+
$this->setPrivateProperty($this->model, 'tempReturnType', User::class);
154+
155+
$user = $this->model->find($id);
156+
157+
$user->country = 'CA';
158+
$this->model->update($user->id, $user);
159+
160+
$user = $this->model->find($id);
161+
162+
$this->assertSame('2023-11-25 12:00:00', (string) $user->created_at);
163+
$this->assertSame('2023-11-25 12:00:00', (string) $user->updated_at);
164+
}
165+
166+
public function testAllowDatesInsertArrayWithoutDatesSetsTimestamp(): void
167+
{
168+
Time::setTestNow('2023-11-25 12:00:00');
169+
170+
$data = [
171+
'name' => 'John Smith',
172+
'email' => 'john@example.com',
173+
'country' => 'US',
174+
// no created_at
175+
// no updated_at
176+
];
177+
$id = $this->allowDatesPrepareOneRecord($data);
178+
179+
$user = $this->model->find($id);
180+
181+
$this->assertSame('2023-11-25 12:00:00', $user['created_at']);
182+
$this->assertSame('2023-11-25 12:00:00', $user['updated_at']);
183+
}
184+
185+
public function testAllowDatesInsertArrayWithDatesSetsTimestamp(): void
186+
{
187+
Time::setTestNow('2023-11-25 12:00:00');
188+
189+
$data = [
190+
'name' => 'John Smith',
191+
'email' => 'john@example.com',
192+
'country' => 'US',
193+
'created_at' => '2000-01-01 12:00:00',
194+
'updated_at' => '2000-01-01 12:00:00',
195+
];
196+
$id = $this->allowDatesPrepareOneRecord($data);
197+
198+
$user = $this->model->find($id);
199+
200+
$this->assertSame('2000-01-01 12:00:00', $user['created_at']);
201+
$this->assertSame('2000-01-01 12:00:00', $user['updated_at']);
202+
}
203+
204+
public function testAllowDatesUpdateArrayUpdatesUpdatedAt(): void
205+
{
206+
Time::setTestNow('2023-11-25 12:00:00');
207+
208+
$data = [
209+
'name' => 'John Smith',
210+
'email' => 'john@example.com',
211+
'country' => 'US',
212+
'created_at' => '2000-01-01 12:00:00',
213+
'updated_at' => '2000-01-01 12:00:00',
214+
];
215+
$id = $this->allowDatesPrepareOneRecord($data);
216+
217+
$user = $this->model->find($id);
218+
219+
$user['country'] = 'CA';
220+
$this->model->update($user['id'], $user);
221+
222+
$user = $this->model->find($id);
223+
224+
$this->assertSame('2000-01-01 12:00:00', $user['created_at']);
225+
$this->assertSame('2000-01-01 12:00:00', $user['updated_at']);
226+
}
227+
228+
public function testAllowDatesUpdateEntityUpdatesUpdatedAt(): void
229+
{
230+
Time::setTestNow('2023-11-25 12:00:00');
231+
232+
$data = [
233+
'name' => 'John Smith',
234+
'email' => 'john@example.com',
235+
'country' => 'US',
236+
'created_at' => '2000-01-01 12:00:00',
237+
'updated_at' => '2000-01-01 12:00:00',
238+
];
239+
$id = $this->allowDatesPrepareOneRecord($data);
240+
$this->setPrivateProperty($this->model, 'returnType', User::class);
241+
$this->setPrivateProperty($this->model, 'tempReturnType', User::class);
242+
243+
$user = $this->model->find($id);
244+
245+
$user->country = 'CA';
246+
$this->model->update($user->id, $user);
247+
248+
$user = $this->model->find($id);
249+
250+
$this->assertSame('2000-01-01 12:00:00', (string) $user->created_at);
251+
// The Entity has `updated_at` value, but it will be discarded because of onlyChanged.
252+
$this->assertSame('2023-11-25 12:00:00', (string) $user->updated_at);
253+
}
254+
}

0 commit comments

Comments
 (0)