-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CExistValidatorTest.php
142 lines (123 loc) · 3.65 KB
/
CExistValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
class CExistValidatorTest extends CTestCase
{
/**
* @var CDbConnection test database connection.
*/
private $_connection;
/**
* @var string test table name.
*/
private $_tableName = 'test_table';
/**
* @var string test {@link CActiveRecord} model.
*/
private $_arModelName = 'TestExistModel';
protected function setUp()
{
if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
$this->markTestSkipped('PDO and SQLite extensions are required.');
$this->_connection=new CDbConnection('sqlite::memory:');
$this->_connection->active=true;
$columns = array(
'id'=>'pk',
'name'=>'string',
);
$this->_connection->createCommand()->createTable($this->_tableName, $columns);
CActiveRecord::$db=$this->_connection;
$this->declareArModelClass();
}
protected function tearDown()
{
$this->_connection->createCommand()->dropTable($this->_tableName);
$this->_connection->active=false;
}
/**
* Declares test active record class.
* @return boolean success.
*/
protected function declareArModelClass()
{
if (!class_exists($this->_arModelName,false))
{
$classDefinitionCode=<<<EOD
class {$this->_arModelName} extends CActiveRecord
{
public static function model(\$className=__CLASS__)
{
return parent::model(\$className);
}
public function tableName()
{
return '{$this->_tableName}';
}
public function rules()
{
return array(
array('name','exist','on'=>'simple'),
array('name','exist','caseSensitive'=>true,'on'=>'case_sensitive'),
array('name','exist','caseSensitive'=>false,'on'=>'not_case_sensitive'),
array('name','exist','criteria'=>array('alias'=>'test_alias'),'on'=>'criteria'),
);
}
}
EOD;
eval($classDefinitionCode);
}
return true;
}
public function testValidate()
{
$modelClassName = $this->_arModelName;
$name = 'test_name';
$model = new $modelClassName('simple');
$model->name = $name;
$this->assertFalse($model->validate(),'Not existing value considered as valid!');
$model->save(false);
$this->assertTrue($model->validate(),'Existing value consider as invalid!');
$anotherModel = new $modelClassName('simple');
$anotherModel->name = $name;
$this->assertTrue($anotherModel->validate(),'Duplicate entry of existing value considered as invalid!');
}
/**
* @depends testValidate
*/
public function testValidateCaseSensitive()
{
$modelClassName = $this->_arModelName;
$name = 'test_name';
$initModel = new $modelClassName();
$initModel->name = $name;
$initModel->save(false);
$caseSensitiveModel = new $modelClassName('case_sensitive');
$caseSensitiveModel->name = $name;
$this->assertTrue($caseSensitiveModel->validate(),'Validation breaks in case sensitive mode!');
$caseSensitiveModel->name = strtoupper($name);
$this->assertFalse($caseSensitiveModel->validate(),'Same value in other case considered as valid!');
$caseInsensitiveModel = new $modelClassName('not_case_sensitive');
$caseInsensitiveModel->name = strtoupper($name);
$this->assertTrue($caseInsensitiveModel->validate(),'Same value in other case considered as invalid!');
}
/**
* @depends testValidate
*/
public function testValidateWithCriteria()
{
$modelClassName = $this->_arModelName;
$name = 'test_name';
$model = new $modelClassName('criteria');
$model->name = $name;
$this->assertFalse($model->validate(),'Unable to validate model with custom criteria!');
}
/**
* https://github.com/yiisoft/yii/issues/1955
*/
public function testArrayValue()
{
$modelClassName = $this->_arModelName;
$model = new $modelClassName('simple');
$model->name = array('test_name');
$this->assertFalse($model->validate());
$this->assertTrue($model->hasErrors('name'));
}
}