1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper as AH; |
7
|
|
|
|
8
|
|
|
class CheckinFormTest extends \Codeception\Test\Unit |
9
|
|
|
{ |
10
|
|
|
use \Codeception\Specify; |
11
|
|
|
|
12
|
|
|
public $behaviors = [ |
13
|
|
|
1 => [ |
14
|
|
|
'making eye contact' => [ |
15
|
|
|
'id' => 7, |
16
|
|
|
'name' => 'making eye contact', |
17
|
|
|
], |
18
|
|
|
], |
19
|
|
|
2 => [ |
20
|
|
|
'bored' => [ |
21
|
|
|
'id' => 12, |
22
|
|
|
'name' => 'bored', |
23
|
|
|
], |
24
|
|
|
'less time/energy for God, meetings, and church' => [ |
25
|
|
|
'id' => 13, |
26
|
|
|
'name' => 'less time/energy for God, meetings, and church', |
27
|
|
|
], |
28
|
|
|
'isolating yourself' => [ |
29
|
|
|
'id' => 17, |
30
|
|
|
'name' => 'isolating yourself', |
31
|
|
|
], |
32
|
|
|
'changes in goals' => [ |
33
|
|
|
'id' => 18, |
34
|
|
|
'name' => 'changes in goals', |
35
|
|
|
], |
36
|
|
|
], |
37
|
|
|
3 => [ |
38
|
|
|
'worry' => [ |
39
|
|
|
'id' => 28, |
40
|
|
|
'name' => 'worry', |
41
|
|
|
], |
42
|
|
|
'fantasy' => [ |
43
|
|
|
'id' => 38, |
44
|
|
|
'name' => 'fantasy', |
45
|
|
|
], |
46
|
|
|
'using over-the-counter medication for pain, sleep, and weight control' => [ |
47
|
|
|
'id' => 46, |
48
|
|
|
'name' => 'using over-the-counter medication for pain, sleep, and weight control', |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
4 => [ |
52
|
|
|
'super busy' => [ |
53
|
|
|
'id' => 47, |
54
|
|
|
'name' => 'super busy', |
55
|
|
|
], |
56
|
|
|
'binge eating (usually at night]' => [ |
57
|
|
|
'id' => 56, |
58
|
|
|
'name' => 'binge eating (usually at night]', |
59
|
|
|
], |
60
|
|
|
'dramatic mood swings' => [ |
61
|
|
|
'id' => 62, |
62
|
|
|
'name' => 'dramatic mood swings', |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
5 => [ |
66
|
|
|
'increased isolation' => [ |
67
|
|
|
'id' => 78, |
68
|
|
|
'name' => 'increased isolation', |
69
|
|
|
], |
70
|
|
|
'blaming' => [ |
71
|
|
|
'id' => 79, |
72
|
|
|
'name' => 'blaming', |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
6 => [ |
76
|
|
|
'pessimistic' => [ |
77
|
|
|
'id' => 104, |
78
|
|
|
'name' => 'pessimistic', |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
7 => [ |
82
|
|
|
'feeling you just can\'t manage without your coping behavior, at least for now' => [ |
83
|
|
|
'id' => 128, |
84
|
|
|
'name' => 'feeling you just can\'t manage without your coping behavior, at least for now', |
85
|
|
|
], |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
protected function setUp() { |
90
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
91
|
|
|
$this->container->set('common\interfaces\UserBehaviorInterface', '\site\tests\_support\MockUserBehavior'); |
92
|
|
|
$this->container->set('common\interfaces\TimeInterface', function () { |
93
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
94
|
|
|
}); |
95
|
|
|
parent::setUp(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testAttributeLabels() |
99
|
|
|
{ |
100
|
|
|
$this->specify('attributeLabels should function properly', function () { |
101
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
102
|
|
|
expect('attributeLabels should be correct', $this->assertEquals([ |
|
|
|
|
103
|
|
|
'behaviors1' => 'Restoration', |
104
|
|
|
'behaviors2' => 'Forgetting Priorities', |
105
|
|
|
'behaviors3' => 'Anxiety', |
106
|
|
|
'behaviors4' => 'Speeding Up', |
107
|
|
|
'behaviors5' => 'Ticked Off', |
108
|
|
|
'behaviors6' => 'Exhausted', |
109
|
|
|
'behaviors7' => 'Relapsed/Moral Failure' |
110
|
|
|
], $model->attributeLabels())); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testSetBehaviors() |
115
|
|
|
{ |
116
|
|
|
$this->specify('setBehaviors should function properly', function () { |
117
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
118
|
|
|
|
119
|
|
|
$model->setBehaviors($this->behaviors); |
120
|
|
|
expect('behaviors1 should be correct', $this->assertEquals([ 0 => 7 ], $model->behaviors1)); |
|
|
|
|
121
|
|
|
expect('behaviors2 should be correct', $this->assertEquals([ 0 => 12, 1 => 13, 2 => 17, 3 => 18 ], $model->behaviors2)); |
|
|
|
|
122
|
|
|
expect('behaviors3 should be correct', $this->assertEquals([ 0 => 28, 1 => 38, 2 => 46 ], $model->behaviors3)); |
|
|
|
|
123
|
|
|
expect('behaviors4 should be correct', $this->assertEquals([ 0 => 47, 1 => 56, 2 => 62 ], $model->behaviors4)); |
|
|
|
|
124
|
|
|
expect('behaviors5 should be correct', $this->assertEquals([ 0 => 78, 1 => 79 ], $model->behaviors5)); |
|
|
|
|
125
|
|
|
expect('behaviors6 should be correct', $this->assertEquals([ 0 => 104 ], $model->behaviors6)); |
|
|
|
|
126
|
|
|
expect('behaviors7 should be correct', $this->assertEquals([ 0 => 128 ], $model->behaviors7)); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$model->setBehaviors($this->behaviors); |
129
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 7 ], $model->behaviors1)); |
|
|
|
|
130
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 12, 1 => 13, 2 => 17, 3 => 18 ], $model->behaviors2)); |
|
|
|
|
131
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 28, 1 => 38, 2 => 46 ], $model->behaviors3)); |
|
|
|
|
132
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 47, 1 => 56, 2 => 62 ], $model->behaviors4)); |
|
|
|
|
133
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 78, 1 => 79 ], $model->behaviors5)); |
|
|
|
|
134
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 104 ], $model->behaviors6)); |
|
|
|
|
135
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 128 ], $model->behaviors7)); |
|
|
|
|
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testValidateBehaviors() |
140
|
|
|
{ |
141
|
|
|
$this->specify('validateBehaviors should function properly', function () { |
142
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
143
|
|
|
$model->setBehaviors($this->behaviors); |
144
|
|
|
expect('validation should be good', $this->assertTrue($model->validate())); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$model->behaviors1[0] = 'bad'; |
147
|
|
|
expect('validation should be bad', $this->assertFalse($model->validate())); |
|
|
|
|
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testCompileBehaviors() |
152
|
|
|
{ |
153
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
154
|
|
|
$model->setBehaviors($this->behaviors); |
155
|
|
|
expect('compiling behaviors should be return a correct array', $this->assertEquals([ |
|
|
|
|
156
|
|
|
0 => 7, |
157
|
|
|
1 => 12, |
158
|
|
|
2 => 13, |
159
|
|
|
3 => 17, |
160
|
|
|
4 => 18, |
161
|
|
|
5 => 28, |
162
|
|
|
6 => 38, |
163
|
|
|
7 => 46, |
164
|
|
|
8 => 47, |
165
|
|
|
9 => 56, |
166
|
|
|
10 => 62, |
167
|
|
|
11 => 78, |
168
|
|
|
12 => 79, |
169
|
|
|
13 => 104, |
170
|
|
|
14 => 128, |
171
|
|
|
], $model->compileBehaviors())); |
172
|
|
|
|
173
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
174
|
|
|
$model->setBehaviors($this->behaviors); |
175
|
|
|
$model->behaviors1[0] = null; |
176
|
|
|
$model->behaviors2[0] = null; |
177
|
|
|
$model->behaviors3[0] = null; |
178
|
|
|
expect('compiling behaviors should strip out any falsy values', $this->assertEquals([ |
|
|
|
|
179
|
|
|
2 => 13, |
180
|
|
|
3 => 17, |
181
|
|
|
4 => 18, |
182
|
|
|
6 => 38, |
183
|
|
|
7 => 46, |
184
|
|
|
8 => 47, |
185
|
|
|
9 => 56, |
186
|
|
|
10 => 62, |
187
|
|
|
11 => 78, |
188
|
|
|
12 => 79, |
189
|
|
|
13 => 104, |
190
|
|
|
14 => 128, |
191
|
|
|
], $model->compileBehaviors())); |
192
|
|
|
|
193
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
194
|
|
|
$model->setBehaviors([]); |
195
|
|
|
expect('compiling behaviors should return an empty array when no behaviors are set', $this->assertEmpty($model->compileBehaviors())); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testMergeWithDefault() { |
199
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
200
|
|
|
|
201
|
|
|
$base_behaviors = AH::index(Yii::$container->get(\common\models\Behavior::class)::$behaviors, 'name', "category_id"); |
202
|
|
|
expect('mergeWithDefault should return the base behaviors when given an empty array', $this->assertEquals($base_behaviors, $model->mergeWithDefault([]))); |
|
|
|
|
203
|
|
|
|
204
|
|
|
$other_behaviors = [ |
205
|
|
|
'some kind of really bad making eye contact' => [ |
206
|
|
|
'id' => 400, |
207
|
|
|
'name' => 'some kind of really bad making eye contact', |
208
|
|
|
'category_id' => 7 |
209
|
|
|
] |
210
|
|
|
]; |
211
|
|
|
$expected_behaviors = $base_behaviors; |
212
|
|
|
$expected_behaviors[7] += $other_behaviors; |
213
|
|
|
|
214
|
|
|
expect('mergeWithDefault should return the given behaviors merged in with the base behaviors', $this->assertEquals($base_behaviors, $model->mergeWithDefault($other_behaviors))); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|