Repository navigation
Expand file tree
/
Copy pathFindModelTest.php
More file actions
303 lines (246 loc) · 7.43 KB
/
Copy pathFindModelTest.php
File metadata and controls
303 lines (246 loc) · 7.43 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace CodeIgniter\Models;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Exceptions\ModelException;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\SecondaryModel;
use Tests\Support\Models\UserModel;
final class FindModelTest extends LiveModelTestCase
{
public function testFindReturnsRow(): void
{
$this->createModel(JobModel::class);
$this->assertSame('Musician', $this->model->find(4)->name);
}
public function testFindReturnsMultipleRows(): void
{
$this->createModel(JobModel::class);
$jobs = $this->model->find([1, 4]);
$this->assertSame('Developer', $jobs[0]->name);
$this->assertSame('Musician', $jobs[1]->name);
}
public function testGetColumnWithStringColumnName(): void
{
$this->createModel(JobModel::class);
$job = $this->model->findColumn('name');
$this->assertSame('Developer', $job[0]);
$this->assertSame('Politician', $job[1]);
$this->assertSame('Accountant', $job[2]);
$this->assertSame('Musician', $job[3]);
}
public function testGetColumnsWithMultipleColumnNames(): void
{
$this->expectException(DataException::class);
$this->expectExceptionMessage('Only single column allowed in Column name.');
$this->createModel(JobModel::class)->findColumn('name,description');
}
public function testFindActsAsGetWithNoParams(): void
{
$this->createModel(JobModel::class);
$jobs = $this->model->asArray()->find();
$this->assertCount(4, $jobs);
$names = array_column($jobs, 'name');
$this->assertContains('Developer', $names);
$this->assertContains('Politician', $names);
$this->assertContains('Accountant', $names);
$this->assertContains('Musician', $names);
}
public function testFindRespectsReturnArray(): void
{
$job = $this->createModel(JobModel::class)->asArray()->find(4);
$this->assertIsArray($job);
}
public function testFindRespectsReturnObject(): void
{
$job = $this->createModel(JobModel::class)->asObject()->find(4);
$this->assertIsObject($job);
}
public function testFindRespectsSoftDeletes(): void
{
$this->db->table('user')
->where('id', 4)
->update(['deleted_at' => date('Y-m-d H:i:s')]);
$this->createModel(UserModel::class);
$user = $this->model->find(4);
$this->assertEmpty($user);
$user = $this->model->withDeleted()->find(4);
$count = is_object($user) ? 1 : 0;
$this->assertSame(1, $count);
}
public function testFindClearsBinds(): void
{
$this->createModel(JobModel::class);
$this->model->find(1);
$this->model->find(1);
// Binds should be reset to 0 after each one
$binds = $this->model->builder()->getBinds();
$this->assertCount(0, $binds);
$query = $this->model->getLastQuery();
$this->assertCount(1, $this->getPrivateProperty($query, 'binds'));
}
public function testFindAllReturnsAllRecords(): void
{
$users = $this->createModel(UserModel::class)->findAll();
$this->assertCount(4, $users);
}
public function testFindAllRespectsLimits(): void
{
$users = $this->createModel(UserModel::class)->findAll(2);
$this->assertCount(2, $users);
$this->assertSame('Derek Jones', $users[0]->name);
}
public function testFindAllRespectsLimitsAndOffset(): void
{
$users = $this->createModel(UserModel::class)->findAll(2, 2);
$this->assertCount(2, $users);
$this->assertSame('Richard A Causey', $users[0]->name);
}
public function testFindAllRespectsSoftDeletes(): void
{
$this->db->table('user')
->where('id', 4)
->update(['deleted_at' => date('Y-m-d H:i:s')]);
$this->createModel(UserModel::class);
$user = $this->model->findAll();
$this->assertCount(3, $user);
$user = $this->model->withDeleted()->findAll();
$this->assertCount(4, $user);
}
public function testFirst(): void
{
$user = $this->createModel(UserModel::class)->where('id >', 2)->first();
$count = is_object($user) ? 1 : 0;
$this->assertSame(1, $count);
$this->assertSame(3, (int) $user->id);
}
/**
* @dataProvider provideGroupBy
*/
public function testFirstAggregate($groupBy, $total): void
{
$this->createModel(UserModel::class);
if ($groupBy)
{
$this->model->groupBy('id');
}
$user = $this->model->select('SUM(id) as total')->where('id >', 2)->first();
$this->assertSame($total, (int) $user->total);
}
public static function provideGroupBy(): iterable
{
return [
[true, 3],
[false, 7],
];
}
/**
* @dataProvider provideAggregateAndGroupBy
*/
public function testFirstRespectsSoftDeletes($aggregate, $groupBy): void
{
$this->db->table('user')
->where('id', 1)
->update(['deleted_at' => date('Y-m-d H:i:s')]);
$this->createModel(UserModel::class);
if ($aggregate)
{
$this->model->select('SUM(id) as id');
}
if ($groupBy)
{
$this->model->groupBy('id');
}
$user = $this->model->first();
if (! $aggregate || $groupBy)
{
$count = is_object($user) ? 1 : 0;
$this->assertSame(1, $count);
$this->assertSame(2, (int) $user->id);
}
else
{
$this->assertSame(9, (int) $user->id);
}
$user = $this->model->withDeleted()->first();
$this->assertSame(1, (int) $user->id);
}
/**
* @dataProvider provideAggregateAndGroupBy
*/
public function testFirstRecoverTempUseSoftDeletes($aggregate, $groupBy): void
{
$this->createModel(UserModel::class);
$this->model->delete(1);
if ($aggregate)
{
$this->model->select('sum(id) as id');
}
if ($groupBy)
{
$this->model->groupBy('id');
}
$user = $this->model->withDeleted()->first();
$this->assertSame(1, (int) $user->id);
$user2 = $this->model->first();
$this->assertSame(2, (int) $user2->id);
}
public static function provideAggregateAndGroupBy(): iterable
{
return [
[true, true],
[false, false],
[true, false],
[false, true],
];
}
public function testFirstWithNoPrimaryKey(): void
{
$this->createModel(SecondaryModel::class);
$this->db->table('secondary')->insert([
'key' => 'foo',
'value' => 'bar',
]);
$this->db->table('secondary')->insert([
'key' => 'bar',
'value' => 'baz',
]);
$record = $this->model->first();
$this->assertInstanceOf('stdClass', $record);
$this->assertSame('foo', $record->key);
}
public function testThrowsWithNoPrimaryKey(): void
{
$this->expectException(ModelException::class);
$this->expectExceptionMessage('`Tests\Support\Models\UserModel` model class does not specify a Primary Key.');
$this->createModel(UserModel::class);
$this->setPrivateProperty($this->model, 'primaryKey', '');
$this->model->find(1);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1881
*/
public function testSoftDeleteWithTableJoinsFindAll(): void
{
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at' => null]);
$results = $this->createModel(UserModel::class)->join('job', 'job.id = user.id')->findAll();
$this->assertCount(4, $results);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1881
*/
public function testSoftDeleteWithTableJoinsFind(): void
{
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at' => null]);
$results = $this->createModel(UserModel::class)->join('job', 'job.id = user.id')->find(1);
$this->assertSame(1, (int) $results->id);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1881
*/
public function testSoftDeleteWithTableJoinsFirst(): void
{
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at' => null]);
$results = $this->createModel(UserModel::class)->join('job', 'job.id = user.id')->first(1);
$this->assertSame(1, (int) $results->id);
}
}