Skip to content

Commit 6f6394b

Browse files
authored
Merge pull request #4 from AntonTyutin/operations-results
Mixin methods return boolean results.
2 parents efa5f6b + 6e5cd48 commit 6f6394b

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function behaviors() {
4747
}
4848
```
4949

50+
then you can use explicitly `$model->softDelete()`, `$model->hardDelete()`
51+
and `$model->unDelete()` (for softly deleted models). Each of these methods return
52+
boolean result. Also `$model->softDelete()` calls indirectly from `$model->delete()`,
53+
which always returns `false`.
54+
5055
In your ActiveQuery class:
5156

5257
```php

src/SoftDeleteBehavior.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function softDelete()
7575
$attribute = $this->attribute;
7676
$this->owner->$attribute = $this->getValue(null);
7777
// save record
78-
$this->owner->save(false, [$attribute]);
78+
return $this->owner->save(false, [$attribute]);
7979
}
8080

8181
/**
@@ -87,7 +87,7 @@ public function unDelete()
8787
$attribute = $this->attribute;
8888
$this->owner->$attribute = null;
8989
// save record
90-
$this->owner->save(false, [$attribute]);
90+
return $this->owner->save(false, [$attribute]);
9191
}
9292

9393
/**
@@ -99,7 +99,7 @@ public function hardDelete()
9999
$model = $this->owner;
100100
$this->detach();
101101
// delete as normal
102-
$model->delete();
102+
return 0 !== $model->delete();
103103
}
104104

105105
/**
@@ -117,4 +117,4 @@ protected function getValue($event)
117117
}
118118
}
119119

120-
}
120+
}

tests/unit/SoftDeleteTest.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use tests\models\PostA;
1212
use tests\models\PostB;
13-
use Yii;
1413

1514
/**
1615
* SoftDeleteTest
@@ -25,7 +24,21 @@ public function testSoftDeletePostA()
2524
{
2625
/** @var PostA $post */
2726
$post = PostA::findOne(2);
28-
$post->delete();
27+
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
28+
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
29+
$this->assertNotNull($post->deleted_at);
30+
$post = PostA::findOne(2);
31+
$this->assertNotNull($post->deleted_at);
32+
}
33+
34+
/**
35+
* Explicit Soft Delete PostA
36+
*/
37+
public function testExplicitSoftDeletePostA()
38+
{
39+
/** @var PostA $post */
40+
$post = PostA::findOne(2);
41+
$this->assertTrue($post->softDelete(), 'Result of `softDelete()` expected to be `true`');
2942
$this->assertNotNull($post->deleted_at);
3043
$post = PostA::findOne(2);
3144
$this->assertNotNull($post->deleted_at);
@@ -38,11 +51,12 @@ public function testUnDeletePostA()
3851
{
3952
/** @var PostA $post */
4053
$post = PostA::findOne(2);
41-
$post->delete();
54+
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
55+
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
4256
$this->assertNotNull($post->deleted_at);
4357
$post = PostA::findOne(2);
4458
$this->assertNotNull($post->deleted_at);
45-
$post->unDelete();
59+
$this->assertTrue($post->unDelete(), 'Result of `unDelete()` expected to be `true`');
4660
$this->assertNull($post->deleted_at);
4761
$post = PostA::findOne(2);
4862
$this->assertNull($post->deleted_at);
@@ -55,7 +69,7 @@ public function testHardDeletePostA()
5569
{
5670
/** @var PostA $post */
5771
$post = PostA::findOne(2);
58-
$post->hardDelete();
72+
$this->assertTrue($post->hardDelete(), 'Result of `hardDelete()` expected to be `true`');
5973
$post = PostA::findOne(2);
6074
$this->assertNull($post);
6175
}
@@ -67,7 +81,8 @@ public function testSoftDeletePostB()
6781
{
6882
/** @var PostB $post */
6983
$post = PostB::findOne(2);
70-
$post->delete();
84+
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
85+
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
7186
$this->assertNotNull($post->deleted_at);
7287
$post = PostB::findOne(2);
7388
$this->assertNotNull($post->deleted_at);
@@ -80,11 +95,12 @@ public function testUnDeletePostB()
8095
{
8196
/** @var PostB $post */
8297
$post = PostB::findOne(2);
83-
$post->delete();
98+
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
99+
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
84100
$this->assertNotNull($post->deleted_at);
85101
$post = PostB::findOne(2);
86102
$this->assertNotNull($post->deleted_at);
87-
$post->unDelete();
103+
$this->assertTrue($post->unDelete(), 'Result of `unDelete()` expected to be `true`');
88104
$this->assertNull($post->deleted_at);
89105
$post = PostB::findOne(2);
90106
$this->assertNull($post->deleted_at);
@@ -97,9 +113,8 @@ public function testHardDeletePostB()
97113
{
98114
/** @var PostB $post */
99115
$post = PostB::findOne(2);
100-
$post->hardDelete();
116+
$this->assertTrue($post->hardDelete(), 'Result of `hardDelete()` expected to be `true`');
101117
$post = PostB::findOne(2);
102118
$this->assertNull($post);
103119
}
104-
105-
}
120+
}

0 commit comments

Comments
 (0)