From 7da6a952eee83098ba62b41377a42ddc168082df Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 2 Oct 2020 18:14:43 +0300 Subject: [PATCH] Fix tests --- .../accounts/models/DeleteAccountFormTest.php | 9 ++++++- .../models/RestoreAccountFormTest.php | 9 ++++++- common/models/Account.php | 4 ++- common/tests/unit/models/AccountTest.php | 25 ++++++++++++++++--- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/api/tests/unit/modules/accounts/models/DeleteAccountFormTest.php b/api/tests/unit/modules/accounts/models/DeleteAccountFormTest.php index 90313f0..0d80dbe 100644 --- a/api/tests/unit/modules/accounts/models/DeleteAccountFormTest.php +++ b/api/tests/unit/modules/accounts/models/DeleteAccountFormTest.php @@ -5,7 +5,9 @@ namespace api\tests\unit\modules\accounts\models; use api\modules\accounts\models\DeleteAccountForm; use api\tests\unit\TestCase; +use Codeception\Util\ReflectionHelper; use common\models\Account; +use common\notifications\AccountEditNotification; use common\tasks\CreateWebHooksDeliveries; use common\tasks\DeleteAccount; use common\tests\fixtures\AccountFixture; @@ -46,7 +48,12 @@ class DeleteAccountFormTest extends TestCase { ->method('push') ->withConsecutive( [$this->callback(function(CreateWebHooksDeliveries $task) use ($account): bool { - $this->assertSame($account->id, $task->payloads['id']); + /** @var AccountEditNotification $notification */ + $notification = ReflectionHelper::readPrivateProperty($task, 'notification'); + $this->assertInstanceOf(AccountEditNotification::class, $notification); + $this->assertSame($account->id, $notification->getPayloads()['id']); + $this->assertTrue($notification->getPayloads()['isDeleted']); + return true; })], [$this->callback(function(DeleteAccount $task) use ($account): bool { diff --git a/api/tests/unit/modules/accounts/models/RestoreAccountFormTest.php b/api/tests/unit/modules/accounts/models/RestoreAccountFormTest.php index bd30b51..1f3b03b 100644 --- a/api/tests/unit/modules/accounts/models/RestoreAccountFormTest.php +++ b/api/tests/unit/modules/accounts/models/RestoreAccountFormTest.php @@ -5,7 +5,9 @@ namespace api\tests\unit\modules\accounts\models; use api\modules\accounts\models\RestoreAccountForm; use api\tests\unit\TestCase; +use Codeception\Util\ReflectionHelper; use common\models\Account; +use common\notifications\AccountEditNotification; use common\tasks\CreateWebHooksDeliveries; use common\tests\fixtures\AccountFixture; use Yii; @@ -39,7 +41,12 @@ class RestoreAccountFormTest extends TestCase { ->method('push') ->withConsecutive( [$this->callback(function(CreateWebHooksDeliveries $task) use ($account): bool { - $this->assertSame($account->id, $task->payloads['id']); + /** @var AccountEditNotification $notification */ + $notification = ReflectionHelper::readPrivateProperty($task, 'notification'); + $this->assertInstanceOf(AccountEditNotification::class, $notification); + $this->assertSame($account->id, $notification->getPayloads()['id']); + $this->assertFalse($notification->getPayloads()['isDeleted']); + return true; })], ); diff --git a/common/models/Account.php b/common/models/Account.php index d5ad0f8..31ab1e6 100644 --- a/common/models/Account.php +++ b/common/models/Account.php @@ -189,7 +189,9 @@ class Account extends ActiveRecord { public function afterDelete(): void { parent::afterDelete(); - Yii::$app->queue->push(new CreateWebHooksDeliveries(new AccountDeletedNotification($this))); + if ($this->status !== self::STATUS_REGISTERED) { + Yii::$app->queue->push(new CreateWebHooksDeliveries(new AccountDeletedNotification($this))); + } } } diff --git a/common/tests/unit/models/AccountTest.php b/common/tests/unit/models/AccountTest.php index 6bef817..87bc15f 100644 --- a/common/tests/unit/models/AccountTest.php +++ b/common/tests/unit/models/AccountTest.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace common\tests\unit\models; +use Codeception\Util\ReflectionHelper; use common\models\Account; +use common\notifications\AccountDeletedNotification; +use common\notifications\AccountEditNotification; use common\tasks\CreateWebHooksDeliveries; use common\tests\fixtures\MojangUsernameFixture; use common\tests\unit\TestCase; @@ -129,23 +132,37 @@ class AccountTest extends TestCase { ]; $account = new Account(); + $account->id = 123; $account->afterSave(false, $changedAttributes); /** @var CreateWebHooksDeliveries $job */ $job = $this->tester->grabLastQueuedJob(); $this->assertInstanceOf(CreateWebHooksDeliveries::class, $job); - $this->assertSame('account.edit', $job->type); - $this->assertSame($changedAttributes, $job->payloads['changedAttributes']); + /** @var AccountEditNotification $notification */ + $notification = ReflectionHelper::readPrivateProperty($job, 'notification'); + $this->assertInstanceOf(AccountEditNotification::class, $notification); + $this->assertSame(123, $notification->getPayloads()['id']); + $this->assertSame($changedAttributes, $notification->getPayloads()['changedAttributes']); } public function testAfterDeletePushEvent() { $account = new Account(); $account->id = 1; + $account->status = Account::STATUS_REGISTERED; + $account->created_at = time() - 60 * 60 * 24; + $account->deleted_at = time(); + + $account->afterDelete(); + $this->assertNull($this->tester->grabLastQueuedJob()); + + $account->status = Account::STATUS_ACTIVE; $account->afterDelete(); /** @var CreateWebHooksDeliveries $job */ $job = $this->tester->grabLastQueuedJob(); $this->assertInstanceOf(CreateWebHooksDeliveries::class, $job); - $this->assertSame('account.deletion', $job->type); - $this->assertSame(1, $job->payloads['id']); + /** @var AccountDeletedNotification $notification */ + $notification = ReflectionHelper::readPrivateProperty($job, 'notification'); + $this->assertInstanceOf(AccountDeletedNotification::class, $notification); + $this->assertSame(1, $notification->getPayloads()['id']); } }