Skip to content

Commit

Permalink
Adding test cases for validation generation
Browse files Browse the repository at this point in the history
Updating validation generation functions.
  • Loading branch information
markstory committed May 8, 2009
1 parent fae7ed5 commit 103b974
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 50 deletions.
98 changes: 55 additions & 43 deletions cake/console/libs/tasks/model.php
Expand Up @@ -201,7 +201,7 @@ function __interactive() {
$this->out(__("Associations:", true));
$assocKeys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany');
foreach ($assocKeys as $assocKey) {
$this->printAssociation($currentModelName, $assocKey, $associations);
$this->_printAssociation($currentModelName, $assocKey, $associations);
}
}

Expand All @@ -225,10 +225,10 @@ function __interactive() {
* @param string $modelName Name of the model relations belong to.
* @param string $type Name of association you want to see. i.e. 'belongsTo'
* @param string $associations Collection of associations.
* @access public
* @access protected
* @return void
**/
function printAssociation($modelName, $type, $associations) {
function _printAssociation($modelName, $type, $associations) {
if (!empty($associations[$type])) {
for ($i = 0; $i < count($associations[$type]); $i++) {
$out = "\t" . $modelName . ' ' . $type . ' ' . $associations[$type][$i]['alias'];
Expand Down Expand Up @@ -311,50 +311,62 @@ function initValidations() {
**/
function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
$defaultChoice = count($this->__validations);
$validate = array();
if ($this->interactive) {
$this->out('');
$this->out(sprintf(__('Field: %s', true), $fieldName));
$this->out(sprintf(__('Type: %s', true), $metaData['type']));
$this->hr();
$this->out(__('Please select one of the following validation options:', true));
$this->hr();
}
$methods = array_flip($this->__validations);
$prompt = '';
for ($i = 1; $i < $defaultChoice; $i++) {
$prompt .= $i . ' - ' . $this->__validations[$i] . "\n";
}
$prompt .= sprintf(__("%s - Do not do any validation on this field.\n", true), $defaultChoice);
$prompt .= __("... or enter in a valid regex validation string.\n", true);
$validate = $alredyChosen = array();

$guess = $defaultChoice;
if ($metaData['null'] != 1 && !in_array($fieldName, array($primaryKey, 'created', 'modified', 'updated'))) {
if ($fieldName == 'email') {
$guess = $methods['email'];
} elseif ($metaData['type'] == 'string') {
$guess = $methods['notempty'];
} elseif ($metaData['type'] == 'integer') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] == 'boolean') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] == 'datetime' || $metaData['type'] == 'date') {
$guess = $methods['date'];
} elseif ($metaData['type'] == 'time') {
$guess = $methods['time'];
$anotherValidator = 'y';
while ($anotherValidator == 'y') {
if ($this->interactive) {
$this->out('');
$this->out(sprintf(__('Field: %s', true), $fieldName));
$this->out(sprintf(__('Type: %s', true), $metaData['type']));
$this->hr();
$this->out(__('Please select one of the following validation options:', true));
$this->hr();
}
}

if ($this->interactive === true) {
$choice = $this->in($prompt, null, $guess);
} else {
$choice = $guess;
}
if ($choice != $defaultChoice) {
if (is_numeric($choice) && isset($choices[$choice])) {
$validate[$fieldName] = $choices[$choice];
$prompt = '';
for ($i = 1; $i < $defaultChoice; $i++) {
$prompt .= $i . ' - ' . $this->__validations[$i] . "\n";
}
$prompt .= sprintf(__("%s - Do not do any validation on this field.\n", true), $defaultChoice);
$prompt .= __("... or enter in a valid regex validation string.\n", true);

$methods = array_flip($this->__validations);
$guess = $defaultChoice;
if ($metaData['null'] != 1 && !in_array($fieldName, array($primaryKey, 'created', 'modified', 'updated'))) {
if ($fieldName == 'email') {
$guess = $methods['email'];
} elseif ($metaData['type'] == 'string') {
$guess = $methods['notempty'];
} elseif ($metaData['type'] == 'integer') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] == 'boolean') {
$guess = $methods['numeric'];
} elseif ($metaData['type'] == 'datetime' || $metaData['type'] == 'date') {
$guess = $methods['date'];
} elseif ($metaData['type'] == 'time') {
$guess = $methods['time'];
}
}

if ($this->interactive === true) {
$choice = $this->in($prompt, null, $guess);
$alreadyChosen[] = $choice;
} else {
$choice = $guess;
}
$validatorName = $this->__validations[$choice];
if ($choice != $defaultChoice) {
if (is_numeric($choice) && isset($this->__validations[$choice])) {
$validate[$validatorName] = $this->__validations[$choice];
} else {
$validate[$validatorName] = $choice;
}
}
if ($this->interactive == true) {
$anotherValidator = $this->in(__('Would you like to add another validation rule?', true), array('y', 'n'), 'n');
} else {
$validate[$fieldName] = $choice;
$anotherValidator = 'n';
}
}
return $validate;
Expand Down
43 changes: 36 additions & 7 deletions cake/tests/cases/console/libs/tasks/model.test.php
Expand Up @@ -164,23 +164,53 @@ function testInitValidations() {
$result = $this->Task->initValidations();
$this->assertTrue(in_array('notempty', $result));
}

/**
* test that individual field validation works, with interactive = false
* tests the guessing features of validation
*
* @return void
**/
function testNoInteractiveFieldValidation() {
function testFieldValidationGuessing() {
$this->Task->interactive = false;
$this->Task->initValidations();

$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('notempty' => 'notempty');

$result = $this->Task->fieldValidation('text', array('type' => 'date', 'length' => 10, 'null' => false));
$expected = array('date' => 'date');

$result = $this->Task->fieldValidation('text', array('type' => 'time', 'length' => 10, 'null' => false));
$expected = array('time' => 'time');

$result = $this->Task->fieldValidation('email', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('email' => 'email');

$result = $this->Task->fieldValidation('test', array('type' => 'integer', 'length' => 10, 'null' => false));
$expected = array('numeric' => 'numeric');

$result = $this->Task->fieldValidation('test', array('type' => 'boolean', 'length' => 10, 'null' => false));
$expected = array('numeric' => 'numeric');
}

/**
* test that interactive field validation works and returns multiple validators.
*
* @return void
**/
function testInteractiveFieldValidation() {

}

/**
* test the validation Generation routine
*
* @return void
**/
function testDoValidation() {
function testNonInteractiveDoValidation() {
$Model =& new MockModelTaskModel();
$Model->primaryKey = 'id';
$Model->setReturnValue('schema', array(
'id' => array(
'type' => 'integer',
Expand Down Expand Up @@ -219,19 +249,18 @@ function testDoValidation() {
$result = $this->Task->doValidation($Model);
$expected = array(
'name' => array(
'notEmpty' => array('rule' => 'notEmpty')
'notempty' => 'notempty'
),
'email' => array(
'email' => array('rule' => 'email'),
'email' => 'email',
),
'some_date' => array(
'date' => array('rule' => 'date')
'date' => 'date'
),
'some_time' => array(
'time' => array('rule' => 'time')
'time' => 'time'
),
);
debug($result);
$this->assertEqual($result, $expected);
}
}
Expand Down

0 comments on commit 103b974

Please sign in to comment.