Jun 28, 2017
0
Level 2
How do you test methods with no return value & how do you mock a collection of models?
Can anyone tell me how I can test the notifySubscribers method this class?
If it doesnt return anything how do I assert anything? Also it takes a collection of User models as a parameter, do I need to add a mock collection? or a collection or mock user models? How do I add state to these models?
Thanks.
namespace App\CoreServices;
class Notifier {
/**
* Broadcaster object. Does the actual pushing out
*
* @var \App\CoreServices\Broadcaster
**/
private $broadcaster;
/**
* Notification Model
*
* @var \Models\Notification
**/
private $notification;
/**
* Instanciate the notification and the broadcaster model
*
* @param \Models\Notification $notification
* @param \App\CoreServices\Broadcaster $broadcaster
* @return void
**/
public function __construct(\App\CoreServices\Broadcaster $broadcaster, \Groopeze\Models\Notification $notification)
{
$this->broadcaster = $broadcaster;
$this->notification = $notification;
}
/**
* Notifies each of the subscribed users
*
* @param array $subscribers
* @param array $message
* @param string $eventName
* @return void
**/
public function notifySubscribers(array $message, \Illuminate\Support\Collection $eventSubscribers, $eventName) {
foreach($eventSubscribers as $i => $user) {
$this->notification->message = $message["message"];
$user->notifications()->save($this->notification);
$this->broadcast($user, $eventName, $message);
}
}
/**
* Calls the broadcaster to push out the message
*
* @param User $user
* @return void
**/
private function broadcast(\Groopeze\Models\User $user, $eventName, array $message) {
$setting = $user->settings->where("event", $eventName)->all();
$notifyMethod = $setting[0]->personalSettings[0]->setting_value;
$message["message"] = $message["message"];
$this->broadcaster->$notifyMethod($user, $eventName, $message);
}
}
Please or to participate in this conversation.