Skip to content

Commit

Permalink
Merge pull request #4 from AntonTyutin/operations-results
Browse files Browse the repository at this point in the history
Mixin methods return boolean results.
  • Loading branch information
cornernote committed Sep 13, 2017
2 parents efa5f6b + 6e5cd48 commit 6f6394b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -47,6 +47,11 @@ public function behaviors() {
}
```

then you can use explicitly `$model->softDelete()`, `$model->hardDelete()`
and `$model->unDelete()` (for softly deleted models). Each of these methods return
boolean result. Also `$model->softDelete()` calls indirectly from `$model->delete()`,
which always returns `false`.

In your ActiveQuery class:

```php
Expand Down
8 changes: 4 additions & 4 deletions src/SoftDeleteBehavior.php
Expand Up @@ -75,7 +75,7 @@ public function softDelete()
$attribute = $this->attribute;
$this->owner->$attribute = $this->getValue(null);
// save record
$this->owner->save(false, [$attribute]);
return $this->owner->save(false, [$attribute]);
}

/**
Expand All @@ -87,7 +87,7 @@ public function unDelete()
$attribute = $this->attribute;
$this->owner->$attribute = null;
// save record
$this->owner->save(false, [$attribute]);
return $this->owner->save(false, [$attribute]);
}

/**
Expand All @@ -99,7 +99,7 @@ public function hardDelete()
$model = $this->owner;
$this->detach();
// delete as normal
$model->delete();
return 0 !== $model->delete();
}

/**
Expand All @@ -117,4 +117,4 @@ protected function getValue($event)
}
}

}
}
37 changes: 26 additions & 11 deletions tests/unit/SoftDeleteTest.php
Expand Up @@ -10,7 +10,6 @@

use tests\models\PostA;
use tests\models\PostB;
use Yii;

/**
* SoftDeleteTest
Expand All @@ -25,7 +24,21 @@ public function testSoftDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$post->delete();
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
$this->assertNotNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNotNull($post->deleted_at);
}

/**
* Explicit Soft Delete PostA
*/
public function testExplicitSoftDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$this->assertTrue($post->softDelete(), 'Result of `softDelete()` expected to be `true`');
$this->assertNotNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNotNull($post->deleted_at);
Expand All @@ -38,11 +51,12 @@ public function testUnDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$post->delete();
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
$this->assertNotNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNotNull($post->deleted_at);
$post->unDelete();
$this->assertTrue($post->unDelete(), 'Result of `unDelete()` expected to be `true`');
$this->assertNull($post->deleted_at);
$post = PostA::findOne(2);
$this->assertNull($post->deleted_at);
Expand All @@ -55,7 +69,7 @@ public function testHardDeletePostA()
{
/** @var PostA $post */
$post = PostA::findOne(2);
$post->hardDelete();
$this->assertTrue($post->hardDelete(), 'Result of `hardDelete()` expected to be `true`');
$post = PostA::findOne(2);
$this->assertNull($post);
}
Expand All @@ -67,7 +81,8 @@ public function testSoftDeletePostB()
{
/** @var PostB $post */
$post = PostB::findOne(2);
$post->delete();
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
$this->assertNotNull($post->deleted_at);
$post = PostB::findOne(2);
$this->assertNotNull($post->deleted_at);
Expand All @@ -80,11 +95,12 @@ public function testUnDeletePostB()
{
/** @var PostB $post */
$post = PostB::findOne(2);
$post->delete();
// delete() must return `false`, because we can't prevent real deletion and return `true` at the same time
$this->assertFalse($post->delete(), 'Result of `delete()` expected to be `false`');
$this->assertNotNull($post->deleted_at);
$post = PostB::findOne(2);
$this->assertNotNull($post->deleted_at);
$post->unDelete();
$this->assertTrue($post->unDelete(), 'Result of `unDelete()` expected to be `true`');
$this->assertNull($post->deleted_at);
$post = PostB::findOne(2);
$this->assertNull($post->deleted_at);
Expand All @@ -97,9 +113,8 @@ public function testHardDeletePostB()
{
/** @var PostB $post */
$post = PostB::findOne(2);
$post->hardDelete();
$this->assertTrue($post->hardDelete(), 'Result of `hardDelete()` expected to be `true`');
$post = PostB::findOne(2);
$this->assertNull($post);
}

}
}

0 comments on commit 6f6394b

Please sign in to comment.