Necessary changes to the validation components. Further improvements and docs will follow in the next days.
Ticket: 150
This commit is contained in:
parent
499da8f9b8
commit
b0f0537071
File diff suppressed because it is too large
Load Diff
@ -27,10 +27,6 @@
|
||||
* @license LGPL
|
||||
*/
|
||||
class Doctrine_Validator {
|
||||
/**
|
||||
* @var array $stack error stack
|
||||
*/
|
||||
private $stack = array();
|
||||
/**
|
||||
* @var array $validators an array of validator objects
|
||||
*/
|
||||
@ -78,6 +74,8 @@ class Doctrine_Validator {
|
||||
$columns = $record->getTable()->getColumns();
|
||||
$component = $record->getTable()->getComponentName();
|
||||
|
||||
$errorStack = $record->getErrorStack();
|
||||
|
||||
switch($record->getState()):
|
||||
case Doctrine_Record::STATE_TDIRTY:
|
||||
case Doctrine_Record::STATE_TCLEAN:
|
||||
@ -102,7 +100,7 @@ class Doctrine_Validator {
|
||||
$value = $record->getTable()->enumIndex($key, $value);
|
||||
|
||||
if($value === false) {
|
||||
$err[$key] = 'enum';
|
||||
$errorStack->add($key, 'enum');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -113,7 +111,7 @@ class Doctrine_Validator {
|
||||
$length = strlen($value);
|
||||
|
||||
if($length > $column[1]) {
|
||||
$err[$key] = 'length';
|
||||
$errorStack->add($key, 'length');
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -144,7 +142,7 @@ class Doctrine_Validator {
|
||||
if( ! $validator->validate($record, $key, $value, $args)) {
|
||||
|
||||
|
||||
$err[$key] = $name;
|
||||
$errorStack->add($key, $name);
|
||||
|
||||
//$err[$key] = 'not valid';
|
||||
|
||||
@ -153,17 +151,10 @@ class Doctrine_Validator {
|
||||
}
|
||||
}
|
||||
if( ! self::isValidType($value, $column[0])) {
|
||||
$err[$key] = 'type';
|
||||
$errorStack->add($key, 'type');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if( ! empty($err)) {
|
||||
$this->stack = $err;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* whether or not this validator has errors
|
||||
@ -173,14 +164,6 @@ class Doctrine_Validator {
|
||||
public function hasErrors() {
|
||||
return (count($this->stack) > 0);
|
||||
}
|
||||
/**
|
||||
* returns the error stack
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrorStack() {
|
||||
return $this->stack;
|
||||
}
|
||||
/**
|
||||
* converts a doctrine type to native php type
|
||||
*
|
||||
|
@ -1,56 +1,156 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
Doctrine::autoload('Doctrine_Access');
|
||||
/**
|
||||
* Doctrine_Validator_ErrorStack
|
||||
*
|
||||
* @author Konsta Vesterinen
|
||||
* @license LGPL
|
||||
* @package Doctrine
|
||||
*/
|
||||
class Doctrine_Validator_ErrorStack extends Doctrine_Access implements Countable, IteratorAggregate {
|
||||
|
||||
private $errors = array();
|
||||
|
||||
public function merge($stack) {
|
||||
if(is_array($stack)) {
|
||||
$this->errors = array_merge($this->errors, $stack);
|
||||
}
|
||||
}
|
||||
|
||||
public function get($name) {
|
||||
if(isset($this->errors[$name]))
|
||||
return $this->errors[$name];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function set($name, $value) {
|
||||
$this->errors[$name] = $value;
|
||||
}
|
||||
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->errors);
|
||||
}
|
||||
public function count() {
|
||||
return count($this->errors);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Doctrine_Validator_ErrorStack
|
||||
*
|
||||
* @author Konsta Vesterinen
|
||||
* @author Roman Borschel
|
||||
* @license LGPL
|
||||
* @package Doctrine
|
||||
*/
|
||||
class Doctrine_Validator_ErrorStack implements ArrayAccess, Countable, IteratorAggregate {
|
||||
|
||||
/**
|
||||
* The errors of the error stack.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $errors = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Adds an error to the stack.
|
||||
*
|
||||
* @param string $invalidFieldName
|
||||
* @param string $errorType
|
||||
*/
|
||||
public function add($invalidFieldName, $errorType = 'general') {
|
||||
$this->errors[$invalidFieldName][] = array('type' => $errorType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all existing errors for the specified field from the stack.
|
||||
*
|
||||
* @param string $fieldName
|
||||
*/
|
||||
public function remove($fieldName) {
|
||||
if (isset($this->errors[$fieldName])) {
|
||||
unset($this->errors[$fieldName]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @param unknown_type $name
|
||||
* @return unknown
|
||||
*/
|
||||
public function get($name) {
|
||||
return $this[$name];
|
||||
}
|
||||
|
||||
/** ArrayAccess implementation */
|
||||
|
||||
/**
|
||||
* Gets all errors that occured for the specified field.
|
||||
*
|
||||
* @param string $offset
|
||||
* @return The array containing the errors or NULL if no errors were found.
|
||||
*/
|
||||
public function offsetGet($offset) {
|
||||
return isset($this->errors[$offset]) ? $this->errors[$offset] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @param string $offset
|
||||
* @param mixed $value
|
||||
* @throws Doctrine_Validator_ErrorStack_Exception Always thrown since this operation is not allowed.
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
throw new Doctrine_Validator_ErrorStack_Exception("Errors can only be added through
|
||||
Doctrine_Validator_ErrorStack::add()");
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @param unknown_type $offset
|
||||
*/
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->errors[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @param unknown_type $offset
|
||||
* @throws Doctrine_Validator_ErrorStack_Exception Always thrown since this operation is not allowed.
|
||||
*/
|
||||
public function offsetUnset($offset) {
|
||||
throw new Doctrine_Validator_ErrorStack_Exception("Errors can only be removed
|
||||
through Doctrine_Validator_ErrorStack::remove()");
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @param unknown_type $stack
|
||||
*/
|
||||
/*
|
||||
public function merge($stack) {
|
||||
if(is_array($stack)) {
|
||||
$this->errors = array_merge($this->errors, $stack);
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
/** IteratorAggregate implementation */
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @return unknown
|
||||
*/
|
||||
public function getIterator() {
|
||||
return new ArrayIterator($this->errors);
|
||||
}
|
||||
|
||||
|
||||
/** Countable implementation */
|
||||
|
||||
/**
|
||||
* Enter description here...
|
||||
*
|
||||
* @return unknown
|
||||
*/
|
||||
public function count() {
|
||||
return count($this->errors);
|
||||
}
|
||||
}
|
||||
|
@ -1,86 +1,86 @@
|
||||
<?php
|
||||
class Doctrine_EnumTestCase extends Doctrine_UnitTestCase {
|
||||
public function prepareData() { }
|
||||
public function prepareTables() {
|
||||
$this->tables = array("EnumTest");
|
||||
parent::prepareTables();
|
||||
}
|
||||
|
||||
public function testParameterConversion() {
|
||||
$test = new EnumTest();
|
||||
$test->status = 'open';
|
||||
$this->assertEqual($test->status, 'open');
|
||||
$test->save();
|
||||
|
||||
$query = new Doctrine_Query($this->connection);
|
||||
$ret = $query->query('FROM EnumTest WHERE EnumTest.status = ?', array('open'));
|
||||
$this->assertEqual(count($ret), 1);
|
||||
|
||||
$query = new Doctrine_Query($this->connection);
|
||||
$ret = $query->query('FROM EnumTest WHERE EnumTest.status = open');
|
||||
$this->assertEqual(count($ret), 1);
|
||||
|
||||
}
|
||||
public function testEnumType() {
|
||||
|
||||
$enum = new EnumTest();
|
||||
$enum->status = "open";
|
||||
$this->assertEqual($enum->status, "open");
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->status = "closed";
|
||||
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
$this->assertTrue(is_numeric($enum->id));
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
}
|
||||
|
||||
public function testEnumTypeWithCaseConversion() {
|
||||
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
|
||||
$enum = new EnumTest();
|
||||
|
||||
$enum->status = "open";
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->status = "closed";
|
||||
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
|
||||
}
|
||||
|
||||
public function testFailingRefresh() {
|
||||
$enum = $this->connection->getTable('EnumTest')->find(1);
|
||||
|
||||
$this->dbh->query('DELETE FROM enum_test WHERE id = 1');
|
||||
|
||||
$f = false;
|
||||
try {
|
||||
$enum->refresh();
|
||||
} catch(Doctrine_Record_Exception $e) {
|
||||
$f = true;
|
||||
}
|
||||
$this->assertTrue($f);
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php
|
||||
class Doctrine_EnumTestCase extends Doctrine_UnitTestCase {
|
||||
public function prepareData() { }
|
||||
public function prepareTables() {
|
||||
$this->tables = array("EnumTest");
|
||||
parent::prepareTables();
|
||||
}
|
||||
|
||||
public function testParameterConversion() {
|
||||
$test = new EnumTest();
|
||||
$test->status = 'open';
|
||||
$this->assertEqual($test->status, 'open');
|
||||
$test->save();
|
||||
|
||||
$query = new Doctrine_Query($this->connection);
|
||||
$ret = $query->query('FROM EnumTest WHERE EnumTest.status = ?', array('open'));
|
||||
$this->assertEqual(count($ret), 1);
|
||||
|
||||
$query = new Doctrine_Query($this->connection);
|
||||
$ret = $query->query('FROM EnumTest WHERE EnumTest.status = open');
|
||||
$this->assertEqual(count($ret), 1);
|
||||
|
||||
}
|
||||
public function testEnumType() {
|
||||
|
||||
$enum = new EnumTest();
|
||||
$enum->status = "open";
|
||||
$this->assertEqual($enum->status, "open");
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->status = "closed";
|
||||
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
$this->assertTrue(is_numeric($enum->id));
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
}
|
||||
|
||||
public function testEnumTypeWithCaseConversion() {
|
||||
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
|
||||
|
||||
$enum = new EnumTest();
|
||||
|
||||
$enum->status = "open";
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "open");
|
||||
|
||||
$enum->status = "closed";
|
||||
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$enum->save();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$enum->refresh();
|
||||
$this->assertEqual($enum->status, "closed");
|
||||
|
||||
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
|
||||
}
|
||||
|
||||
public function testFailingRefresh() {
|
||||
$enum = $this->connection->getTable('EnumTest')->find(1);
|
||||
|
||||
$this->dbh->query('DELETE FROM enum_test WHERE id = 1');
|
||||
|
||||
$f = false;
|
||||
try {
|
||||
$enum->refresh();
|
||||
} catch(Doctrine_Record_Exception $e) {
|
||||
$f = true;
|
||||
}
|
||||
$this->assertTrue($f);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -1,188 +1,227 @@
|
||||
<?php
|
||||
class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
|
||||
public function prepareTables() {
|
||||
$this->tables[] = "ValidatorTest";
|
||||
parent::prepareTables();
|
||||
}
|
||||
|
||||
public function testIsValidType() {
|
||||
$var = "123";
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = 123;
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = 123.12;
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = '123.12';
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = '';
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = null;
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = 'str';
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = array();
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = new Exception();
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"object"));
|
||||
}
|
||||
|
||||
public function testValidate2() {
|
||||
$test = new ValidatorTest();
|
||||
$test->mymixed = "message";
|
||||
$test->myrange = 1;
|
||||
$test->myregexp = '123a';
|
||||
|
||||
$validator = new Doctrine_Validator();
|
||||
$validator->validateRecord($test);
|
||||
|
||||
$stack = $validator->getErrorStack();
|
||||
|
||||
$this->assertTrue(is_array($stack));
|
||||
|
||||
$this->assertEqual($stack['mystring'], 'notnull');
|
||||
$this->assertEqual($stack['myemail2'], 'notblank');
|
||||
$this->assertEqual($stack['myrange'], 'range');
|
||||
$this->assertEqual($stack['myregexp'], 'regexp');
|
||||
$test->mystring = 'str';
|
||||
|
||||
|
||||
$test->save();
|
||||
}
|
||||
public function testEmailValidation() {
|
||||
}
|
||||
|
||||
public function testValidate() {
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
|
||||
$set = array("password" => "this is an example of too long password",
|
||||
"loginname" => "this is an example of too long loginname",
|
||||
"name" => "valid name",
|
||||
"created" => "invalid");
|
||||
$user->setArray($set);
|
||||
$email = $user->Email;
|
||||
$email->address = "zYne@invalid";
|
||||
|
||||
$this->assertTrue($user->getModified() == $set);
|
||||
|
||||
$validator = new Doctrine_Validator();
|
||||
$validator->validateRecord($user);
|
||||
|
||||
|
||||
$stack = $validator->getErrorStack();
|
||||
|
||||
$this->assertTrue(is_array($stack));
|
||||
$this->assertEqual($stack["loginname"], 'length');
|
||||
$this->assertEqual($stack["password"], 'length');
|
||||
$this->assertEqual($stack["created"], 'type');
|
||||
|
||||
|
||||
$validator->validateRecord($email);
|
||||
$stack = $validator->getErrorStack();
|
||||
$this->assertEqual($stack["address"], 'email');
|
||||
$email->address = "arnold@example.com";
|
||||
|
||||
$validator->validateRecord($email);
|
||||
$stack = $validator->getErrorStack();
|
||||
|
||||
$this->assertEqual($stack["address"], 'unique');
|
||||
|
||||
$email->isValid();
|
||||
|
||||
$this->assertTrue($email->getErrorStack() instanceof Doctrine_Validator_ErrorStack);
|
||||
}
|
||||
|
||||
public function testIsValidEmail() {
|
||||
|
||||
$validator = new Doctrine_Validator_Email();
|
||||
|
||||
$email = $this->connection->create("Email");
|
||||
$this->assertFalse($validator->validate($email,"address","example@example",null));
|
||||
$this->assertFalse($validator->validate($email,"address","example@@example",null));
|
||||
$this->assertFalse($validator->validate($email,"address","example@example.",null));
|
||||
$this->assertFalse($validator->validate($email,"address","example@e..",null));
|
||||
|
||||
$this->assertFalse($validator->validate($email,"address","example@e..",null));
|
||||
|
||||
$this->assertTrue($validator->validate($email,"address","null@pookey.co.uk",null));
|
||||
$this->assertTrue($validator->validate($email,"address","null@pookey.com",null));
|
||||
$this->assertTrue($validator->validate($email,"address","null@users.doctrine.pengus.net",null));
|
||||
|
||||
}
|
||||
|
||||
public function testSave() {
|
||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
try {
|
||||
$user->name = "this is an example of too long name not very good example but an example nevertheless";
|
||||
$user->save();
|
||||
} catch(Doctrine_Validator_Exception $e) {
|
||||
$this->assertEqual($e->count(), 1);
|
||||
}
|
||||
|
||||
try {
|
||||
$user = $this->connection->create("User");
|
||||
$user->Email->address = "jackdaniels@drinkmore.info...";
|
||||
$user->name = "this is an example of too long user name not very good example but an example nevertheles";
|
||||
$user->save();
|
||||
$this->fail();
|
||||
} catch(Doctrine_Validator_Exception $e) {
|
||||
$this->pass();
|
||||
$a = $e->getInvalidRecords();
|
||||
}
|
||||
|
||||
$this->assertTrue(is_array($a));
|
||||
|
||||
$emailStack = $a[array_search($user->Email, $a)]->getErrorStack();
|
||||
$userStack = $a[array_search($user, $a)]->getErrorStack();
|
||||
|
||||
$this->assertEqual($emailStack["address"], 'email');
|
||||
$this->assertEqual($userStack["name"], 'length');
|
||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
<?php
|
||||
/**
|
||||
* TestCase for Doctrine's validation component.
|
||||
*
|
||||
* @todo More tests to cover the full interface of Doctrine_Validator_ErrorStack.
|
||||
*/
|
||||
class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
|
||||
public function prepareTables() {
|
||||
$this->tables[] = "ValidatorTest";
|
||||
parent::prepareTables();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests correct type detection.
|
||||
*/
|
||||
public function testIsValidType() {
|
||||
$var = "123";
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = 123;
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = 123.12;
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = '123.12';
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = '';
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = null;
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = 'str';
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = array();
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"object"));
|
||||
|
||||
$var = new Exception();
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"string"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"integer"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"float"));
|
||||
$this->assertFalse(Doctrine_Validator::isValidType($var,"array"));
|
||||
$this->assertTrue(Doctrine_Validator::isValidType($var,"object"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Doctrine_Validator::validateRecord()
|
||||
*/
|
||||
public function testValidate2() {
|
||||
$test = new ValidatorTest();
|
||||
$test->mymixed = "message";
|
||||
$test->myrange = 1;
|
||||
$test->myregexp = '123a';
|
||||
|
||||
$validator = new Doctrine_Validator();
|
||||
$validator->validateRecord($test);
|
||||
|
||||
$stack = $test->getErrorStack();
|
||||
|
||||
$this->assertTrue($stack instanceof Doctrine_Validator_ErrorStack);
|
||||
|
||||
$this->assertTrue(in_array(array('type' => 'notnull'), $stack['mystring']));
|
||||
$this->assertTrue(in_array(array('type' => 'notblank'), $stack['myemail2']));
|
||||
$this->assertTrue(in_array(array('type' => 'range'), $stack['myrange']));
|
||||
$this->assertTrue(in_array(array('type' => 'regexp'), $stack['myregexp']));
|
||||
$test->mystring = 'str';
|
||||
|
||||
|
||||
$test->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests Doctrine_Validator::validateRecord()
|
||||
*/
|
||||
public function testValidate() {
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
|
||||
$set = array("password" => "this is an example of too long password",
|
||||
"loginname" => "this is an example of too long loginname",
|
||||
"name" => "valid name",
|
||||
"created" => "invalid");
|
||||
$user->setArray($set);
|
||||
$email = $user->Email;
|
||||
$email->address = "zYne@invalid";
|
||||
|
||||
$this->assertTrue($user->getModified() == $set);
|
||||
|
||||
$validator = new Doctrine_Validator();
|
||||
$validator->validateRecord($user);
|
||||
|
||||
|
||||
$stack = $user->getErrorStack();
|
||||
|
||||
$this->assertTrue($stack instanceof Doctrine_Validator_ErrorStack);
|
||||
$this->assertTrue(in_array(array('type' => 'length'), $stack['loginname']));
|
||||
$this->assertTrue(in_array(array('type' => 'length'), $stack['password']));
|
||||
$this->assertTrue(in_array(array('type' => 'type'), $stack['created']));
|
||||
|
||||
$validator->validateRecord($email);
|
||||
$stack = $email->getErrorStack();
|
||||
$this->assertTrue(in_array(array('type' => 'email'), $stack['address']));
|
||||
$email->address = "arnold@example.com";
|
||||
|
||||
$validator->validateRecord($email);
|
||||
$stack = $email->getErrorStack();
|
||||
|
||||
$this->assertTrue(in_array(array('type' => 'unique'), $stack['address']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the Email validator. (Doctrine_Validator_Email)
|
||||
*/
|
||||
public function testIsValidEmail() {
|
||||
|
||||
$validator = new Doctrine_Validator_Email();
|
||||
|
||||
$email = $this->connection->create("Email");
|
||||
$this->assertFalse($validator->validate($email,"address","example@example",null));
|
||||
$this->assertFalse($validator->validate($email,"address","example@@example",null));
|
||||
$this->assertFalse($validator->validate($email,"address","example@example.",null));
|
||||
$this->assertFalse($validator->validate($email,"address","example@e..",null));
|
||||
|
||||
$this->assertFalse($validator->validate($email,"address","example@e..",null));
|
||||
|
||||
$this->assertTrue($validator->validate($email,"address","null@pookey.co.uk",null));
|
||||
$this->assertTrue($validator->validate($email,"address","null@pookey.com",null));
|
||||
$this->assertTrue($validator->validate($email,"address","null@users.doctrine.pengus.net",null));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests saving records with invalid attributes.
|
||||
*/
|
||||
public function testSave() {
|
||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
try {
|
||||
$user->name = "this is an example of too long name not very good example but an example nevertheless";
|
||||
$user->save();
|
||||
} catch(Doctrine_Validator_Exception $e) {
|
||||
$this->assertEqual($e->count(), 1);
|
||||
$invalidRecords = $e->getInvalidRecords();
|
||||
$this->assertEqual(count($invalidRecords), 1);
|
||||
$stack = $invalidRecords[0]->getErrorStack();
|
||||
$this->assertTrue(in_array(array('type' => 'length'), $stack['name']));
|
||||
}
|
||||
|
||||
try {
|
||||
$user = $this->connection->create("User");
|
||||
$user->Email->address = "jackdaniels@drinkmore.info...";
|
||||
$user->name = "this is an example of too long user name not very good example but an example nevertheles";
|
||||
$user->save();
|
||||
$this->fail();
|
||||
} catch(Doctrine_Validator_Exception $e) {
|
||||
$this->pass();
|
||||
$a = $e->getInvalidRecords();
|
||||
}
|
||||
|
||||
$this->assertTrue(is_array($a));
|
||||
|
||||
$emailStack = $a[array_search($user->Email, $a)]->getErrorStack();
|
||||
$userStack = $a[array_search($user, $a)]->getErrorStack();
|
||||
|
||||
$this->assertTrue(in_array(array('type' => 'email'), $emailStack['address']));
|
||||
$this->assertTrue(in_array(array('type' => 'length'), $userStack['name']));
|
||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether custom validation through template methods works correctly
|
||||
* in descendants of Doctrine_Record.
|
||||
*/
|
||||
public function testCustomValidation() {
|
||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
|
||||
$user = $this->connection->getTable("User")->find(4);
|
||||
try {
|
||||
$user->name = "I'm not The Saint";
|
||||
$user->save();
|
||||
} catch(Doctrine_Validator_Exception $e) {
|
||||
$this->assertEqual($e->count(), 1);
|
||||
$invalidRecords = $e->getInvalidRecords();
|
||||
$this->assertEqual(count($invalidRecords), 1);
|
||||
|
||||
$stack = $invalidRecords[0]->getErrorStack();
|
||||
|
||||
$this->assertEqual($stack->count(), 1);
|
||||
$this->assertTrue(in_array(array('type' => 'notTheSaint'), $stack['name']));
|
||||
}
|
||||
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -97,6 +97,13 @@ class User extends Entity {
|
||||
$this->hasMany("Group","Groupuser.group_id");
|
||||
$this->setInheritanceMap(array("type"=>0));
|
||||
}
|
||||
/** Custom validation */
|
||||
public function validate() {
|
||||
// Allow only one name!
|
||||
if ($this->name !== 'The Saint') {
|
||||
$this->errorStack->add('name', 'notTheSaint');
|
||||
}
|
||||
}
|
||||
}
|
||||
class Groupuser extends Doctrine_Record {
|
||||
public function setTableDefinition() {
|
||||
@ -541,4 +548,5 @@ class BoardWithPosition extends Doctrine_Record {
|
||||
$this->hasOne("CategoryWithPosition as Category", "BoardWithPosition.category_id");
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user