Reverted back to Model class
This commit is contained in:
@ -1,90 +1,90 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
|
||||
class Migrator {
|
||||
function __construct() {
|
||||
$this->prefix = Env::$db_prefix;
|
||||
$this->charset = Env::$db_charset;
|
||||
$this->models = array(
|
||||
'subscribers',
|
||||
'settings',
|
||||
'newsletters'
|
||||
);
|
||||
}
|
||||
|
||||
function up() {
|
||||
global $wpdb;
|
||||
|
||||
$_this = $this;
|
||||
$migrate = function($model) use($_this) {
|
||||
dbDelta($_this->$model());
|
||||
};
|
||||
|
||||
array_map($migrate, $this->models);
|
||||
}
|
||||
|
||||
function down() {
|
||||
global $wpdb;
|
||||
|
||||
$drop_table = function($model) {
|
||||
$table = $this->prefix . $model;
|
||||
$wpdb->query("DROP TABLE {$table}");
|
||||
};
|
||||
|
||||
array_map($drop_table, $this->models);
|
||||
}
|
||||
|
||||
function subscribers() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'first_name tinytext NOT NULL,',
|
||||
'last_name tinytext NOT NULL,',
|
||||
'email varchar(150) 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 email (email)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function settings() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(20) NOT NULL,',
|
||||
'value varchar(255) 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 newsletters() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subject varchar(250) NOT NULL,',
|
||||
'body longtext,',
|
||||
'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;
|
||||
|
||||
$sql = array();
|
||||
$sql[] = "CREATE TABLE " . $table . " (";
|
||||
$sql = array_merge($sql, $attributes);
|
||||
$sql[] = ") " . $this->charset . ";";
|
||||
|
||||
return implode("\n", $sql);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
|
||||
class Migrator {
|
||||
function __construct() {
|
||||
$this->prefix = Env::$db_prefix;
|
||||
$this->charset = Env::$db_charset;
|
||||
$this->models = array(
|
||||
'subscribers',
|
||||
'settings',
|
||||
'newsletters'
|
||||
);
|
||||
}
|
||||
|
||||
function up() {
|
||||
global $wpdb;
|
||||
|
||||
$_this = $this;
|
||||
$migrate = function($model) use($_this) {
|
||||
dbDelta($_this->$model());
|
||||
};
|
||||
|
||||
array_map($migrate, $this->models);
|
||||
}
|
||||
|
||||
function down() {
|
||||
global $wpdb;
|
||||
|
||||
$drop_table = function($model) {
|
||||
$table = $this->prefix . $model;
|
||||
$wpdb->query("DROP TABLE {$table}");
|
||||
};
|
||||
|
||||
array_map($drop_table, $this->models);
|
||||
}
|
||||
|
||||
function subscribers() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'first_name tinytext NOT NULL,',
|
||||
'last_name tinytext NOT NULL,',
|
||||
'email varchar(150) 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 email (email)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function settings() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'name varchar(20) NOT NULL,',
|
||||
'value varchar(255) 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 newsletters() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'subject varchar(250) NOT NULL,',
|
||||
'body longtext,',
|
||||
'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;
|
||||
|
||||
$sql = array();
|
||||
$sql[] = "CREATE TABLE " . $table . " (";
|
||||
$sql = array_merge($sql, $attributes);
|
||||
$sql[] = ") " . $this->charset . ";";
|
||||
|
||||
return implode("\n", $sql);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ namespace MailPoet\Models;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class BaseModel extends \Sudzy\ValidModel {
|
||||
class Model extends \Sudzy\ValidModel {
|
||||
function __construct() {
|
||||
$customValidators = new CustomValidator();
|
||||
parent::__construct($customValidators->init());
|
@ -3,7 +3,7 @@ namespace MailPoet\Models;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Newsletter extends BaseModel {
|
||||
class Newsletter extends Model {
|
||||
public static $_table = MP_NEWSLETTERS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
|
@ -1,23 +1,23 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Setting extends BaseModel {
|
||||
public static $_table = MP_SETTINGS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations("name", array(
|
||||
"required" => "name_is_blank",
|
||||
"isString" => "name_is_not_string",
|
||||
"minLength|2" => "name_is_short"
|
||||
));
|
||||
$this->addValidations("value", array(
|
||||
"required" => "value_is_blank",
|
||||
"isString" => "value_is_not_string",
|
||||
"minLength|2" => "value_is_short"
|
||||
));
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Setting extends Model {
|
||||
public static $_table = MP_SETTINGS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->addValidations("name", array(
|
||||
"required" => "name_is_blank",
|
||||
"isString" => "name_is_not_string",
|
||||
"minLength|2" => "name_is_short"
|
||||
));
|
||||
$this->addValidations("value", array(
|
||||
"required" => "value_is_blank",
|
||||
"isString" => "value_is_not_string",
|
||||
"minLength|2" => "value_is_short"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,23 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Subscriber extends BaseModel {
|
||||
public static $_table = MP_SUBSCRIBERS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
$this->addValidations('email', array(
|
||||
'required' => "email_is_blank",
|
||||
'isEmail' => "email_is_invalid"
|
||||
));
|
||||
$this->addValidations('first_name', array(
|
||||
'required' => "first_name_is_blank",
|
||||
'minLength|2' => "first_name_is_short"
|
||||
));
|
||||
$this->addValidations('last_name', array(
|
||||
'required' => "last_name_is_blank",
|
||||
'minLength|2' => "last_name_is_short"
|
||||
));
|
||||
}
|
||||
}
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Subscriber extends Model {
|
||||
public static $_table = MP_SUBSCRIBERS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
$this->addValidations('email', array(
|
||||
'required' => "email_is_blank",
|
||||
'isEmail' => "email_is_invalid"
|
||||
));
|
||||
$this->addValidations('first_name', array(
|
||||
'required' => "first_name_is_blank",
|
||||
'minLength|2' => "first_name_is_short"
|
||||
));
|
||||
$this->addValidations('last_name', array(
|
||||
'required' => "last_name_is_blank",
|
||||
'minLength|2' => "last_name_is_short"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
$console = new \Codeception\Lib\Console\Output([]);
|
||||
|
||||
$console->writeln('Loading WP core...');
|
||||
require_once(getenv('WP_TEST_PATH') . '/wp-load.php');
|
||||
|
||||
$console->writeln('Cleaning up database...');
|
||||
$models = array(
|
||||
"Subscriber",
|
||||
"Setting",
|
||||
"Newsletter"
|
||||
);
|
||||
$destroy = function ($model) {
|
||||
Model::factory("\MailPoet\Models\\" . $model)
|
||||
->delete_many();
|
||||
};
|
||||
array_map($destroy, $models);
|
||||
<?php
|
||||
|
||||
$console = new \Codeception\Lib\Console\Output([]);
|
||||
|
||||
$console->writeln('Loading WP core...');
|
||||
require_once(getenv('WP_TEST_PATH') . '/wp-load.php');
|
||||
|
||||
$console->writeln('Cleaning up database...');
|
||||
$models = array(
|
||||
"Subscriber",
|
||||
"Setting",
|
||||
"Newsletter"
|
||||
);
|
||||
$destroy = function ($model) {
|
||||
Model::factory("\MailPoet\Models\\" . $model)
|
||||
->delete_many();
|
||||
};
|
||||
array_map($destroy, $models);
|
||||
|
@ -1,90 +1,90 @@
|
||||
<?php
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
class SettingCest {
|
||||
|
||||
function _before() {
|
||||
$this->before_time = time();
|
||||
$this->data = array(
|
||||
'name' => 'sending_method',
|
||||
'value' => 'smtp'
|
||||
);
|
||||
|
||||
$setting = Setting::create();
|
||||
$setting->hydrate($this->data);
|
||||
$setting->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
expect($setting->id)->notNull();
|
||||
}
|
||||
|
||||
function nameShouldValidate() {
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', '');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_blank');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', 31337);
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_not_string');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', 'a');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_short');
|
||||
}
|
||||
|
||||
function valueShouldValidate() {
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('value', '');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_blank');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('value', 31337);
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_not_string');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('value', 'a');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_short');
|
||||
}
|
||||
|
||||
function itHasACreatedAtOnCreation() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($setting->created_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itHasAnUpdatedAtOnCreation() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($setting->updated_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itKeepsTheCreatedAtOnUpdate() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$old_created_at = $setting->created_at;
|
||||
$setting->value = 'http_api';
|
||||
$setting->save();
|
||||
expect($old_created_at)->equals($setting->created_at);
|
||||
}
|
||||
|
||||
function itUpdatesTheUpdatedAtOnUpdate() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$update_time = time();
|
||||
$setting->value = 'http_api';
|
||||
$setting->save();
|
||||
$time_difference = strtotime($setting->updated_at) >= $update_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne()
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
<?php
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
class SettingCest {
|
||||
|
||||
function _before() {
|
||||
$this->before_time = time();
|
||||
$this->data = array(
|
||||
'name' => 'sending_method',
|
||||
'value' => 'smtp'
|
||||
);
|
||||
|
||||
$setting = Setting::create();
|
||||
$setting->hydrate($this->data);
|
||||
$setting->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
expect($setting->id)->notNull();
|
||||
}
|
||||
|
||||
function nameShouldValidate() {
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', '');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_blank');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', 31337);
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_not_string');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('name', 'a');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('name_is_short');
|
||||
}
|
||||
|
||||
function valueShouldValidate() {
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('value', '');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_blank');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('value', 31337);
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_not_string');
|
||||
|
||||
$conflict_setting = Setting::create();
|
||||
$conflict_setting->validateField('value', 'a');
|
||||
expect($conflict_setting->getValidationErrors()[0])->equals('value_is_short');
|
||||
}
|
||||
|
||||
function itHasACreatedAtOnCreation() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($setting->created_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itHasAnUpdatedAtOnCreation() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($setting->updated_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itKeepsTheCreatedAtOnUpdate() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$old_created_at = $setting->created_at;
|
||||
$setting->value = 'http_api';
|
||||
$setting->save();
|
||||
expect($old_created_at)->equals($setting->created_at);
|
||||
}
|
||||
|
||||
function itUpdatesTheUpdatedAtOnUpdate() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
$update_time = time();
|
||||
$setting->value = 'http_api';
|
||||
$setting->save();
|
||||
$time_difference = strtotime($setting->updated_at) >= $update_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
$setting = Setting::where('name', $this->data['name'])
|
||||
->findOne()
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
|
@ -1,131 +1,131 @@
|
||||
<?php
|
||||
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'
|
||||
);
|
||||
|
||||
$this->subscriber = Subscriber::create();
|
||||
$this->subscriber->hydrate($this->data);
|
||||
$this->subscriber->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->id)->notNull();
|
||||
}
|
||||
|
||||
function itHasAFirstName() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->first_name)->equals($this->data['first_name']);
|
||||
}
|
||||
|
||||
function itHasALastName() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->last_name)->equals($this->data['last_name']);
|
||||
}
|
||||
|
||||
function itHasAnEmail() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->email)->equals($this->data['email']);
|
||||
}
|
||||
|
||||
function emailMustBeUnique() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->hydrate($this->data);
|
||||
$conflicted = false;
|
||||
try {
|
||||
$conflict_subscriber->save();
|
||||
} catch (Exception $e) {
|
||||
$conflicted = true;
|
||||
}
|
||||
expect($conflicted)->equals(true);
|
||||
}
|
||||
|
||||
function emailShouldValidate() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('email', '');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('email_is_blank');
|
||||
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('email', 'some @ email . com');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('email_is_invalid');
|
||||
}
|
||||
|
||||
function firstNameShouldValidate() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('first_name', '');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('first_name_is_blank');
|
||||
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('first_name', 'a');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('first_name_is_short');
|
||||
}
|
||||
|
||||
|
||||
function lastNameShouldValidate() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('last_name', '');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('last_name_is_blank');
|
||||
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('last_name', 'a');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('last_name_is_short');
|
||||
}
|
||||
|
||||
function itHasACreatedAtOnCreation() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($subscriber->created_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itHasAnUpdatedAtOnCreation() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($subscriber->updated_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itKeepsTheCreatedAtOnUpdate() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
|
||||
$old_created_at = $subscriber->created_at;
|
||||
|
||||
$subscriber->first_name = 'New Name';
|
||||
$subscriber->save();
|
||||
|
||||
expect($old_created_at)->equals($subscriber->created_at);
|
||||
}
|
||||
|
||||
function itUpdatesTheUpdatedAtOnUpdate() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
|
||||
$update_time = time();
|
||||
$subscriber->first_name = 'New Name';
|
||||
$subscriber->save();
|
||||
|
||||
$time_difference = strtotime($subscriber->updated_at) >= $update_time;
|
||||
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne()
|
||||
->delete();
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
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'
|
||||
);
|
||||
|
||||
$this->subscriber = Subscriber::create();
|
||||
$this->subscriber->hydrate($this->data);
|
||||
$this->subscriber->save();
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->id)->notNull();
|
||||
}
|
||||
|
||||
function itHasAFirstName() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->first_name)->equals($this->data['first_name']);
|
||||
}
|
||||
|
||||
function itHasALastName() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->last_name)->equals($this->data['last_name']);
|
||||
}
|
||||
|
||||
function itHasAnEmail() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
expect($subscriber->email)->equals($this->data['email']);
|
||||
}
|
||||
|
||||
function emailMustBeUnique() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->hydrate($this->data);
|
||||
$conflicted = false;
|
||||
try {
|
||||
$conflict_subscriber->save();
|
||||
} catch (Exception $e) {
|
||||
$conflicted = true;
|
||||
}
|
||||
expect($conflicted)->equals(true);
|
||||
}
|
||||
|
||||
function emailShouldValidate() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('email', '');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('email_is_blank');
|
||||
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('email', 'some @ email . com');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('email_is_invalid');
|
||||
}
|
||||
|
||||
function firstNameShouldValidate() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('first_name', '');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('first_name_is_blank');
|
||||
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('first_name', 'a');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('first_name_is_short');
|
||||
}
|
||||
|
||||
|
||||
function lastNameShouldValidate() {
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('last_name', '');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('last_name_is_blank');
|
||||
|
||||
$conflict_subscriber = Subscriber::create();
|
||||
$conflict_subscriber->validateField('last_name', 'a');
|
||||
expect($conflict_subscriber->getValidationErrors()[0])->equals('last_name_is_short');
|
||||
}
|
||||
|
||||
function itHasACreatedAtOnCreation() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($subscriber->created_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itHasAnUpdatedAtOnCreation() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
$time_difference = strtotime($subscriber->updated_at) >= $this->before_time;
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function itKeepsTheCreatedAtOnUpdate() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
|
||||
$old_created_at = $subscriber->created_at;
|
||||
|
||||
$subscriber->first_name = 'New Name';
|
||||
$subscriber->save();
|
||||
|
||||
expect($old_created_at)->equals($subscriber->created_at);
|
||||
}
|
||||
|
||||
function itUpdatesTheUpdatedAtOnUpdate() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
|
||||
$update_time = time();
|
||||
$subscriber->first_name = 'New Name';
|
||||
$subscriber->save();
|
||||
|
||||
$time_difference = strtotime($subscriber->updated_at) >= $update_time;
|
||||
|
||||
expect($time_difference)->equals(true);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
$subscriber = Subscriber::where('email', $this->data['email'])
|
||||
->findOne()
|
||||
->delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
<?php
|
||||
|
||||
class UtilCSSCest {
|
||||
public function _before() {
|
||||
$this->css = new \MailPoet\Util\CSS();
|
||||
}
|
||||
|
||||
// tests
|
||||
public function itCanBeInstantiated() {
|
||||
expect_that($this->css instanceof \MailPoet\Util\CSS);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
class UtilCSSCest {
|
||||
public function _before() {
|
||||
$this->css = new \MailPoet\Util\CSS();
|
||||
}
|
||||
|
||||
// tests
|
||||
public function itCanBeInstantiated() {
|
||||
expect_that($this->css instanceof \MailPoet\Util\CSS);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user