Unit tests for listing specific methods
- small fixes/optimization in Subscriber model - added test for search and group in Subscriber - added test for search in Newsletter
This commit is contained in:
@ -16,6 +16,10 @@ class Subscriber extends Model {
|
||||
}
|
||||
|
||||
static function search($orm, $search = '') {
|
||||
if(strlen(trim($search) === 0)) {
|
||||
return $orm;
|
||||
}
|
||||
|
||||
return $orm->where_raw(
|
||||
'(`email` LIKE ? OR `first_name` LIKE ? OR `last_name` LIKE ?)',
|
||||
array('%'.$search.'%', '%'.$search.'%', '%'.$search.'%')
|
||||
@ -48,8 +52,13 @@ class Subscriber extends Model {
|
||||
}
|
||||
|
||||
static function group($orm, $group = null) {
|
||||
if(in_array($group, array('subscribed', 'unconfirmed', 'unsubscribed'))) {
|
||||
if($group === null or !in_array(
|
||||
$group,
|
||||
array('subscribed', 'unconfirmed', 'unsubscribed')
|
||||
)) {
|
||||
return $orm;
|
||||
}
|
||||
|
||||
return $orm->where('status', $group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,11 @@ class NewsletterCest {
|
||||
expect($this->saved)->equals(true);
|
||||
}
|
||||
|
||||
function itHasASearchFilter() {
|
||||
$newsletter = Newsletter::filter('search', 'first')->findOne();
|
||||
expect($newsletter->subject)->equals($this->data['subject']);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::for_table(Newsletter::$_table)
|
||||
->delete_many();
|
||||
|
@ -4,16 +4,29 @@ use MailPoet\Models\Subscriber;
|
||||
class SubscriberCest {
|
||||
|
||||
function _before() {
|
||||
$this->before_time = time();
|
||||
$this->data = array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
'email' => 'jo@mailpoet.com'
|
||||
);
|
||||
|
||||
$this->subscriber = Subscriber::create();
|
||||
$this->subscriber->hydrate($this->data);
|
||||
$this->saved = $this->subscriber->save();
|
||||
|
||||
$subscribed = Subscriber::create();
|
||||
$subscribed->hydrate(array(
|
||||
'email' => 'marco@mailpoet.com',
|
||||
'status' => 'subscribed'
|
||||
));
|
||||
$subscribed->save();
|
||||
|
||||
$unsubscribed = Subscriber::create();
|
||||
$unsubscribed->hydrate(array(
|
||||
'email' => 'marco@mailpoet.com',
|
||||
'status' => 'unsubscribed'
|
||||
));
|
||||
$unsubscribed->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
@ -64,6 +77,34 @@ class SubscriberCest {
|
||||
expect($subscriber_updated->status)->equals('subscribed');
|
||||
}
|
||||
|
||||
function itHasASearchFilter() {
|
||||
$subscriber = Subscriber::filter('search', 'john')->findOne();
|
||||
expect($subscriber->first_name)->equals($this->data['first_name']);
|
||||
|
||||
$subscriber = Subscriber::filter('search', 'mailer')->findOne();
|
||||
expect($subscriber->last_name)->equals($this->data['last_name']);
|
||||
|
||||
$subscriber = Subscriber::filter('search', 'mailpoet')->findOne();
|
||||
expect($subscriber->email)->equals($this->data['email']);
|
||||
}
|
||||
|
||||
function itHasAGroupFilter() {
|
||||
$subscribers = Subscriber::filter('group', 'unconfirmed')->findMany();
|
||||
foreach($subscribers as $subscriber) {
|
||||
expect($subscriber->status)->equals('unconfirmed');
|
||||
}
|
||||
|
||||
$subscribers = Subscriber::filter('group', 'subscribed')->findMany();
|
||||
foreach($subscribers as $subscriber) {
|
||||
expect($subscriber->status)->equals('subscribed');
|
||||
}
|
||||
|
||||
$subscribers = Subscriber::filter('group', 'unsubscribed')->findMany();
|
||||
foreach($subscribers as $subscriber) {
|
||||
expect($subscriber->status)->equals('unsubscribed');
|
||||
}
|
||||
}
|
||||
|
||||
function emailMustBeUnique() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->hydrate($this->data);
|
||||
|
Reference in New Issue
Block a user