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
|
|
|
/* fake custom behaviors */ |
90
|
|
|
public $custom_behaviors = [ |
91
|
|
|
1 => [ |
92
|
|
|
'making eye contact' => [ |
93
|
|
|
'id' => '777-custom', |
94
|
|
|
'name' => 'making eye contact', |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
2 => [ |
98
|
|
|
'bored' => [ |
99
|
|
|
'id' => '12333-custom', |
100
|
|
|
'name' => 'bored', |
101
|
|
|
], |
102
|
|
|
'less time/energy for God, meetings, and church' => [ |
103
|
|
|
'id' => '11122333-custom', |
104
|
|
|
'name' => 'less time/energy for God, meetings, and church', |
105
|
|
|
], |
106
|
|
|
'isolating yourself' => [ |
107
|
|
|
'id' => '17777-custom', |
108
|
|
|
'name' => 'isolating yourself', |
109
|
|
|
], |
110
|
|
|
'changes in goals' => [ |
111
|
|
|
'id' => '11888-custom', |
112
|
|
|
'name' => 'changes in goals', |
113
|
|
|
], |
114
|
|
|
], |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
protected function setUp() { |
118
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
119
|
|
|
$this->container->set('common\interfaces\UserBehaviorInterface', '\site\tests\_support\MockUserBehavior'); |
120
|
|
|
$this->container->set('common\interfaces\TimeInterface', function () { |
121
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
122
|
|
|
}); |
123
|
|
|
parent::setUp(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testAttributeLabels() |
127
|
|
|
{ |
128
|
|
|
$this->specify('attributeLabels should function properly', function () { |
129
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
130
|
|
|
expect('attributeLabels should be correct', $this->assertEquals([ |
|
|
|
|
131
|
|
|
'behaviors1' => 'Restoration', |
132
|
|
|
'behaviors2' => 'Forgetting Priorities', |
133
|
|
|
'behaviors3' => 'Anxiety', |
134
|
|
|
'behaviors4' => 'Speeding Up', |
135
|
|
|
'behaviors5' => 'Ticked Off', |
136
|
|
|
'behaviors6' => 'Exhausted', |
137
|
|
|
'behaviors7' => 'Relapsed/Moral Failure' |
138
|
|
|
], $model->attributeLabels())); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testSetBehaviors() |
143
|
|
|
{ |
144
|
|
|
$this->specify('setBehaviors should function properly', function () { |
145
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
146
|
|
|
|
147
|
|
|
$model->setBehaviors($this->behaviors); |
148
|
|
|
expect('behaviors1 should be correct', $this->assertEquals([ 0 => 7 ], $model->behaviors1)); |
|
|
|
|
149
|
|
|
expect('behaviors2 should be correct', $this->assertEquals([ 0 => 12, 1 => 13, 2 => 17, 3 => 18 ], $model->behaviors2)); |
|
|
|
|
150
|
|
|
expect('behaviors3 should be correct', $this->assertEquals([ 0 => 28, 1 => 38, 2 => 46 ], $model->behaviors3)); |
|
|
|
|
151
|
|
|
expect('behaviors4 should be correct', $this->assertEquals([ 0 => 47, 1 => 56, 2 => 62 ], $model->behaviors4)); |
|
|
|
|
152
|
|
|
expect('behaviors5 should be correct', $this->assertEquals([ 0 => 78, 1 => 79 ], $model->behaviors5)); |
|
|
|
|
153
|
|
|
expect('behaviors6 should be correct', $this->assertEquals([ 0 => 104 ], $model->behaviors6)); |
|
|
|
|
154
|
|
|
expect('behaviors7 should be correct', $this->assertEquals([ 0 => 128 ], $model->behaviors7)); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$model->setBehaviors($this->behaviors); |
157
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 7 ], $model->behaviors1)); |
|
|
|
|
158
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 12, 1 => 13, 2 => 17, 3 => 18 ], $model->behaviors2)); |
|
|
|
|
159
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 28, 1 => 38, 2 => 46 ], $model->behaviors3)); |
|
|
|
|
160
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 47, 1 => 56, 2 => 62 ], $model->behaviors4)); |
|
|
|
|
161
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 78, 1 => 79 ], $model->behaviors5)); |
|
|
|
|
162
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 104 ], $model->behaviors6)); |
|
|
|
|
163
|
|
|
expect('setBehaviors should not append behaviors to existing ones', $this->assertEquals([ 0 => 128 ], $model->behaviors7)); |
|
|
|
|
164
|
|
|
}); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testValidateBehaviors() |
168
|
|
|
{ |
169
|
|
|
$this->specify('validateBehaviors should function properly', function () { |
170
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
171
|
|
|
$model->setBehaviors($this->behaviors); |
172
|
|
|
expect('validation should be good', $this->assertTrue($model->validate())); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$model->behaviors1[0] = 'bad'; |
175
|
|
|
expect('validation should be bad', $this->assertFalse($model->validate())); |
|
|
|
|
176
|
|
|
}); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testCompileBehaviors() |
180
|
|
|
{ |
181
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
182
|
|
|
$model->setBehaviors($this->behaviors); |
183
|
|
|
expect('compiling behaviors should return a boolean true', $this->assertTrue($model->compileBehaviors())); |
|
|
|
|
184
|
|
|
expect('compiling behaviors should set the correct array', $this->assertEquals([ |
|
|
|
|
185
|
|
|
7, 12, 13, 17, 18, 28, 38, 46, 47, 56, 62, 78, 79, 104, 128, |
186
|
|
|
], $model->compiled_behaviors)); |
187
|
|
|
|
188
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
189
|
|
|
$model->setBehaviors($this->behaviors); |
190
|
|
|
$model->behaviors1[0] = null; |
191
|
|
|
$model->behaviors2[0] = null; |
192
|
|
|
$model->behaviors3[0] = null; |
193
|
|
|
$model->compileBehaviors(); |
194
|
|
|
expect('compiling behaviors should strip out any falsy values', $this->assertEquals([ |
|
|
|
|
195
|
|
|
13, 17, 18, 38, 46, 47, 56, 62, 78, 79, 104, 128, |
196
|
|
|
], $model->compiled_behaviors)); |
197
|
|
|
|
198
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
199
|
|
|
$model->setBehaviors([]); |
200
|
|
|
expect('compiling behaviors should return an empty array when no behaviors are set', $this->assertEmpty($model->compileBehaviors())); |
|
|
|
|
201
|
|
|
expect('compiling behaviors should not set the compiled_behaviors array to anything when no behaviors are set', $this->assertEmpty($model->compiled_behaviors)); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
204
|
|
|
$model->setBehaviors($this->custom_behaviors); |
205
|
|
|
$model->compileBehaviors(); |
206
|
|
|
expect('compiling behaviors should identify and set custom behaviors', $this->assertEquals(["777-custom", "12333-custom", "11122333-custom", "17777-custom", "11888-custom"], $model->custom_behaviors)); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testGetCustomBehaviors() { |
210
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
211
|
|
|
$model->custom_behaviors = ["777-custom", "12333-custom", "11122333-custom", "17777-custom", "11888-custom"]; |
212
|
|
|
expect('to return an array of extracted ids from an array of custom_behaviors', $this->assertEquals([777, 12333, 11122333, 17777, 11888], $model->getCustomBehaviors())); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
215
|
|
|
$model->custom_behaviors = []; |
216
|
|
|
$model->compiled_behaviors = [ 1, 2, 3]; |
217
|
|
|
expect('to return an empty array if no custom behaviors are set', $this->assertEquals([], $model->getCustomBehaviors())); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testMergeWithDefault() { |
221
|
|
|
$model = $this->container->get('\site\models\CheckinForm'); |
222
|
|
|
|
223
|
|
|
$base_behaviors = AH::index(Yii::$container->get(\common\models\Behavior::class)::$behaviors, 'name', "category_id"); |
224
|
|
|
expect('mergeWithDefault should return the base behaviors when given an empty array', $this->assertEquals($base_behaviors, $model->mergeWithDefault([]))); |
|
|
|
|
225
|
|
|
|
226
|
|
|
$other_behaviors = [ |
227
|
|
|
'some kind of really bad making eye contact' => [ |
228
|
|
|
'id' => 400, |
229
|
|
|
'name' => 'some kind of really bad making eye contact', |
230
|
|
|
'category_id' => 7 |
231
|
|
|
] |
232
|
|
|
]; |
233
|
|
|
$expected_behaviors = $base_behaviors; |
234
|
|
|
$expected_behaviors[7] += $other_behaviors; |
235
|
|
|
|
236
|
|
|
expect('mergeWithDefault should return the given behaviors merged in with the base behaviors', $this->assertEquals($base_behaviors, $model->mergeWithDefault($other_behaviors))); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|