Skip to content

Commit

Permalink
Fix saving database notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyakhov committed Aug 24, 2018
1 parent 8ff3bc5 commit 637befc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/channels/ActiveRecordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use tuyakhov\notifications\NotifiableInterface;
use tuyakhov\notifications\NotificationInterface;
use yii\base\Component;
use yii\base\DynamicModel;
use yii\db\ActiveRecordInterface;
use yii\db\BaseActiveRecord;
use yii\di\Instance;

class ActiveRecordChannel extends Component implements ChannelInterface
{
/**
* @var ActiveRecordInterface|string
* @var BaseActiveRecord|string
*/
public $model = 'tuyakhov\notifications\models\Notification';

Expand All @@ -26,20 +28,26 @@ class ActiveRecordChannel extends Component implements ChannelInterface
public function init()
{
parent::init();
$this->model = Instance::ensure($this->model, 'yii\db\ActiveRecordInterface');
$this->model = Instance::ensure($this->model, 'yii\db\BaseActiveRecord');
}

public function send(NotifiableInterface $recipient, NotificationInterface $notification)
{
/** @var DatabaseMessage $message */
$message = $notification->exportFor('database');
list($notifiableType, $notifiableId) = $recipient->routeNotificationFor('database');
return $this->model->insert(true, [
$data = [
'level' => $message->level,
'subject' => $message->subject,
'body' => $message->body,
'notifiable_type' => $notifiableType,
'notifiable_id' => $notifiableId,
]);
];

if ($this->model->load($data, '')) {
return $this->model->insert();
}

return false;
}
}
14 changes: 8 additions & 6 deletions tests/ActiveRecordChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ public function testSend()
'subject' => 'It',
'body' => 'Works',
]);
$notificationModel = $this->createMock('yii\db\ActiveRecordInterface');
$notificationModel->method('insert');
$notificationModel = $this->createMock('yii\db\BaseActiveRecord');
$notificationModel->method('load')->willReturn(true);
$notificationModel->expects($this->once())
->method('insert')
->with(true, [
->method('load')
->with([
'level' => $message->level,
'subject' => $message->subject,
'body' => $message->body,
'notifiable_type' => 'yii\base\DynamicModel',
'notifiable_id' => 123,
])
->willReturnSelf();
], '');
$notificationModel->method('insert')->willReturn(true);
$notificationModel->expects($this->once())
->method('insert');


$channel = \Yii::createObject([
Expand Down
3 changes: 1 addition & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

Yii::setAlias('@tuyakhov/tests', __DIR__);
Yii::setAlias('@tuyakhov/braintree', dirname(__DIR__));
Yii::setAlias('@extension', __DIR__ . '/../');

0 comments on commit 637befc

Please sign in to comment.