- Added new model "SubscriberList"
- Added has_many_through relations to the new model + Subscriber model - Added tests - Fixed syntax in other models This closes #95
This commit is contained in:
@ -33,10 +33,14 @@ class Initializer {
|
||||
$subscribers = Env::$db_prefix . 'subscribers';
|
||||
$settings = Env::$db_prefix . 'settings';
|
||||
$newsletters = Env::$db_prefix . 'newsletters';
|
||||
$lists = Env::$db_prefix . 'lists';
|
||||
$subscriber_list = Env::$db_prefix . 'subscriber_list';
|
||||
|
||||
define('MP_SUBSCRIBERS_TABLE', $subscribers);
|
||||
define('MP_SETTINGS_TABLE', $settings);
|
||||
define('MP_NEWSLETTERS_TABLE', $newsletters);
|
||||
define('MP_LISTS_TABLE', $lists);
|
||||
define('MP_PIVOT_SUBSCRIBER_LIST_TABLE', $subscriber_list);
|
||||
}
|
||||
|
||||
function setupActivator() {
|
||||
|
@ -12,7 +12,9 @@ class Migrator {
|
||||
$this->models = array(
|
||||
'subscribers',
|
||||
'settings',
|
||||
'newsletters'
|
||||
'newsletters',
|
||||
'lists',
|
||||
'pivot_subscriber_list'
|
||||
);
|
||||
}
|
||||
|
||||
@ -77,6 +79,30 @@ class Migrator {
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function lists() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(90) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY name (name)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function pivot_subscriber_list() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subscriber_id mediumint(9) NOT NULL,',
|
||||
'list_id mediumint(9) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (id)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
private function sqlify($model, $attributes) {
|
||||
$table = $this->prefix . $model;
|
||||
|
||||
|
12
lib/Models/PivotSubscriberList.php
Normal file
12
lib/Models/PivotSubscriberList.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class PivotSubscriberList extends Model {
|
||||
public static $_table = MP_PIVOT_SUBSCRIBER_LIST_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
}
|
@ -20,11 +20,11 @@ class Setting extends Model {
|
||||
}
|
||||
|
||||
public static function createOrUpdate($model) {
|
||||
$exists = Setting::where('name', $model['name'])
|
||||
$exists = self::where('name', $model['name'])
|
||||
->find_one();
|
||||
|
||||
if($exists === false) {
|
||||
$new_model = Setting::create();
|
||||
$new_model = self::create();
|
||||
$new_model->hydrate($model);
|
||||
return $new_model->save();
|
||||
}
|
||||
|
@ -14,4 +14,8 @@ class Subscriber extends Model {
|
||||
'isEmail' => __('Your email address is invalid.')
|
||||
));
|
||||
}
|
||||
|
||||
public function lists() {
|
||||
return self::has_many_through(__NAMESPACE__ . '\SubscriberList', __NAMESPACE__ . '\PivotSubscriberList', 'subscriber_id', 'list_id');
|
||||
}
|
||||
}
|
||||
|
35
lib/Models/SubscriberList.php
Normal file
35
lib/Models/SubscriberList.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class SubscriberList extends Model {
|
||||
public static $_table = MP_LISTS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations('name', array(
|
||||
'required' => 'name_is_blank',
|
||||
'isString' => 'name_is_not_string'
|
||||
));
|
||||
}
|
||||
|
||||
public static function createOrUpdate($model) {
|
||||
$exists = self::where('name', $model['name'])
|
||||
->find_one();
|
||||
|
||||
if($exists === false) {
|
||||
$new_model = self::create();
|
||||
$new_model->name = $model['name'];
|
||||
return $new_model->save();
|
||||
}
|
||||
|
||||
$exists->name = $model['name_updated'];
|
||||
return $exists->save();
|
||||
}
|
||||
|
||||
public function subscribers() {
|
||||
return self::has_many_through(__NAMESPACE__ . '\Subscriber', __NAMESPACE__ . '\PivotSubscriberList', 'list_id', 'subscriber_id');
|
||||
}
|
||||
}
|
@ -9,7 +9,9 @@ $console->writeln('Cleaning up database...');
|
||||
$models = array(
|
||||
"Subscriber",
|
||||
"Setting",
|
||||
"Newsletter"
|
||||
"Newsletter",
|
||||
"SubscriberList",
|
||||
"PivotSubscriberList"
|
||||
);
|
||||
$destroy = function ($model) {
|
||||
Model::factory("\MailPoet\Models\\" . $model)
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberList;
|
||||
use MailPoet\Models\PivotSubscriberList;
|
||||
|
||||
class SubscriberCest {
|
||||
|
||||
@ -51,8 +53,31 @@ class SubscriberCest {
|
||||
expect($saved)->equals(false);
|
||||
}
|
||||
|
||||
function itCanHaveAList() {
|
||||
$listData = array(
|
||||
'name' => 'some name'
|
||||
);
|
||||
|
||||
$list = SubscriberList::create();
|
||||
$list->hydrate($listData);
|
||||
$list->save();
|
||||
$association = PivotSubscriberList::create();
|
||||
$association->subscriber_id = $this->subscriber->id;
|
||||
$association->list_id = $list->id;
|
||||
$association->save();
|
||||
|
||||
$subscriber = Subscriber::find_one($this->subscriber->id);
|
||||
$subscriberList = $subscriber->lists()
|
||||
->find_one();
|
||||
expect($subscriberList->id)->equals($list->id);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::for_table(Subscriber::$_table)
|
||||
->delete_many();
|
||||
ORM::for_table(SubscriberList::$_table)
|
||||
->delete_many();
|
||||
ORM::for_table(PivotSubscriberList::$_table)
|
||||
->delete_many();
|
||||
}
|
||||
}
|
||||
|
120
tests/unit/Models/SubscriberListCest.php
Normal file
120
tests/unit/Models/SubscriberListCest.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
use MailPoet\Models\PivotSubscriberList;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberList;
|
||||
|
||||
class SubscriberListCest {
|
||||
function _before() {
|
||||
$this->before_time = time();
|
||||
$this->data = array(
|
||||
'name' => 'some name',
|
||||
);
|
||||
|
||||
$this->list = SubscriberList::create();
|
||||
$this->list->hydrate($this->data);
|
||||
$this->saved = $this->list->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
expect($this->saved)->equals(true);
|
||||
}
|
||||
|
||||
function itHasToBeValid() {
|
||||
expect($this->saved)->equals(true);
|
||||
$empty_model = SubscriberList::create();
|
||||
expect($empty_model->save())->equals(false);
|
||||
$validations = $empty_model->getValidationErrors();
|
||||
expect(count($validations))->equals(2);
|
||||
}
|
||||
|
||||
function itHasACreatedAtOnCreation() {
|
||||
$list = SubscriberList::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($list->created_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itHasAnUpdatedAtOnCreation() {
|
||||
$list = SubscriberList::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($list->updated_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itKeepsTheCreatedAtOnUpdate() {
|
||||
$list = SubscriberList::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$old_created_at = $list->created_at;
|
||||
$list->name = 'new name';
|
||||
$list->save();
|
||||
expect($old_created_at)->equals($list->created_at);
|
||||
}
|
||||
|
||||
function itUpdatesTheUpdatedAtOnUpdate() {
|
||||
$list = SubscriberList::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$update_time = time();
|
||||
$list->name = 'new name';
|
||||
$list->save();
|
||||
$time_difference = strtotime($list->updated_at) >= $update_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itCanCreateOrUpdate() {
|
||||
$data = array(
|
||||
'name' => 'some other new name'
|
||||
);
|
||||
$createNewRecord = SubscriberList::createOrUpdate($data);
|
||||
|
||||
$data = array(
|
||||
'name' => $this->data['name'],
|
||||
'name_updated' => 'updated name',
|
||||
);
|
||||
$updateExistingRecord = SubscriberList::createOrUpdate($data);
|
||||
|
||||
$allRecords = SubscriberList::find_array();
|
||||
expect(count($allRecords))->equals(2);
|
||||
expect($allRecords[0]['name'])->equals($data['name_updated']);
|
||||
}
|
||||
|
||||
function itCanHaveMultipleSubscribers() {
|
||||
$subscribersData = array(
|
||||
array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
),
|
||||
array(
|
||||
'first_name' => 'Mike',
|
||||
'last_name' => 'Smith',
|
||||
'email' => 'mike@maipoet.com'
|
||||
)
|
||||
);
|
||||
foreach ($subscribersData as $subscriberData) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->hydrate($subscriberData);
|
||||
$subscriber->save();
|
||||
$association = PivotSubscriberList::create();
|
||||
$association->subscriber_id = $subscriber->id;
|
||||
$association->list_id = $this->list->id;
|
||||
$association->save();
|
||||
}
|
||||
|
||||
$list = SubscriberList::find_one($this->list->id);
|
||||
$subscribers = $list->subscribers()
|
||||
->find_array();
|
||||
expect(count($subscribers))->equals(2);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::for_table(SubscriberList::$_table)
|
||||
->delete_many();
|
||||
ORM::for_table(Subscriber::$_table)
|
||||
->delete_many();
|
||||
ORM::for_table(PivotSubscriberList::$_table)
|
||||
->delete_many();
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user