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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace yiiunit\framework\base;
use yii\base\Model;
use yiiunit\TestCase;
use yiiunit\data\base\Speaker;
use yiiunit\data\base\Singer;
use yiiunit\data\base\InvalidRulesModel;
/**
* ModelTest
*/
class ModelTest extends TestCase
{
public function testGetAttributeLabel()
{
$speaker = new Speaker();
$this->assertEquals('First Name', $speaker->getAttributeLabel('firstName'));
$this->assertEquals('This is the custom label', $speaker->getAttributeLabel('customLabel'));
$this->assertEquals('Underscore Style', $speaker->getAttributeLabel('underscore_style'));
}
public function testGetAttributes()
{
$speaker = new Speaker();
$speaker->firstName = 'Qiang';
$speaker->lastName = 'Xue';
$this->assertEquals(array(
'firstName' => 'Qiang',
'lastName' => 'Xue',
'customLabel' => null,
'underscore_style' => null,
), $speaker->getAttributes());
$this->assertEquals(array(
'firstName' => 'Qiang',
'lastName' => 'Xue',
), $speaker->getAttributes(array('firstName', 'lastName')));
$this->assertEquals(array(
'firstName' => 'Qiang',
'lastName' => 'Xue',
), $speaker->getAttributes(null, array('customLabel', 'underscore_style')));
$this->assertEquals(array(
'firstName' => 'Qiang',
), $speaker->getAttributes(array('firstName', 'lastName'), array('lastName', 'customLabel', 'underscore_style')));
}
public function testSetAttributes()
{
// by default mass assignment doesn't work at all
$speaker = new Speaker();
$speaker->setAttributes(array('firstName' => 'Qiang', 'underscore_style' => 'test'));
$this->assertNull($speaker->firstName);
$this->assertNull($speaker->underscore_style);
// in the test scenario
$speaker = new Speaker();
$speaker->setScenario('test');
$speaker->setAttributes(array('firstName' => 'Qiang', 'underscore_style' => 'test'));
$this->assertNull($speaker->underscore_style);
$this->assertEquals('Qiang', $speaker->firstName);
$speaker->setAttributes(array('firstName' => 'Qiang', 'underscore_style' => 'test'), false);
$this->assertEquals('test', $speaker->underscore_style);
$this->assertEquals('Qiang', $speaker->firstName);
}
public function testActiveAttributes()
{
// by default mass assignment doesn't work at all
$speaker = new Speaker();
$this->assertEmpty($speaker->activeAttributes());
$speaker = new Speaker();
$speaker->setScenario('test');
$this->assertEquals(array('firstName', 'lastName', 'underscore_style'), $speaker->activeAttributes());
}
public function testIsAttributeSafe()
{
// by default mass assignment doesn't work at all
$speaker = new Speaker();
$this->assertFalse($speaker->isAttributeSafe('firstName'));
$speaker = new Speaker();
$speaker->setScenario('test');
$this->assertTrue($speaker->isAttributeSafe('firstName'));
}
public function testErrors()
{
$speaker = new Speaker();
$this->assertEmpty($speaker->getErrors());
$this->assertEmpty($speaker->getErrors('firstName'));
$this->assertEmpty($speaker->getFirstErrors());
$this->assertFalse($speaker->hasErrors());
$this->assertFalse($speaker->hasErrors('firstName'));
$speaker->addError('firstName', 'Something is wrong!');
$this->assertEquals(array('firstName' => array('Something is wrong!')), $speaker->getErrors());
$this->assertEquals(array('Something is wrong!'), $speaker->getErrors('firstName'));
$speaker->addError('firstName', 'Totally wrong!');
$this->assertEquals(array('firstName' => array('Something is wrong!', 'Totally wrong!')), $speaker->getErrors());
$this->assertEquals(array('Something is wrong!', 'Totally wrong!'), $speaker->getErrors('firstName'));
$this->assertTrue($speaker->hasErrors());
$this->assertTrue($speaker->hasErrors('firstName'));
$this->assertFalse($speaker->hasErrors('lastName'));
$this->assertEquals(array('Something is wrong!'), $speaker->getFirstErrors());
$this->assertEquals('Something is wrong!', $speaker->getFirstError('firstName'));
$this->assertNull($speaker->getFirstError('lastName'));
$speaker->addError('lastName', 'Another one!');
$this->assertEquals(array(
'firstName' => array(
'Something is wrong!',
'Totally wrong!',
),
'lastName' => array('Another one!'),
), $speaker->getErrors());
$speaker->clearErrors('firstName');
$this->assertEquals(array(
'lastName' => array('Another one!'),
), $speaker->getErrors());
$speaker->clearErrors();
$this->assertEmpty($speaker->getErrors());
$this->assertFalse($speaker->hasErrors());
}
public function testArraySyntax()
{
$speaker = new Speaker();
// get
$this->assertNull($speaker['firstName']);
// isset
$this->assertFalse(isset($speaker['firstName']));
// set
$speaker['firstName'] = 'Qiang';
$this->assertEquals('Qiang', $speaker['firstName']);
$this->assertTrue(isset($speaker['firstName']));
// iteration
$attributes = array();
foreach ($speaker as $key => $attribute) {
$attributes[$key] = $attribute;
}
$this->assertEquals(array(
'firstName' => 'Qiang',
'lastName' => null,
'customLabel' => null,
'underscore_style' => null,
), $attributes);
// unset
unset($speaker['firstName']);
// exception isn't expected here
$this->assertNull($speaker['firstName']);
$this->assertFalse(isset($speaker['firstName']));
}
public function testDefaults()
{
$singer = new Model();
$this->assertEquals(array(), $singer->rules());
$this->assertEquals(array(), $singer->attributeLabels());
}
public function testDefaultScenarios()
{
$singer = new Singer();
$this->assertEquals(array('default' => array('lastName', 'underscore_style')), $singer->scenarios());
}
public function testIsAttributeRequired()
{
$singer = new Singer();
$this->assertFalse($singer->isAttributeRequired('firstName'));
$this->assertTrue($singer->isAttributeRequired('lastName'));
}
public function testCreateValidators()
{
$this->setExpectedException('yii\base\InvalidConfigException', 'Invalid validation rule: a rule must specify both attribute names and validator type.');
$invalid = new InvalidRulesModel();
$invalid->createValidators();
}
}