diff --git a/api/modules/internal/models/BlockForm.php b/api/modules/internal/models/BlockForm.php index a60b757..7cb0bdb 100644 --- a/api/modules/internal/models/BlockForm.php +++ b/api/modules/internal/models/BlockForm.php @@ -7,6 +7,7 @@ use common\models\Account; use common\models\amqp\AccountBanned; use PhpAmqpLib\Message\AMQPMessage; use Yii; +use yii\base\ErrorException; class BlockForm extends ApiForm { @@ -27,7 +28,7 @@ class BlockForm extends ApiForm { * * @var string */ - public $message; + public $message = ''; /** * @var Account @@ -50,7 +51,9 @@ class BlockForm extends ApiForm { $account = $this->account; $account->status = Account::STATUS_BANNED; - $account->save(); + if (!$account->save()) { + throw new ErrorException('Cannot ban account'); + } $this->createTask(); diff --git a/tests/codeception/api/unit/modules/internal/models/BlockFormTest.php b/tests/codeception/api/unit/modules/internal/models/BlockFormTest.php new file mode 100644 index 0000000..055eaec --- /dev/null +++ b/tests/codeception/api/unit/modules/internal/models/BlockFormTest.php @@ -0,0 +1,47 @@ +getMockBuilder(Account::class) + ->setMethods(['save']) + ->getMock(); + + $account->expects($this->once()) + ->method('save') + ->willReturn(true); + + $model = new BlockForm($account); + $this->assertTrue($model->ban()); + $this->assertEquals(Account::STATUS_BANNED, $account->status); + $this->tester->canSeeAmqpMessageIsCreated('events'); + } + + public function testCreateTask() { + $account = new Account(); + $account->id = 3; + + $model = new BlockForm($account); + $model->createTask(); + $message = json_decode($this->tester->grabLastSentAmqpMessage('events')->body, true); + $this->assertSame(3, $message['accountId']); + $this->assertSame(-1, $message['duration']); + $this->assertSame('', $message['message']); + + $model = new BlockForm($account); + $model->duration = 123; + $model->message = 'test'; + $model->createTask(); + $message = json_decode($this->tester->grabLastSentAmqpMessage('events')->body, true); + $this->assertSame(3, $message['accountId']); + $this->assertSame(123, $message['duration']); + $this->assertSame('test', $message['message']); + } + +}