Models unit tests update

This commit is contained in:
Jonathan Labreuille
2016-06-27 13:53:56 +02:00
parent 264b7e180b
commit 306cdeb68f
5 changed files with 109 additions and 3 deletions

View File

@ -59,8 +59,6 @@ class Model extends \Sudzy\ValidModel {
default:
$this->setError($e->getMessage());
}
} catch(\Exception $e) {
$this->setError($e->getMessage());
}
return $this;
}

View File

@ -34,6 +34,19 @@ class CustomFieldTest extends MailPoetTest {
expect($this->custom_field->getErrors())->false();
}
function testItCanBeUpdated() {
expect($this->custom_field->name)->equals($this->data['name']);
$updated_custom_field = CustomField::createOrUpdate(array(
'id' => $this->custom_field->id,
'name' => 'Country'
));
expect($updated_custom_field->getErrors())->false();
expect($updated_custom_field->name)->equals('Country');
expect($updated_custom_field->id)->equals($this->custom_field->id);
}
function testItHasAName() {
expect($this->custom_field->name)->equals($this->data['name']);
}

View File

@ -25,6 +25,15 @@ class SettingTest extends MailPoetTest {
expect($default_settings['signup_confirmation']['enabled'])->true();
}
function testItCanLoadDefaults() {
Setting::$defaults = null;
expect(Setting::$defaults)->null();
$default_settings = Setting::getDefaults();
expect(Setting::$defaults)->notEmpty();
expect($default_settings['signup_confirmation']['enabled'])->true();
}
function testItCanGetAllSettingsIncludingDefaults() {
Setting::setValue('key_1', 'value_1');
Setting::setValue('key_2', 'value_2');

View File

@ -28,6 +28,14 @@ class StatisticsFormsTest extends MailPoetTest {
expect(StatisticsForms::count())->equals(3);
}
function testItCannotRecordStatsWithoutFormOrSubscriber() {
$record = StatisticsForms::record($form_id = null, $subscriber_id = 1);
expect($record)->false();
$record = StatisticsForms::record($form_id = 1, $subscriber_id = null);
expect($record)->false();
}
function _after() {
StatisticsForms::deleteMany();
}

View File

@ -48,6 +48,27 @@ class SubscriberTest extends MailPoetTest {
->equals($this->data['email']);
}
function testItShouldSetErrors() {
// model validation
$subscriber = Subscriber::create();
$subscriber->hydrate(array(
'email' => 'invalid_email'
));
$subscriber->save();
$errors = $subscriber->getErrors();
expect($errors)->contains("Your email address is invalid.");
// pdo error
$subscriber = Subscriber::create();
$subscriber->hydrate(array(
'email' => 'test@test.com',
'invalid_column' => true
));
$subscriber->save();
$errors = $subscriber->getErrors();
expect($errors[0])->contains("Unknown column 'invalid_column' in 'field list'");
}
function emailMustBeUnique() {
$conflict_subscriber = Subscriber::create();
$conflict_subscriber->hydrate($this->data);
@ -124,7 +145,7 @@ class SubscriberTest extends MailPoetTest {
function testItCanHaveCustomFields() {
$custom_field = CustomField::createOrUpdate(array(
'name' => 'DOB',
'type' => 'date',
'type' => 'date'
));
$association = SubscriberCustomField::create();
@ -138,6 +159,63 @@ class SubscriberTest extends MailPoetTest {
expect($subscriber->DOB)->equals($association->value);
}
function testItCanCreateSubscriberWithCustomFields() {
$custom_field = CustomField::createOrUpdate(array(
'name' => 'City',
'type' => 'text'
));
$custom_field_2 = CustomField::createOrUpdate(array(
'name' => 'Age',
'type' => 'text'
));
$subscriber_with_custom_field = Subscriber::createOrUpdate(array(
'email' => 'user.with.cf@mailpoet.com',
'cf_'.$custom_field->id => 'Paris',
'cf_'.$custom_field_2->id => array(12, 23, 34)
));
$subscriber = Subscriber::findOne($subscriber_with_custom_field->id)
->withCustomFields();
expect($subscriber->id)->equals($subscriber_with_custom_field->id);
expect($subscriber->email)->equals('user.with.cf@mailpoet.com');
expect($subscriber->{'cf_'.$custom_field->id})->equals('Paris');
// array values are not supported so it should have assigned the first value
expect($subscriber->{'cf_'.$custom_field_2->id})->equals(12);
}
function testItShouldUnsubscribeFromAllSegments() {
$segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1'));
$segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
$subscriber = Subscriber::createOrUpdate(array(
'email' => 'jean.louis@mailpoet.com',
'status' => Subscriber::STATUS_SUBSCRIBED,
'segments' => array(
$segment_1->id,
$segment_2->id
)
));
$subscriber = Subscriber::findOne($subscriber->id);
$subscribed_segments = $subscriber->segments()->findArray();
expect($subscribed_segments)->count(2);
expect($subscribed_segments[0]['name'] = 'Segment 1');
expect($subscribed_segments[1]['name'] = 'Segment 2');
// update subscriber status
$unsubscribed_subscriber = Subscriber::createOrUpdate(array(
'email' => 'jean.louis@mailpoet.com',
'status' => Subscriber::STATUS_UNSUBSCRIBED
));
$subscribed_segments = $subscriber->segments()->findArray();
expect($subscribed_segments)->count(0);
}
function testItCanCreateOrUpdate() {
$data = array(
'email' => 'john.doe@mailpoet.com',