Repository navigation
Expand file tree
/
Copy pathDeleteModelTest.php
More file actions
202 lines (162 loc) · 6.05 KB
/
Copy pathDeleteModelTest.php
File metadata and controls
202 lines (162 loc) · 6.05 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
<?php
namespace CodeIgniter\Models;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Exceptions\ModelException;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\StringifyPkeyModel;
use Tests\Support\Models\UserModel;
final class DeleteModelTest extends LiveModelTestCase
{
public function testDeleteBasics(): void
{
$this->createModel(JobModel::class);
$this->seeInDatabase('job', ['name' => 'Developer']);
$result = $this->model->delete(1);
$this->assertTrue($result->resultID !== false);
$this->dontSeeInDatabase('job', ['name' => 'Developer']);
}
public function testDeleteFail(): void
{
$this->setPrivateProperty($this->db, 'DBDebug', false);
$this->createModel(JobModel::class);
$this->seeInDatabase('job', ['name' => 'Developer']);
$result = $this->model->where('name123', 'Developer')->delete();
$this->assertFalse($result->resultID);
$this->seeInDatabase('job', ['name' => 'Developer']);
}
public function testDeleteStringPrimaryKey(): void
{
$this->createModel(StringifyPkeyModel::class);
$this->seeInDatabase('stringifypkey', ['value' => 'test']);
$this->model->delete('A01');
$this->dontSeeInDatabase('stringifypkey', ['value' => 'test']);
}
public function testDeleteWithSoftDeletes(): void
{
$this->createModel(UserModel::class);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$result = $this->model->delete(1);
$this->assertTrue($result);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NOT NULL' => null]);
}
public function testDeleteWithSoftDeleteFail(): void
{
$this->setPrivateProperty($this->db, 'DBDebug', false);
$this->createModel(UserModel::class);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$result = $this->model->where('name123', 'Derek Jones')->delete();
$this->assertFalse($result);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
}
public function testDeleteWithSoftDeletesPurge(): void
{
$this->createModel(UserModel::class);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$this->model->delete(1, true);
$this->dontSeeInDatabase('user', ['name' => 'Derek Jones']);
}
public function testDeleteMultiple(): void
{
$this->createModel(JobModel::class);
$this->seeInDatabase('job', ['name' => 'Developer']);
$this->seeInDatabase('job', ['name' => 'Politician']);
$this->model->delete([1, 2]);
$this->dontSeeInDatabase('job', ['name' => 'Developer']);
$this->dontSeeInDatabase('job', ['name' => 'Politician']);
$this->seeInDatabase('job', ['name' => 'Accountant']);
}
public function testDeleteNoParams(): void
{
$this->createModel(JobModel::class);
$this->seeInDatabase('job', ['name' => 'Developer']);
$this->model->where('id', 1)->delete();
$this->dontSeeInDatabase('job', ['name' => 'Developer']);
}
public function testPurgeDeleted(): void
{
$this->createModel(UserModel::class);
$this->db->table('user')
->where('id', 1)
->update(['deleted_at' => date('Y-m-d H:i:s')]);
$this->model->purgeDeleted();
$users = $this->model->withDeleted()->findAll();
$this->assertCount(3, $users);
}
public function testOnlyDeleted(): void
{
$this->createModel(UserModel::class);
$this->db->table('user')
->where('id', 1)
->update(['deleted_at' => date('Y-m-d H:i:s')]);
$users = $this->model->onlyDeleted()->findAll();
$this->assertCount(1, $users);
}
/**
* If where condition is set, beyond the value was empty (0,'', NULL, etc.),
* Exception should not be thrown because condition was explicity set
*
* @dataProvider emptyPkValues
*/
public function testDontThrowExceptionWhenSoftDeleteConditionIsSetWithEmptyValue($emptyValue): void
{
$this->createModel(UserModel::class);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$this->model->where('id', $emptyValue)->delete();
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
}
/**
* @dataProvider emptyPkValues
*/
public function testThrowExceptionWhenSoftDeleteParamIsEmptyValue($emptyValue): void
{
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Deletes are not allowed unless they contain a "where" or "like" clause.');
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$this->createModel(UserModel::class)->delete($emptyValue);
}
/**
* @dataProvider emptyPkValues
*/
public function testDontDeleteRowsWhenSoftDeleteParamIsEmpty($emptyValue): void
{
$this->expectException(DatabaseException::class);
$this->expectExceptionMessage('Deletes are not allowed unless they contain a "where" or "like" clause.');
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
$this->createModel(UserModel::class)->delete($emptyValue);
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
}
public static function emptyPkValues(): iterable
{
return [
[0],
[null],
['0'],
];
}
public function testThrowsWithNoDateFormat(): void
{
$this->expectException(ModelException::class);
$this->expectExceptionMessage('`Tests\Support\Models\UserModel` model class does not have a valid dateFormat.');
$this->createModel(UserModel::class);
$this->setPrivateProperty($this->model, 'dateFormat', '');
$this->model->delete(1);
}
public function testDeleteWithSoftDelete(): void
{
$this->createModel(JobModel::class);
$this->setPrivateProperty($this->model, 'useTimestamps', true);
$this->setPrivateProperty($this->model, 'useSoftDeletes', true);
$this->model->delete(1);
$this->seeInDatabase('job', ['id' => 1, 'deleted_at IS NOT NULL' => null]);
}
public function testPurgeDeletedWithSoftDeleteFalse(): void
{
$this->db->table('job')
->where('id', 1)
->update(['deleted_at' => time()]);
$this->createModel(JobModel::class);
$this->model->purgeDeleted();
$jobs = $this->model->findAll();
$this->assertCount(4, $jobs);
}
}