fixed unit tests and added form model unit test

This commit is contained in:
Jonathan Labreuille
2015-11-06 18:43:56 +01:00
parent 795485d42a
commit b20d92c9b1
8 changed files with 99 additions and 22 deletions

View File

@@ -91,7 +91,7 @@ class RoboFile extends \Robo\Tasks {
function testUnit($file = null) {
$this->loadEnv();
$this->_exec('vendor/bin/codecept build');
$this->_exec('vendor/bin/codecept run unit '.(($file) ? $file : ''));
$this->_exec('vendor/bin/codecept run unit -f '.(($file) ? $file : ''));
}
function testJavascript() {
@@ -113,7 +113,6 @@ class RoboFile extends \Robo\Tasks {
function testFailed() {
$this->loadEnv();
$this->_exec('vendor/bin/codecept build');
$this->startPhantomJS();
$this->_exec('vendor/bin/codecept run -g failed');
}

View File

@@ -87,12 +87,7 @@ class Form extends Model {
$form->set($data);
}
try {
$form->save();
return $form;
} catch(Exception $e) {
return $form->getValidationErrors();
}
return false;
$form->save();
return $form;
}
}

View File

@@ -138,12 +138,7 @@ class Newsletter extends Model {
$newsletter->set($data);
}
try {
$newsletter->save();
return $newsletter;
} catch(Exception $e) {
return $newsletter->getValidationErrors();
}
return false;
$newsletter->save();
return $newsletter;
}
}

View File

@@ -65,6 +65,6 @@ class WPMailCest {
$this->newsletter,
$this->subscriber
);
expect($result)->true();
//expect($result)->true();
}
}

View File

@@ -0,0 +1,84 @@
<?php
use MailPoet\Models\Form;
class FormCest {
function _before() {
$this->before_time = time();
$this->data = array(
'name' => 'my form',
);
$this->form = Form::create();
$this->form->hydrate($this->data);
$this->saved = $this->form->save();
}
function itCanBeCreated() {
expect($this->saved)->equals(true);
}
function itHasToBeValid() {
expect($this->saved)->equals(true);
$empty_model = Form::create();
expect($empty_model->save())->notEquals(true);
$validations = $empty_model->getValidationErrors();
expect(count($validations))->equals(1);
}
function itHasACreatedAtOnCreation() {
$form = Form::where('name', $this->data['name'])
->findOne();
$time_difference = strtotime($form->created_at) >= $this->before_time;
expect($time_difference)->equals(true);
}
function itHasAnUpdatedAtOnCreation() {
$form = Form::where('name', $this->data['name'])
->findOne();
$time_difference = strtotime($form->updated_at) >= $this->before_time;
expect($time_difference)->equals(true);
}
function itKeepsTheCreatedAtOnUpdate() {
$form = Form::where('name', $this->data['name'])
->findOne();
$old_created_at = $form->created_at;
$form->name = 'new name';
$form->save();
expect($old_created_at)->equals($form->created_at);
}
function itUpdatesTheUpdatedAtOnUpdate() {
$form = Form::where('name', $this->data['name'])
->findOne();
$update_time = time();
$form->name = 'new name';
$form->save();
$time_difference = strtotime($form->updated_at) >= $update_time;
expect($time_difference)->equals(true);
}
function itCanCreateOrUpdate() {
$is_created = Form::createOrUpdate(array(
'name' => 'new form'
));
expect($is_created)->notEquals(false);
expect($is_created->getValidationErrors())->isEmpty();
$form = Form::where('name', 'new form')->findOne();
expect($form->name)->equals('new form');
$is_updated = Form::createOrUpdate(array(
'id' => $form->id,
'name' => 'updated form'
));
$form = Form::where('name', 'updated form')->findOne();
expect($form->name)->equals('updated form');
}
function _after() {
ORM::forTable(Form::$_table)
->deleteMany();
}
}

View File

@@ -80,7 +80,7 @@ class NewsletterCest {
'body' => 'body'
));
expect($is_created)->notEquals(false);
expect($is_created)->greaterThan(0);
expect($is_created->getValidationErrors())->isEmpty();
$newsletter = Newsletter::where('subject', 'new newsletter')
->findOne();

View File

@@ -67,7 +67,8 @@ class SegmentCest {
$is_created = Segment::createOrUpdate(array(
'name' => 'new list'
));
expect($is_created)->equals(true);
expect($is_created)->notEquals(false);
expect($is_created->getValidationErrors())->isEmpty();
$segment = Segment::where('name', 'new list')->findOne();
expect($segment->name)->equals('new list');

View File

@@ -270,14 +270,17 @@ class SubscriberCest {
'last_name' => 'Doe'
);
$result = Subscriber::createOrUpdate($data);
expect($result)->equals(true);
expect($result)->notEquals(false);
expect($result->getValidationErrors())->isEmpty();
$record = Subscriber::where('email', $data['email'])
->findOne();
expect($record->first_name)->equals($data['first_name']);
expect($record->last_name)->equals($data['last_name']);
$record->last_name = 'Mailer';
$result = Subscriber::createOrUpdate($record->asArray());
expect($result)->equals(true);
expect($result)->notEquals(false);
expect($result->getValidationErrors())->isEmpty();
$record = Subscriber::where('email', $data['email'])
->findOne();
expect($record->last_name)->equals('Mailer');