1
0
mirror of synced 2024-12-14 23:26:04 +03:00
doctrine2/tests/ValidatorTestCase.php

168 lines
7.6 KiB
PHP
Raw Normal View History

2006-04-14 00:37:28 +04:00
<?php
class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase {
public function prepareTables() {
$this->tables[] = "Validator_Test";
parent::prepareTables();
}
2006-06-11 16:02:05 +04:00
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 Validator_Test();
$test->mymixed = "message";
$validator = new Doctrine_Validator();
$validator->validateRecord($test);
$stack = $validator->getErrorStack();
$this->assertTrue(is_array($stack));
$stack = $stack['Validator_Test'][0];
2006-08-16 01:32:59 +04:00
$this->assertEqual($stack['mystring'], Doctrine_Validator::ERR_NOTNULL);
2006-08-16 01:32:59 +04:00
$this->assertEqual($stack['myemail2'], Doctrine_Validator::ERR_NOTBLANK);
$test->mystring = 'str';
$test->save();
}
public function testEmailValidation() {
}
2006-04-14 00:37:28 +04:00
public function testValidate() {
$user = $this->session->getTable("User")->find(4);
2006-04-14 00:37:28 +04:00
$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;
2006-04-14 00:37:28 +04:00
$email->address = "zYne@invalid";
$this->assertTrue($user->getModified() == $set);
2006-04-14 00:37:28 +04:00
$validator = new Doctrine_Validator();
$validator->validateRecord($user);
2006-04-14 00:37:28 +04:00
$validator->validateRecord($email);
$stack = $validator->getErrorStack();
$this->assertTrue(is_array($stack));
$this->assertEqual($stack["User"][0]["loginname"], Doctrine_Validator::ERR_LENGTH);
$this->assertEqual($stack["User"][0]["password"], Doctrine_Validator::ERR_LENGTH);
$this->assertEqual($stack["User"][0]["created"], Doctrine_Validator::ERR_TYPE);
$this->assertEqual($stack["Email"][0]["address"], Doctrine_Validator::ERR_VALID);
$email->address = "arnold@example.com";
$validator->validateRecord($email);
$stack = $validator->getErrorStack();
$this->assertEqual($stack["Email"][1]["address"], Doctrine_Validator::ERR_UNIQUE);
}
2006-04-14 00:37:28 +04:00
public function testIsValidEmail() {
$validator = new Doctrine_Validator_Email();
$email = $this->session->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));
2006-04-14 00:37:28 +04:00
$this->assertFalse($validator->validate($email,"address","example@e..",null));
$this->assertTrue($validator->validate($email,"address","example@e.e.e.e.e",null));
2006-04-14 00:37:28 +04:00
}
public function testSave() {
$this->manager->setAttribute(Doctrine::ATTR_VLD, true);
2006-08-16 01:32:59 +04:00
$user = $this->session->getTable("User")->find(4);
2006-04-14 00:37:28 +04:00
try {
$user->name = "this is an example of too long name not very good example but an example nevertheless";
$user->save();
2006-04-14 00:37:28 +04:00
} catch(Doctrine_Validator_Exception $e) {
$this->assertEqual($e->getErrorStack(),array("User" => array(array("name" => 0))));
}
try {
$user = $this->session->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();
} catch(Doctrine_Validator_Exception $e) {
$a = $e->getErrorStack();
}
$this->assertTrue(is_array($a));
$this->assertEqual($a["Email"][0]["address"], Doctrine_Validator::ERR_VALID);
$this->assertEqual($a["User"][0]["name"], Doctrine_Validator::ERR_LENGTH);
$this->manager->setAttribute(Doctrine::ATTR_VLD, false);
2006-04-14 00:37:28 +04:00
}
2006-06-20 01:31:22 +04:00
2006-04-14 00:37:28 +04:00
}
?>