This repository was archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSoftDeleteBehaviorTest.php
231 lines (189 loc) · 6.37 KB
/
SoftDeleteBehaviorTest.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
<?php
namespace yii2tech\tests\unit\ar\softdelete;
use yii\base\ModelEvent;
use yii2tech\ar\softdelete\SoftDeleteBehavior;
use yii2tech\tests\unit\ar\softdelete\data\Category;
use yii2tech\tests\unit\ar\softdelete\data\Item;
use yii2tech\tests\unit\ar\softdelete\data\VersionedItem;
class SoftDeleteBehaviorTest extends TestCase
{
public function testSoftDelete()
{
/* @var $item Item|SoftDeleteBehavior */
$item = Item::findOne(2);
$result = $item->softDelete();
$this->assertEquals(1, $result);
$this->assertEquals(true, $item->isDeleted);
}
public function testReplaceDelete()
{
/* @var $item Item|SoftDeleteBehavior */
/* @var $behavior SoftDeleteBehavior */
$item = Item::findOne(2);
$behavior = $item->getBehavior('softDelete');
$behavior->replaceRegularDelete = true;
$item->delete();
$this->assertEquals(true, $item->isDeleted);
$this->assertEquals(2, Item::find()->count());
}
/**
* @depends testSoftDelete
*/
public function testAllowDelete()
{
/* @var $item Item|SoftDeleteBehavior */
/* @var $behavior SoftDeleteBehavior */
$item = Item::findOne(1);
$behavior = $item->getBehavior('softDelete');
$behavior->replaceRegularDelete = true;
$item->name = 'allow-delete';
$item->softDelete();
$this->assertEquals(1, Item::find()->count());
}
/**
* @depends testSoftDelete
*/
public function testRestore()
{
/* @var $item Item|SoftDeleteBehavior */
$item = Item::findOne(2);
$item->softDelete();
$result = $item->restore();
$this->assertEquals(1, $result);
$this->assertEquals(false, $item->isDeleted);
}
/**
* @depends testRestore
*/
public function testCallback()
{
/* @var $item Item|SoftDeleteBehavior */
/* @var $behavior SoftDeleteBehavior */
$item = Item::findOne(1);
$behavior = $item->getBehavior('softDelete');
$behavior->softDeleteAttributeValues = [
'deletedAt' => function() {
return time();
}
];
$item->softDelete();
$this->assertTrue($item->deletedAt >= time());
/* @var $item Item|SoftDeleteBehavior */
$item = Item::findOne(1);
$behavior = $item->getBehavior('softDelete');
$behavior->restoreAttributeValues = [
'deletedAt' => function() {
return null;
}
];
$item->restore();
$this->assertNull($item->deletedAt);
}
/**
* @depends testSoftDelete
*/
public function testSafeDelete()
{
/* @var $item Item|SoftDeleteBehavior */
/* @var $behavior SoftDeleteBehavior */
// actual delete
$item = Item::findOne(1);
$result = $item->safeDelete();
$this->assertEquals(1, $result);
$this->assertNull(Item::findOne(1));
// fallback
$item = Item::findOne(2);
$item->throwOnDeleteException = true;
$result = $item->safeDelete();
$this->assertEquals(1, $result);
$item = Item::findOne(2);
$this->assertNotNull($item);
$this->assertEquals(true, $item->isDeleted);
// custom exception class
$item = Item::findOne(2);
$item->throwOnDeleteException = true;
$item->onDeleteExceptionClass = 'yii\base\InvalidValueException';
$behavior = $item->getBehavior('softDelete');
$behavior->deleteFallbackException = $item->onDeleteExceptionClass;
$item->safeDelete();
$this->assertNotNull(Item::findOne(2));
$this->assertEquals(true, $item->isDeleted);
$item->onDeleteExceptionClass = 'yii\db\IntegrityException';
try {
$item->isDeleted = false;
$item->safeDelete();
$this->assertTrue(false, 'No exception thrown');
} catch (\Exception $exception) {
$this->assertEquals('yii\db\IntegrityException', get_class($exception));
$this->assertEquals(false, $item->isDeleted);
}
}
/**
* @depends testSoftDelete
*/
public function testBeforeSoftDelete()
{
/* @var $item Item|SoftDeleteBehavior */
$item = Item::findOne(1);
$item->on(SoftDeleteBehavior::EVENT_BEFORE_SOFT_DELETE, function (ModelEvent $event) {
$item = $event->sender;
$item->deletedAt = 100;
});
$item->softDelete();
$item = Item::findOne(1);
$this->assertEquals(100, $item->deletedAt);
}
/**
* @depends testRestore
*/
public function testBeforeRestore()
{
/* @var $item Item|SoftDeleteBehavior */
$item = Item::findOne(1);
$item->softDelete();
$item = Item::findOne(1);
$item->on(SoftDeleteBehavior::EVENT_BEFORE_RESTORE, function (ModelEvent $event) {
$item = $event->sender;
$item->deletedAt = 200;
});
$item->restore();
$item = Item::findOne(1);
$this->assertEquals(200, $item->deletedAt);
}
/**
* @depends testRestore
*/
public function testOptimisticLock()
{
/* @var $item VersionedItem|SoftDeleteBehavior */
$item = new VersionedItem();
$item->name = 'optimistic lock';
$item->version = 1;
$item->save(false);
$item = VersionedItem::findOne($item->id);
$this->assertTrue($item->softDelete() > 0);
$this->assertEquals(2, $item->version);
$item = VersionedItem::findOne($item->id);
$this->assertTrue($item->restore() > 0);
$this->assertEquals(3, $item->version);
$item = VersionedItem::findOne($item->id);
$item->version = 0;
$this->expectException('yii\db\StaleObjectException');
$item->softDelete();
}
/**
* @depends testRestore
*/
public function testUseRestoreAttributeValuesAsDefaults()
{
$category = new Category();
$category->name = 'apply restore attribute';
$category->save(false);
$this->assertSame(false, $category->isDeleted);
$category = new Category();
$category->name = 'prevent restore attribute application';
$category->isDeleted = true;
$category->save(false);
$this->assertSame(true, $category->isDeleted);
}
}