...
 
Commits (2)
<?php
namespace Minds\Core\Email\CampaignLogs;
use Minds\Traits\MagicAttributes;
/**
* @method int getReceiverGuid()
* @method CampaignLog setReceiverGuid(int $receiverGuid)
* @method int getTimeSent()
* @method CampaignLog setTimeSet(int $timeSent)
* @method string getEmailCampaignId()
* @method CampaignLog setEmailCampaignId(string $emailCampaignId)
*/
class CampaignLog
{
use MagicAttributes;
/** @var int $receiverGuid the user guid who received the email */
protected $receiverGuid;
/** @var int $timeStamp the timestamp when the email was sent */
protected $timeSent;
/** @var string $emailCampaignId the class name of the email campaign */
protected $emailCampaignID;
}
<?php
/**
* Email Campaign Log Repository.
*/
namespace Minds\Core\Email\CampaignLogs;
use Minds\Core\Data\Cassandra\Client;
use Minds\Core\Di\Di;
use Cassandra\Varint;
use Cassandra\Timestamp;
use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Core\Email\CampaignLogs\CampaignLog;
class Repository
{
/**
* @var Client
*/
protected $db;
/**
* Repository constructor.
*
* @param null $db
*/
public function __construct($db = null)
{
$this->db = $db ?: Di::_()->get('Database\Cassandra\Cql');
}
/**
* Gets the list of email states, they are clustered on time_sent, so they will be in descending order.
*
* @param array $options
*
* receiver_guid the user guid
* limit 10
*
* @return CampaignLog[]
*/
public function getList(array $options = [])
{
$options = array_merge([
'limit' => 10,
'offset' => '',
'receiver_guid' => null,
], $options);
$template = 'SELECT * FROM email_campaign_logs';
$where = "";
$values = [];
if (isset($options['receiver_guid'])) {
$where = 'receiver_guid = ?';
$values[] = new Varint($options['receiver_guid']);
}
if ($where) {
$template .= ' WHERE ' . $where;
}
$query = new Custom();
$query->query($template, $values);
$query->setOpts([
'page_size' => (int) $options['limit'],
'paging_state_token' => $options['offset'],
]);
try {
$result = $this->db->request($query);
} catch (\Exception $e) {
error_log($e);
}
$campaignLog = [];
$token = '';
if ($result) {
foreach ($result as $row) {
$campaignLog = (new CampaignLog())
->setReceiverGuid($row['receiver_guid']->value())
->setTimeSent($row['time_sent'])
->setEmailCampaignId($row['email_campaign_id']);
$campaignLogs[] = $campaignLog;
}
$token = base64_encode($result->pagingStateToken());
}
return [
'data' => $campaignLogs,
'next' => $token,
];
}
/**
* Inserts an email campaign log into cassandra.
*
* @param CampaignLog $campaignLog
*
* @return bool the write results
*
* @throws \Exception
*/
public function add(CampaignLog $campaignLog)
{
$template = 'INSERT INTO email_campaign_logs (receiver_guid, time_sent, email_campaign_id) VALUES (?, ?, ?)';
$values = [
new Varint($campaignLog->getReceiverGuid()),
new Timestamp($campaignLog->getTimeSent()),
(string) $campaignLog->getEmailCampaignId(),
];
$query = new Custom();
$query->query($template, $values);
return $this->db->request($query);
}
}
......@@ -7,6 +7,7 @@ use Minds\Core\Email\EmailSubscription;
use Minds\Core\Email\Manager;
use Minds\Entities\User;
use Minds\Traits\MagicAttributes;
use Minds\Core\Email\CampaignLogs\CampaignLog;
abstract class EmailCampaign
{
......@@ -65,4 +66,31 @@ abstract class EmailCampaign
return true;
}
/**
* Returns the short name of the class as the template name.
*/
public function getEmailCampaignId()
{
return (new \ReflectionClass($this))->getShortName();
}
/**
* Saves when the user received the email campaign to the db.
*
* @var int defaults to the current time
*/
public function saveCampaignLog(int $time = null)
{
$time = $time ?: time();
if (!$this->manager || !$this->user) {
return false;
}
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->user->guid)
->setTimeSent($time)
->setEmailCampaignId($this->getEmailCampaignId());
$this->manager->saveCampaignLog($campaignLog);
}
}
......@@ -70,11 +70,13 @@ class GoneCold extends EmailCampaign
return $message;
}
public function send()
{
public function send($time = null)
{
$time = $time ?: time();
//send email
if ($this->canSend()) {
$this->mailer->queue($this->build());
$this->saveCampaignLog($time);
}
}
}
......@@ -70,11 +70,13 @@ class WelcomeComplete extends EmailCampaign
return $message;
}
public function send()
public function send($time = null)
{
$time = $time ?: time();
//send email
if ($this->canSend()) {
$this->mailer->queue($this->build());
$this->saveCampaignLog($time);
}
}
}
......@@ -75,6 +75,7 @@ class WelcomeIncomplete extends EmailCampaign
//send email
if ($this->canSend()) {
$this->mailer->queue($this->build());
$this->saveCampaignLog();
}
}
}
<?php
namespace Minds\Core\Email\Delegates;
use Minds\Core\Suggestions\Manager;
use Minds\Entities\User;
use Minds\Core\Di\Di;
use Minds\Interfaces\SenderInterface;
use Minds\Core\Email\Campaigns\UserRetention\GoneCold;
class GoneColdSender implements SenderInterface
{
/** @var Manager */
private $manager;
/** @var GoneCold */
private $campaign;
public function __construct(Manager $manager = null, GoneCold $campaign = null)
{
$this->manager = $manager ?: Di::_()->get('Suggestions\Manager');
$this->campaign = $campaign ?: new GoneCold();
}
public function send(User $user)
{
$this->manager->setUser($user);
$suggestions = $this->manager->getList();
$this->campaign->setUser($user);
$this->campaign->setSuggestions($suggestions);
$this->campaign->send();
}
}
<?php
namespace Minds\Core\Email\Delegates;
use Minds\Entities\User;
use Minds\Core\Di\Di;
use Minds\Core\Suggestions\Manager as SuggestionsManager;
use Minds\Core\Onboarding\Manager as OnboardingManager;
use Minds\Interfaces\SenderInterface;
use Minds\Core\Email\Campaigns\UserRetention\WelcomeComplete;
use Minds\Core\Email\Campaigns\UserRetention\WelcomeIncomplete;
class WelcomeSender implements SenderInterface
{
/** @var SuggestionsManager */
private $suggestionsManager;
/** @var OnboardingManager */
private $onboardingManager;
/** @var WelcomeComplete */
private $welcomeComplete;
/** @var WelcomeIncomplete */
private $welcomeIncomplete;
public function __construct(
SuggestionsManager $suggestionsManager = null,
OnboardingManager $onboardingManager = null,
WelcomeComplete $welcomeComplete = null,
WelcomeIncomplete $welcomeIncomplete = null
) {
$this->suggestionsManager = $suggestionsManager ?: Di::_()->get('Suggestions\Manager');
$this->onboardingManager = $onboardingManager ?: Di::_()->get('Onboarding\Manager');
$this->welcomeComplete = $welcomeComplete ?: new WelcomeComplete();
$this->welcomeIncomplete = $welcomeIncomplete ?: new WelcomeIncomplete();
}
public function send(User $user)
{
$this->onboardingManager->setUser($user);
$campaign = $this->welcomeComplete;
if ($this->onboardingManager->isComplete()) {
$this->suggestionsManager->setUser($user);
$suggestions = $this->suggestionsManager->getList();
$campaign->setSuggestions($suggestions);
} else {
$campaign = $this->welcomeIncomplete;
}
$campaign->setUser($user);
$campaign->send();
}
}
......@@ -8,11 +8,13 @@ namespace Minds\Core\Email;
use Minds\Core\Di\Di;
use Minds\Core\Events\Dispatcher;
use Minds\Core\Analytics\UserStates\UserActivityBuckets;
use Minds\Core\Email\Campaigns\UserRetention\GoneCold;
use Minds\Core\Email\Campaigns\UserRetention\WelcomeComplete;
use Minds\Core\Email\Campaigns\UserRetention\WelcomeIncomplete;
use Minds\Entities\User;
use Minds\Core\Email\Manager;
use Minds\Core\Suggestions\Manager as SuggestionManager;
use Minds\Interfaces\SenderInterface;
class Events
{
......@@ -36,6 +38,7 @@ class Events
Dispatcher::register('user_state_change', UserActivityBuckets::STATE_NEW, function ($opts) {
error_log('user_state_change new');
$this->sendCampaign(new Delegates\WelcomeSender(), $opts->getParameters());
});
Dispatcher::register('user_state_change', UserActivityBuckets::STATE_RESURRECTED, function ($opts) {
......@@ -43,37 +46,17 @@ class Events
});
Dispatcher::register('user_state_change', UserActivityBuckets::STATE_COLD, function ($opts) {
error_log('user_state_change cold');
$params = $opts->getParameters();
$user = new User($params['user_guid']);
$manager = new SuggestionManager();
$manager->setUser($user);
$suggestions = $manager->getList();
$campaign = (new GoneCold())
->setUser($user)
->setSuggestions($suggestions);
$campaign->send();
$this->sendCampaign(new Delegates\GoneColdSender(), $opts->getParameters());
});
Dispatcher::register('welcome_email', 'all', function ($opts) {
error_log('welcome_email');
$params = $opts->getParameters();
$user = new User($params['user_guid']);
$onboardingManager = Di::_()->get('Onboarding\Manager');
$onboardingManager->setUser($user);
if ($onboardingManager->isComplete()) {
$campaign = (new WelcomeComplete());
$suggestionManager = Di::_()->get('Suggestions\Manager');
$suggestionManager->setUser($user);
$suggestions = $suggestionManager->getList();
$campaign->setSuggestions($suggestions);
} else {
$campaign = (new WelcomeIncomplete());
error_log('Sending Welcome Incomplete');
}
$campaign->setUser($user);
$campaign->send();
$this->sendCampaign(new Delegates\WelcomeSender(), $opts->getParameters());
});
}
private function sendCampaign (SenderInterface $sender, $params) {
$user = new User($params['user_guid']);
$sender->send($user);
}
}
......@@ -7,15 +7,24 @@ namespace Minds\Core\Email;
use Minds\Core\Di\Di;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Entities;
use Minds\Entities\User;
use Minds\Core\Email\Repository;
use Minds\Core\Email\CampaignLogs\Repository as CampaignLogsRepository;
use Minds\Core\Email\CampaignLogs\CampaignLog;
class Manager
{
/** @var Repository */
protected $repository;
public function __construct($repository = null)
/** @var CampaignLogsRepository */
protected $campaignLogsRepository;
public function __construct(Repository $repository = null, CampaignLogsRepository $campaignLogsRepository = null)
{
$this->repository = $repository ?: Di::_()->get('Email\Repository');
$this->campaignLogsRepository = $campaignLogsRepository ?: Di::_()->get('Email\CampaignLogs\Repository');
}
public function getSubscribers($options = [])
......@@ -108,4 +117,20 @@ class Manager
return true;
}
/**
* Saves a log when we send a user a campaign email
* Used to select subsequent mailings and send different emails
* @param CampaignLog $campaignLog the receiver, time and campaign class name
* @return boolean the add result
*/
public function saveCampaignLog(CampaignLog $campaignLog) {
$this->campaignLogsRepository->add($campaignLog);
}
public function getCampaignLogs(User $receiver) {
$options = [
'receiver_guid' => $receiver->guid
];
return $this->campaignLogsRepository->getList($options);
}
}
......@@ -37,5 +37,9 @@ class Provider extends DiProvider
$this->di->bind('Email\EmailStyles', function ($di) {
return new EmailStyles();
}, ['useFactory' => false]);
$this->di->bind('Email\CampaignLogs\Repository', function ($di) {
return new CampaignLogs\Repository();
}, ['useFactory' => true]);
}
}
......@@ -1429,3 +1429,10 @@ CREATE TABLE minds.user_snapshots (
json_data text,
PRIMARY KEY (user_guid, type, key)
) WITH CLUSTERING ORDER BY (type ASC, key ASC);
CREATE TABLE minds.email_campaign_logs (
receiver_guid varint,
time_sent timestamp,
email_campaign_id text,
PRIMARY KEY (receiver_guid, time_sent)
) WITH CLUSTERING ORDER BY (time_sent desc);
<?php
namespace Minds\Interfaces;
use Minds\Entities\User;
/**
* Delegate interface for sending emails
*/
interface SenderInterface
{
/**
* sending campaign emails to a user
* @return void
*/
public function send(User $user);
}
\ No newline at end of file
<?php
namespace Spec\Minds\Core\Email\CampaignLogs;
use Minds\Core\Data\Cassandra\Client;
use Minds\Core\Email\CampaignLogs\Repository;
use Minds\Core\Email\CampaignLogs\CampaignLog;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Spec\Minds\Mocks\Cassandra\Rows;
use Cassandra\Varint;
class RepositorySpec extends ObjectBehavior
{
protected $db;
public function let(Client $db)
{
$this->db = $db;
$this->beConstructedWith($db);
}
public function it_is_initializable()
{
$this->shouldHaveType(Repository::class);
}
public function it_should_write_a_log ()
{
$campaignLog = (new CampaignLog())
->setReceiverGuid(123)
->setTimeSent(0)
->setEmailCampaignId('test');
$this->db->request(Argument::that(function ($query) {
$built = $query->build();
return $built['string'] === 'INSERT INTO email_campaign_logs (receiver_guid, time_sent, email_campaign_id) VALUES (?, ?, ?)';
}))
->shouldBeCalled()
->willReturn(true);
$this->add($campaignLog)->shouldReturn(true);
}
public function it_should_get_a_list_of_logs()
{
$opts = [
'receiver_guid' => 123,
'limit' => 10,
'offset' => '',
];
$this->db->request(Argument::that(function ($query) {
$built = $query->build();
return $built['string'] === 'SELECT * FROM email_campaign_logs WHERE receiver_guid = ?';
}))
->shouldBeCalled()
->willReturn(new Rows([
[
'receiver_guid' => new Varint(123),
'time_sent' => 1,
'email_campaign_id' => 'test'
],
[
'receiver_guid' => new Varint(123),
'time_sent' => 1,
'email_campaign_id' => 'test2'
],
], ''));
$this->getList($opts)->shouldBeArray();
}
}
......@@ -8,6 +8,7 @@ use Minds\Core\Email\Mailer;
use Minds\Core\Email\Manager;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Suggestions\Suggestion;
use Minds\Core\Email\CampaignLogs\CampaignLog;
use Minds\Entities\User;
use Prophecy\Argument;
......@@ -57,15 +58,24 @@ class GoneColdSpec extends ObjectBehavior
$data['username']->shouldEqual($this->testUsername);
$this->mailer->queue(Argument::any())->shouldBeCalled();
$testEmailSubscription = (new EmailSubscription())
->setUserGuid($this->testGUID)
->setCampaign('global')
->setTopic('minds_tips')
->setValue(true);
$time = time();
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->testGUID)
->setTimeSent($time)
->setEmailCampaignId($this->getEmailCampaignId()->getWrappedObject());
$this->manager->saveCampaignLog($campaignLog)->shouldBeCalled();
$this->manager->isSubscribed($testEmailSubscription)->shouldBeCalled()->willReturn(true);
$this->send();
$this->send($time);
}
public function it_should_not_send_unsubscribed(User $user)
......
......@@ -8,6 +8,7 @@ use Minds\Core\Email\Mailer;
use Minds\Core\Email\Manager;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Suggestions\Suggestion;
use Minds\Core\Email\CampaignLogs\CampaignLog;
use Minds\Entities\User;
use Prophecy\Argument;
......@@ -66,6 +67,15 @@ class WelcomeCompleteSpec extends ObjectBehavior
->setTopic('minds_tips')
->setValue(true);
$time = time();
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->testGUID)
->setTimeSent($time)
->setEmailCampaignId($this->getEmailCampaignId()->getWrappedObject());
$this->manager->saveCampaignLog($campaignLog)->shouldBeCalled();
$this->manager->isSubscribed($testEmailSubscription)->shouldBeCalled()->willReturn(true);
$this->send();
}
......
......@@ -7,6 +7,7 @@ use PhpSpec\ObjectBehavior;
use Minds\Core\Email\Mailer;
use Minds\Core\Email\Manager;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Email\CampaignLogs\CampaignLog;
use Minds\Entities\User;
use Prophecy\Argument;
......@@ -63,8 +64,17 @@ class WelcomeIncompleteSpec extends ObjectBehavior
->setTopic('minds_tips')
->setValue(true);
$time = time();
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->testGUID)
->setTimeSent($time)
->setEmailCampaignId($this->getEmailCampaignId()->getWrappedObject());
$this->manager->saveCampaignLog($campaignLog)->shouldBeCalled();
$this->manager->isSubscribed($testEmailSubscription)->shouldBeCalled()->willReturn(true);
$this->send();
$this->send($time);
}
public function it_should_not_send_unsubscribed(User $user)
......
<?php
namespace Spec\Minds\Core\Email\Delegates;
use Minds\Core\Email\Delegates\GoneColdSender;
use PhpSpec\ObjectBehavior;
use Minds\Core\Suggestions\Manager as SuggestionsManager;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Email\Manager;
use Minds\Core\Email\Mailer;
use Minds\Entities\User;
use Minds\Core\Email\Campaigns\UserRetention\GoneCold;
use Minds\Core\Email\CampaignLogs\CampaignLog;
class GoneColdSenderSpec extends ObjectBehavior
{
/** @var Manager $manager */
private $manager;
/** @var SuggestionsManager $manager */
private $suggestionsManager;
/** @var Mailer $mailer */
private $mailer;
/** @var GoneCold $campaign */
private $campaign;
private $testGUID = 123;
private $testName = 'test_name';
private $testEmail = 'test@minds.com';
private $testUsername = 'testUsername';
private $testBriefDescription = 'test brief description';
public function let(Manager $manager, SuggestionsManager $suggestionsManager, Mailer $mailer)
{
$this->manager = $manager;
$this->suggestionsManager = $suggestionsManager;
$this->mailer = $mailer;
$campaign = new GoneCold(null, $mailer->getWrappedObject(), $manager->getWrappedObject());
$this->campaign = $campaign;
$this->beConstructedWith($suggestionsManager, $campaign);
}
public function it_is_initializable()
{
$this->shouldHaveType(GoneColdSender::class);
}
public function it_should_send(User $user)
{
$user->getGUID()->shouldBeCalled()->willReturn($this->testGUID);
$user->get('enabled')->shouldBeCalled()->willReturn('yes');
$user->get('name')->shouldBeCalled()->willReturn($this->testName);
$user->get('guid')->shouldBeCalled()->willReturn($this->testGUID);
$user->getEmail()->shouldBeCalled()->willReturn($this->testEmail);
$user->get('username')->shouldBeCalled()->willReturn($this->testUsername);
$this->suggestionsManager->setUser($user)->shouldBeCalled();
$this->suggestionsManager->getList()->shouldBeCalled();
$emailSubscription = (new EmailSubscription())
->setUserGuid(123)
->setCampaign('global')
->setTopic('minds_tips')
->setValue('true');
$time = time();
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->testGUID)
->setTimeSent($time)
->setEmailCampaignId($this->campaign->getEmailCampaignId());
$this->manager->saveCampaignLog($campaignLog)->shouldBeCalled();
$this->manager->isSubscribed($emailSubscription)->shouldBeCalled()->willReturn(true);
$this->send($user);
}
}
<?php
namespace Spec\Minds\Core\Email\Delegates;
use Minds\Core\Email\Delegates\WelcomeSender;
use PhpSpec\ObjectBehavior;
use Minds\Core\Suggestions\Manager as SuggestionsManager;
use Minds\Core\Onboarding\Manager as OnboardingManager;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Email\Manager;
use Minds\Core\Email\Mailer;
use Minds\Entities\User;
use Minds\Core\Email\Campaigns\UserRetention\WelcomeComplete;
use Minds\Core\Email\Campaigns\UserRetention\WelcomeIncomplete;
use Minds\Core\Email\CampaignLogs\CampaignLog;
class WelcomeSenderSpec extends ObjectBehavior
{
/** @var Manager $manager */
private $manager;
/** @var SuggestionsManager */
private $suggestionsManager;
/** @var OnboardingManager */
private $onboardingManager;
/** @var WelcomeComplete */
private $welcomeComplete;
/** @var WelcomeIncomplete */
private $welcomeIncomplete;
private $testGUID = 123;
private $testName = 'test_name';
private $testEmail = 'test@minds.com';
private $testUsername = 'testUsername';
private $testBriefDescription = 'test brief description';
public function let(
Manager $manager,
SuggestionsManager $suggestionsManager,
OnboardingManager $onboardingManager,
Mailer $mailer
) {
$welcomeComplete = new WelcomeComplete(null, $mailer->getWrappedObject(), $manager->getWrappedObject());
$welcomeIncomplete = new WelcomeIncomplete(null, $mailer->getWrappedObject(), $manager->getWrappedObject());
$this->manager = $manager;
$this->suggestionsManager = $suggestionsManager;
$this->onboardingManager = $onboardingManager;
$this->welcomeComplete = $welcomeComplete;
$this->welcomeIncomplete = $welcomeIncomplete;
$this->beConstructedWith($suggestionsManager, $onboardingManager, $welcomeComplete, $welcomeIncomplete);
}
public function it_is_initializable()
{
$this->shouldHaveType(WelcomeSender::class);
}
public function it_should_send_a_welcome_complete(User $user)
{
$user->getGUID()->shouldBeCalled()->willReturn($this->testGUID);
$user->get('enabled')->shouldBeCalled()->willReturn('yes');
$user->get('name')->shouldBeCalled()->willReturn($this->testName);
$user->get('guid')->shouldBeCalled()->willReturn($this->testGUID);
$user->getEmail()->shouldBeCalled()->willReturn($this->testEmail);
$user->get('username')->shouldBeCalled()->willReturn($this->testUsername);
$this->onboardingManager->setUser($user)->shouldBeCalled();
$this->onboardingManager->isComplete()->shouldBeCalled()->willReturn(true);
$this->suggestionsManager->setUser($user)->shouldBeCalled();
$this->suggestionsManager->getList()->shouldBeCalled();
$emailSubscription = (new EmailSubscription())
->setUserGuid(123)
->setCampaign('global')
->setTopic('minds_tips')
->setValue('true');
$time = time();
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->testGUID)
->setTimeSent($time)
->setEmailCampaignId($this->welcomeComplete->getEmailCampaignId());
$this->manager->saveCampaignLog($campaignLog)->shouldBeCalled();
$this->manager->isSubscribed($emailSubscription)->shouldBeCalled()->willReturn(true);
$this->send($user);
}
public function it_should_send_a_welcome_incomplete(User $user)
{
$user->getGUID()->shouldBeCalled()->willReturn($this->testGUID);
$user->get('enabled')->shouldBeCalled()->willReturn('yes');
$user->get('name')->shouldBeCalled()->willReturn($this->testName);
$user->get('guid')->shouldBeCalled()->willReturn($this->testGUID);
$user->getEmail()->shouldBeCalled()->willReturn($this->testEmail);
$user->get('username')->shouldBeCalled()->willReturn($this->testUsername);
$this->onboardingManager->setUser($user)->shouldBeCalled();
$this->onboardingManager->isComplete()->shouldBeCalled()->willReturn(false);
$this->suggestionsManager->setUser($user)->shouldNotBeCalled();
$this->suggestionsManager->getList()->shouldNotBeCalled();
$emailSubscription = (new EmailSubscription())
->setUserGuid(123)
->setCampaign('global')
->setTopic('minds_tips')
->setValue('true');
$time = time();
$campaignLog = (new CampaignLog())
->setReceiverGuid($this->testGUID)
->setTimeSent($time)
->setEmailCampaignId($this->welcomeIncomplete->getEmailCampaignId());
$this->manager->saveCampaignLog($campaignLog)->shouldBeCalled();
$this->manager->isSubscribed($emailSubscription)->shouldBeCalled()->willReturn(true);
$this->send($user);
}
}
......@@ -6,20 +6,30 @@ use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Minds\Core\Email\Repository;
use Minds\Core\Email\CampaignLogs\Repository as CampaignLogsRepository;
use Minds\Entities\User;
use Minds\Core\Email\EmailSubscription;
use Minds\Core\Email\CampaignLogs\CampaignLog;
class ManagerSpec extends ObjectBehavior
{
private $repository;
private $campaignLogsRepository;
function let(Repository $repository, CampaignLogsRepository $campaignLogsRepository) {
$this->repository = $repository;
$this->campaignLogsRepository = $campaignLogsRepository;
$this->beConstructedWith($this->repository, $this->campaignLogsRepository);
}
function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Email\Manager');
}
function it_should_get_subscribers(Repository $repository)
function it_should_get_subscribers()
{
$this->beConstructedWith($repository);
$opts = [
'campaign' => 'when',
'topic' => 'boost_completed',
......@@ -34,7 +44,7 @@ class ManagerSpec extends ObjectBehavior
$user1->guid = '456';
$user1->username = 'user2';
$repository->getList(Argument::type('array'))
$this->repository->getList(Argument::type('array'))
->shouldBeCalled()
->willReturn([
'data' => [
......@@ -48,15 +58,13 @@ class ManagerSpec extends ObjectBehavior
}
function it_should_unsubscribe_a_user_from_a_campaign(Repository $repository)
function it_should_unsubscribe_a_user_from_a_campaign()
{
$this->beConstructedWith($repository);
$user = new User();
$user->guid = '123';
$user->username = 'user1';
$repository->delete(Argument::type('Minds\Core\Email\EmailSubscription'))
$this->repository->delete(Argument::type('Minds\Core\Email\EmailSubscription'))
->shouldBeCalled()
->willReturn(true);
......@@ -65,10 +73,8 @@ class ManagerSpec extends ObjectBehavior
}
function it_should_unsubscribe_from_all_emails(Repository $repository)
function it_should_unsubscribe_from_all_emails()
{
$this->beConstructedWith($repository);
$user = new User();
$user->guid = '123';
......@@ -83,7 +89,7 @@ class ManagerSpec extends ObjectBehavior
->setTopic('top_posts'),
];
$repository->getList([
$this->repository->getList([
'campaigns' => [ 'when', 'with', 'global' ],
'topics' => [
'unread_notifications',
......@@ -102,14 +108,30 @@ class ManagerSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn($subscriptions);
$repository->delete($subscriptions[0])
$this->repository->delete($subscriptions[0])
->shouldBeCalled();
$repository->delete($subscriptions[1])
$this->repository->delete($subscriptions[1])
->shouldBeCalled();
$this->unsubscribe($user)
->shouldReturn(true);
}
function it_should_save_a_campaign_log() {
$campaignLog = new CampaignLog();
$this->campaignLogsRepository->add($campaignLog)->shouldBeCalled();
$this->saveCampaignLog($campaignLog);
}
function it_should_get_campaign_logs() {
$user = new User();
$user->guid = '123';
$options = [
'receiver_guid' => $user->guid
];
$this->campaignLogsRepository->getList($options)->shouldBeCalled();
$this->getCampaignLogs($user);
}
}