Made changes as per Marco's comments

This commit is contained in:
MrCasual
2015-09-02 17:28:49 -04:00
parent e2775981af
commit c804e44961
6 changed files with 36 additions and 36 deletions

View File

@ -33,13 +33,13 @@ class Initializer {
$subscribers = Env::$db_prefix . 'subscribers';
$settings = Env::$db_prefix . 'settings';
$newsletters = Env::$db_prefix . 'newsletters';
$lists = Env::$db_prefix . 'lists';
$segments = Env::$db_prefix . 'segments';
$subscriber_segment = Env::$db_prefix . 'subscriber_segment';
define('MP_SUBSCRIBERS_TABLE', $subscribers);
define('MP_SETTINGS_TABLE', $settings);
define('MP_NEWSLETTERS_TABLE', $newsletters);
define('MP_LISTS_TABLE', $lists);
define('MP_SEGMENTS_TABLE', $segments);
define('MP_SUBSCRIBER_SEGMENT_TABLE', $subscriber_segment);
}

View File

@ -13,7 +13,7 @@ class Migrator {
'subscribers',
'settings',
'newsletters',
'lists',
'segments',
'subscriber_segment'
);
}
@ -79,7 +79,7 @@ class Migrator {
return $this->sqlify(__FUNCTION__, $attributes);
}
function lists() {
function segments() {
$attributes = array(
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
'name varchar(90) NOT NULL,',
@ -95,7 +95,7 @@ class Migrator {
$attributes = array(
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
'subscriber_id mediumint(9) NOT NULL,',
'list_id mediumint(9) NOT NULL,',
'segment_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)'

View File

@ -4,7 +4,7 @@ namespace MailPoet\Models;
if(!defined('ABSPATH')) exit;
class Segment extends Model {
public static $_table = MP_LISTS_TABLE;
public static $_table = MP_SEGMENTS_TABLE;
function __construct() {
parent::__construct();
@ -30,6 +30,6 @@ class Segment extends Model {
}
public function subscribers() {
return self::has_many_through(__NAMESPACE__ . '\Subscriber', __NAMESPACE__ . '\SubscriberSegment', 'list_id', 'subscriber_id');
return $this->has_many_through(__NAMESPACE__ . '\Subscriber', __NAMESPACE__ . '\SubscriberSegment', 'segment_id', 'subscriber_id');
}
}

View File

@ -15,7 +15,7 @@ class Subscriber extends Model {
));
}
public function lists() {
return self::has_many_through(__NAMESPACE__ . '\Segment', __NAMESPACE__ . '\SubscriberSegment', 'subscriber_id', 'list_id');
public function segments() {
return $this->has_many_through(__NAMESPACE__ . '\Segment', __NAMESPACE__ . '\SubscriberSegment', 'subscriber_id', 'segment_id');
}
}

View File

@ -11,9 +11,9 @@ class SegmentCest {
'name' => 'some name',
);
$this->list = Segment::create();
$this->list->hydrate($this->data);
$this->saved = $this->list->save();
$this->segment = Segment::create();
$this->segment->hydrate($this->data);
$this->saved = $this->segment->save();
}
function itCanBeCreated() {
@ -29,35 +29,35 @@ class SegmentCest {
}
function itHasACreatedAtOnCreation() {
$list = Segment::where('name', $this->data['name'])
$segment = Segment::where('name', $this->data['name'])
->findOne();
$time_difference = strtotime($list->created_at) >= $this->before_time;
$time_difference = strtotime($segment->created_at) >= $this->before_time;
expect($time_difference)->equals(true);
}
function itHasAnUpdatedAtOnCreation() {
$list = Segment::where('name', $this->data['name'])
$segment = Segment::where('name', $this->data['name'])
->findOne();
$time_difference = strtotime($list->updated_at) >= $this->before_time;
$time_difference = strtotime($segment->updated_at) >= $this->before_time;
expect($time_difference)->equals(true);
}
function itKeepsTheCreatedAtOnUpdate() {
$list = Segment::where('name', $this->data['name'])
$segment = Segment::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);
$old_created_at = $segment->created_at;
$segment->name = 'new name';
$segment->save();
expect($old_created_at)->equals($segment->created_at);
}
function itUpdatesTheUpdatedAtOnUpdate() {
$list = Segment::where('name', $this->data['name'])
$segment = Segment::where('name', $this->data['name'])
->findOne();
$update_time = time();
$list->name = 'new name';
$list->save();
$time_difference = strtotime($list->updated_at) >= $update_time;
$segment->name = 'new name';
$segment->save();
$time_difference = strtotime($segment->updated_at) >= $update_time;
expect($time_difference)->equals(true);
}
@ -97,12 +97,12 @@ class SegmentCest {
$subscriber->save();
$association = SubscriberSegment::create();
$association->subscriber_id = $subscriber->id;
$association->list_id = $this->list->id;
$association->segment_id = $this->segment->id;
$association->save();
}
$list = Segment::find_one($this->list->id);
$subscribers = $list->subscribers()
$segment = Segment::find_one($this->segment->id);
$subscribers = $segment->subscribers()
->find_array();
expect(count($subscribers))->equals(2);
}

View File

@ -53,23 +53,23 @@ class SubscriberCest {
expect($saved)->equals(false);
}
function itCanHaveAList() {
$listData = array(
function itCanHaveASegment() {
$segmentData = array(
'name' => 'some name'
);
$list = Segment::create();
$list->hydrate($listData);
$list->save();
$segment = Segment::create();
$segment->hydrate($segmentData);
$segment->save();
$association = SubscriberSegment::create();
$association->subscriber_id = $this->subscriber->id;
$association->list_id = $list->id;
$association->segment_id = $segment->id;
$association->save();
$subscriber = Subscriber::find_one($this->subscriber->id);
$subscriberList = $subscriber->lists()
$subscriberSegment = $subscriber->segments()
->find_one();
expect($subscriberList->id)->equals($list->id);
expect($subscriberSegment->id)->equals($segment->id);
}
function _after() {