- 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:
MrCasual
2015-09-01 09:18:00 -04:00
parent e0ef01d9f8
commit 0015bdb617
9 changed files with 239 additions and 11 deletions

View File

@ -33,10 +33,14 @@ class Initializer {
$subscribers = Env::$db_prefix . 'subscribers'; $subscribers = Env::$db_prefix . 'subscribers';
$settings = Env::$db_prefix . 'settings'; $settings = Env::$db_prefix . 'settings';
$newsletters = Env::$db_prefix . 'newsletters'; $newsletters = Env::$db_prefix . 'newsletters';
$lists = Env::$db_prefix . 'lists';
$subscriber_list = Env::$db_prefix . 'subscriber_list';
define('MP_SUBSCRIBERS_TABLE', $subscribers); define('MP_SUBSCRIBERS_TABLE', $subscribers);
define('MP_SETTINGS_TABLE', $settings); define('MP_SETTINGS_TABLE', $settings);
define('MP_NEWSLETTERS_TABLE', $newsletters); define('MP_NEWSLETTERS_TABLE', $newsletters);
define('MP_LISTS_TABLE', $lists);
define('MP_PIVOT_SUBSCRIBER_LIST_TABLE', $subscriber_list);
} }
function setupActivator() { function setupActivator() {

View File

@ -12,7 +12,9 @@ class Migrator {
$this->models = array( $this->models = array(
'subscribers', 'subscribers',
'settings', 'settings',
'newsletters' 'newsletters',
'lists',
'pivot_subscriber_list'
); );
} }
@ -77,6 +79,30 @@ class Migrator {
return $this->sqlify(__FUNCTION__, $attributes); 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) { private function sqlify($model, $attributes) {
$table = $this->prefix . $model; $table = $this->prefix . $model;

View 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();
}
}

View File

@ -20,11 +20,11 @@ class Setting extends Model {
} }
public static function createOrUpdate($model) { public static function createOrUpdate($model) {
$exists = Setting::where('name', $model['name']) $exists = self::where('name', $model['name'])
->find_one(); ->find_one();
if($exists === false) { if($exists === false) {
$new_model = Setting::create(); $new_model = self::create();
$new_model->hydrate($model); $new_model->hydrate($model);
return $new_model->save(); return $new_model->save();
} }

View File

@ -1,7 +1,7 @@
<?php <?php
namespace MailPoet\Models; namespace MailPoet\Models;
if (!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class Subscriber extends Model { class Subscriber extends Model {
public static $_table = MP_SUBSCRIBERS_TABLE; public static $_table = MP_SUBSCRIBERS_TABLE;
@ -14,4 +14,8 @@ class Subscriber extends Model {
'isEmail' => __('Your email address is invalid.') 'isEmail' => __('Your email address is invalid.')
)); ));
} }
public function lists() {
return self::has_many_through(__NAMESPACE__ . '\SubscriberList', __NAMESPACE__ . '\PivotSubscriberList', 'subscriber_id', 'list_id');
}
} }

View 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');
}
}

View File

@ -9,10 +9,12 @@ $console->writeln('Cleaning up database...');
$models = array( $models = array(
"Subscriber", "Subscriber",
"Setting", "Setting",
"Newsletter" "Newsletter",
"SubscriberList",
"PivotSubscriberList"
); );
$destroy = function ($model) { $destroy = function ($model) {
Model::factory("\MailPoet\Models\\" . $model) Model::factory("\MailPoet\Models\\" . $model)
->delete_many(); ->delete_many();
}; };
array_map($destroy, $models); array_map($destroy, $models);

View File

@ -1,5 +1,7 @@
<?php <?php
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberList;
use MailPoet\Models\PivotSubscriberList;
class SubscriberCest { class SubscriberCest {
@ -7,8 +9,8 @@ class SubscriberCest {
$this->before_time = time(); $this->before_time = time();
$this->data = array( $this->data = array(
'first_name' => 'John', 'first_name' => 'John',
'last_name' => 'Mailer', 'last_name' => 'Mailer',
'email' => 'john@mailpoet.com' 'email' => 'john@mailpoet.com'
); );
$this->subscriber = Subscriber::create(); $this->subscriber = Subscriber::create();
@ -23,7 +25,7 @@ class SubscriberCest {
function itHasAFirstName() { function itHasAFirstName() {
$subscriber = $subscriber =
Subscriber::where('email', $this->data['email']) Subscriber::where('email', $this->data['email'])
->findOne(); ->findOne();
expect($subscriber->first_name) expect($subscriber->first_name)
->equals($this->data['first_name']); ->equals($this->data['first_name']);
} }
@ -31,7 +33,7 @@ class SubscriberCest {
function itHasALastName() { function itHasALastName() {
$subscriber = $subscriber =
Subscriber::where('email', $this->data['email']) Subscriber::where('email', $this->data['email'])
->findOne(); ->findOne();
expect($subscriber->last_name) expect($subscriber->last_name)
->equals($this->data['last_name']); ->equals($this->data['last_name']);
} }
@ -39,7 +41,7 @@ class SubscriberCest {
function itHasAnEmail() { function itHasAnEmail() {
$subscriber = $subscriber =
Subscriber::where('email', $this->data['email']) Subscriber::where('email', $this->data['email'])
->findOne(); ->findOne();
expect($subscriber->email) expect($subscriber->email)
->equals($this->data['email']); ->equals($this->data['email']);
} }
@ -51,8 +53,31 @@ class SubscriberCest {
expect($saved)->equals(false); 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() { function _after() {
ORM::for_table(Subscriber::$_table) ORM::for_table(Subscriber::$_table)
->delete_many(); ->delete_many();
ORM::for_table(SubscriberList::$_table)
->delete_many();
ORM::for_table(PivotSubscriberList::$_table)
->delete_many();
} }
} }

View 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();
}
}