Skip to content

Commit

Permalink
Merge pull request tuyakhov#18 from tuyakhov/fix_active_record_channel
Browse files Browse the repository at this point in the history
fix active record channel using populated model
  • Loading branch information
tuyakhov authored Nov 13, 2018
2 parents 4fc4071 + 6ac6bab commit d10f8f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
23 changes: 9 additions & 14 deletions src/channels/ActiveRecordChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
use tuyakhov\notifications\NotifiableInterface;
use tuyakhov\notifications\NotificationInterface;
use yii\base\Component;
use yii\base\DynamicModel;
use yii\db\ActiveRecordInterface;
use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
use yii\di\Instance;

class ActiveRecordChannel extends Component implements ChannelInterface
{
Expand All @@ -22,17 +20,14 @@ class ActiveRecordChannel extends Component implements ChannelInterface
*/
public $model = 'tuyakhov\notifications\models\Notification';

/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
$this->model = Instance::ensure($this->model, 'yii\db\BaseActiveRecord');
}

public function send(NotifiableInterface $recipient, NotificationInterface $notification)
{
$model = \Yii::createObject($this->model);

if (!$model instanceof BaseActiveRecord) {
throw new InvalidConfigException('Model class must extend from \\yii\\db\\BaseActiveRecord');
}

/** @var DatabaseMessage $message */
$message = $notification->exportFor('database');
list($notifiableType, $notifiableId) = $recipient->routeNotificationFor('database');
Expand All @@ -44,8 +39,8 @@ public function send(NotifiableInterface $recipient, NotificationInterface $noti
'notifiable_id' => $notifiableId,
];

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

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


$channel = \Yii::createObject([
'class' => 'tuyakhov\notifications\channels\ActiveRecordChannel',
'model' => $notificationModel,
'model' => function () use ($message) {
$notificationModel = $this->createMock('yii\db\BaseActiveRecord');
$notificationModel->method('load')->willReturn(true);
$notificationModel->expects($this->once())
->method('load')
->with([
'level' => $message->level,
'subject' => $message->subject,
'body' => $message->body,
'notifiable_type' => 'yii\base\DynamicModel',
'notifiable_id' => 123,
], '');
$notificationModel->method('insert')->willReturn(true);
$notificationModel->expects($this->once())
->method('insert');
return $notificationModel;
},
]);

$notification = $this->createMock('tuyakhov\notifications\NotificationInterface');
Expand Down

0 comments on commit d10f8f9

Please sign in to comment.