- Renamed Subscriber and RelationSubscriberList models to SList and

SubscriberList, respectively
This commit is contained in:
MrCasual
2015-09-01 19:49:50 -04:00
parent f6f8f1390f
commit 78a2a50af7
9 changed files with 67 additions and 67 deletions

View File

@ -40,7 +40,7 @@ class Initializer {
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_LISTS_TABLE', $lists);
define('MP_PIVOT_SUBSCRIBER_LIST_TABLE', $subscriber_list); define('MP_SUBSCRIBER_LIST_TABLE', $subscriber_list);
} }
function setupActivator() { function setupActivator() {

View File

@ -14,7 +14,7 @@ class Migrator {
'settings', 'settings',
'newsletters', 'newsletters',
'lists', 'lists',
'pivot_subscriber_list' 'subscriber_list'
); );
} }
@ -91,7 +91,7 @@ class Migrator {
return $this->sqlify(__FUNCTION__, $attributes); return $this->sqlify(__FUNCTION__, $attributes);
} }
function pivot_subscriber_list() { function subscriber_list() {
$attributes = array( $attributes = array(
'id mediumint(9) NOT NULL AUTO_INCREMENT,', 'id mediumint(9) NOT NULL AUTO_INCREMENT,',
'subscriber_id mediumint(9) NOT NULL,', 'subscriber_id mediumint(9) NOT NULL,',

View File

@ -1,12 +0,0 @@
<?php
namespace MailPoet\Models;
if(!defined('ABSPATH')) exit;
class RelationSubscriberList extends Model {
public static $_table = MP_PIVOT_SUBSCRIBER_LIST_TABLE;
function __construct() {
parent::__construct();
}
}

35
lib/Models/SList.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace MailPoet\Models;
if(!defined('ABSPATH')) exit;
class SList 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__ . '\SubscriberList', 'list_id', 'subscriber_id');
}
}

View File

@ -16,6 +16,6 @@ class Subscriber extends Model {
} }
public function lists() { public function lists() {
return self::has_many_through(__NAMESPACE__ . '\SubscriberList', __NAMESPACE__ . '\RelationSubscriberList', 'subscriber_id', 'list_id'); return self::has_many_through(__NAMESPACE__ . '\SList', __NAMESPACE__ . '\SubscriberList', 'subscriber_id', 'list_id');
} }
} }

View File

@ -4,32 +4,9 @@ namespace MailPoet\Models;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class SubscriberList extends Model { class SubscriberList extends Model {
public static $_table = MP_LISTS_TABLE; public static $_table = MP_SUBSCRIBER_LIST_TABLE;
function __construct() { function __construct() {
parent::__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__ . '\RelationSubscriberList', 'list_id', 'subscriber_id');
} }
} }

View File

@ -7,14 +7,14 @@ require_once(getenv('WP_TEST_PATH') . '/wp-load.php');
$console->writeln('Cleaning up database...'); $console->writeln('Cleaning up database...');
$models = array( $models = array(
"Subscriber", 'Subscriber',
"Setting", 'Setting',
"Newsletter", 'Newsletter',
"SubscriberList", 'SList',
"RelationSubscriberList" 'SubscriberList'
); );
$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,7 +1,7 @@
<?php <?php
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Models\SList;
use MailPoet\Models\SubscriberList; use MailPoet\Models\SubscriberList;
use MailPoet\Models\RelationSubscriberList;
class SubscriberCest { class SubscriberCest {
@ -58,10 +58,10 @@ class SubscriberCest {
'name' => 'some name' 'name' => 'some name'
); );
$list = SubscriberList::create(); $list = SList::create();
$list->hydrate($listData); $list->hydrate($listData);
$list->save(); $list->save();
$association = RelationSubscriberList::create(); $association = SubscriberList::create();
$association->subscriber_id = $this->subscriber->id; $association->subscriber_id = $this->subscriber->id;
$association->list_id = $list->id; $association->list_id = $list->id;
$association->save(); $association->save();
@ -75,9 +75,9 @@ class SubscriberCest {
function _after() { function _after() {
ORM::for_table(Subscriber::$_table) ORM::for_table(Subscriber::$_table)
->delete_many(); ->delete_many();
ORM::for_table(SList::$_table)
->delete_many();
ORM::for_table(SubscriberList::$_table) ORM::for_table(SubscriberList::$_table)
->delete_many(); ->delete_many();
ORM::for_table(RelationSubscriberList::$_table)
->delete_many();
} }
} }

View File

@ -1,17 +1,17 @@
<?php <?php
use MailPoet\Models\RelationSubscriberList;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberList; use MailPoet\Models\SubscriberList;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SList;
class SubscriberListCest { class SListCest {
function _before() { function _before() {
$this->before_time = time(); $this->before_time = time();
$this->data = array( $this->data = array(
'name' => 'some name', 'name' => 'some name',
); );
$this->list = SubscriberList::create(); $this->list = SList::create();
$this->list->hydrate($this->data); $this->list->hydrate($this->data);
$this->saved = $this->list->save(); $this->saved = $this->list->save();
} }
@ -22,28 +22,28 @@ class SubscriberListCest {
function itHasToBeValid() { function itHasToBeValid() {
expect($this->saved)->equals(true); expect($this->saved)->equals(true);
$empty_model = SubscriberList::create(); $empty_model = SList::create();
expect($empty_model->save())->equals(false); expect($empty_model->save())->equals(false);
$validations = $empty_model->getValidationErrors(); $validations = $empty_model->getValidationErrors();
expect(count($validations))->equals(2); expect(count($validations))->equals(2);
} }
function itHasACreatedAtOnCreation() { function itHasACreatedAtOnCreation() {
$list = SubscriberList::where('name', $this->data['name']) $list = SList::where('name', $this->data['name'])
->findOne(); ->findOne();
$time_difference = strtotime($list->created_at) >= $this->before_time; $time_difference = strtotime($list->created_at) >= $this->before_time;
expect($time_difference)->equals(true); expect($time_difference)->equals(true);
} }
function itHasAnUpdatedAtOnCreation() { function itHasAnUpdatedAtOnCreation() {
$list = SubscriberList::where('name', $this->data['name']) $list = SList::where('name', $this->data['name'])
->findOne(); ->findOne();
$time_difference = strtotime($list->updated_at) >= $this->before_time; $time_difference = strtotime($list->updated_at) >= $this->before_time;
expect($time_difference)->equals(true); expect($time_difference)->equals(true);
} }
function itKeepsTheCreatedAtOnUpdate() { function itKeepsTheCreatedAtOnUpdate() {
$list = SubscriberList::where('name', $this->data['name']) $list = SList::where('name', $this->data['name'])
->findOne(); ->findOne();
$old_created_at = $list->created_at; $old_created_at = $list->created_at;
$list->name = 'new name'; $list->name = 'new name';
@ -52,7 +52,7 @@ class SubscriberListCest {
} }
function itUpdatesTheUpdatedAtOnUpdate() { function itUpdatesTheUpdatedAtOnUpdate() {
$list = SubscriberList::where('name', $this->data['name']) $list = SList::where('name', $this->data['name'])
->findOne(); ->findOne();
$update_time = time(); $update_time = time();
$list->name = 'new name'; $list->name = 'new name';
@ -65,15 +65,15 @@ class SubscriberListCest {
$data = array( $data = array(
'name' => 'some other new name' 'name' => 'some other new name'
); );
$createNewRecord = SubscriberList::createOrUpdate($data); $createNewRecord = SList::createOrUpdate($data);
$data = array( $data = array(
'name' => $this->data['name'], 'name' => $this->data['name'],
'name_updated' => 'updated name', 'name_updated' => 'updated name',
); );
$updateExistingRecord = SubscriberList::createOrUpdate($data); $updateExistingRecord = SList::createOrUpdate($data);
$allRecords = SubscriberList::find_array(); $allRecords = SList::find_array();
expect(count($allRecords))->equals(2); expect(count($allRecords))->equals(2);
expect($allRecords[0]['name'])->equals($data['name_updated']); expect($allRecords[0]['name'])->equals($data['name_updated']);
} }
@ -95,24 +95,24 @@ class SubscriberListCest {
$subscriber = Subscriber::create(); $subscriber = Subscriber::create();
$subscriber->hydrate($subscriberData); $subscriber->hydrate($subscriberData);
$subscriber->save(); $subscriber->save();
$association = RelationSubscriberList::create(); $association = SubscriberList::create();
$association->subscriber_id = $subscriber->id; $association->subscriber_id = $subscriber->id;
$association->list_id = $this->list->id; $association->list_id = $this->list->id;
$association->save(); $association->save();
} }
$list = SubscriberList::find_one($this->list->id); $list = SList::find_one($this->list->id);
$subscribers = $list->subscribers() $subscribers = $list->subscribers()
->find_array(); ->find_array();
expect(count($subscribers))->equals(2); expect(count($subscribers))->equals(2);
} }
function _after() { function _after() {
ORM::for_table(SubscriberList::$_table) ORM::for_table(SList::$_table)
->delete_many(); ->delete_many();
ORM::for_table(Subscriber::$_table) ORM::for_table(Subscriber::$_table)
->delete_many(); ->delete_many();
ORM::for_table(RelationSubscriberList::$_table) ORM::for_table(SubscriberList::$_table)
->delete_many(); ->delete_many();
} }