1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \site\models\QuestionForm; |
7
|
|
|
use \yii\di\Container; |
8
|
|
|
|
9
|
|
|
class QuestionFormTest extends \Codeception\Test\Unit |
10
|
|
|
{ |
11
|
|
|
use \Codeception\Specify; |
12
|
|
|
|
13
|
|
|
private $container; |
14
|
|
|
|
15
|
|
|
public function setUp() { |
16
|
|
|
$this->container = new Container; |
17
|
|
|
$this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser'); |
18
|
|
|
$this->container->set('common\interfaces\UserBehaviorInterface', '\site\tests\_support\MockUserBehavior'); |
19
|
|
|
$this->container->set('common\interfaces\QuestionInterface', '\site\tests\_support\MockQuestion'); |
20
|
|
|
$this->container->set('common\interfaces\TimeInterface', function () { |
21
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAttributeLabels() |
26
|
|
|
{ |
27
|
|
|
$this->specify('setBehaviors should function properly', function () { |
28
|
|
|
$model = $this->container->get('\site\models\QuestionForm'); |
29
|
|
|
expect('attributeLabels should be correct', $this->assertEquals($model->attributeLabels(), [ |
|
|
|
|
30
|
|
|
'user_behavior_id1' => 'Restoration', |
31
|
|
|
'user_behavior_id2' => 'Forgetting Priorities', |
32
|
|
|
'user_behavior_id3' => 'Anxiety', |
33
|
|
|
'user_behavior_id4' => 'Speeding Up', |
34
|
|
|
'user_behavior_id5' => 'Ticked Off', |
35
|
|
|
'user_behavior_id6' => 'Exhausted', |
36
|
|
|
'user_behavior_id7' => 'Relapsed/Moral Failure' |
37
|
|
|
])); |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetBhvrValidator() |
42
|
|
|
{ |
43
|
|
|
$model = $this->container->get('\site\models\QuestionForm'); |
44
|
|
|
$validator = $model->getBhvrValidator(); |
45
|
|
|
|
46
|
|
|
$this->specify('getBhvrValidator should function properly', function () use($model, $validator) { |
47
|
|
|
expect('getBhvrValidator should return false when nothing is set on the form', $this->assertFalse($validator($model, "user_behavior_id1"))); |
|
|
|
|
48
|
|
|
expect('getBhvrValidator should return false when nothing is set on the form', $this->assertFalse($validator($model, "user_behavior_id7"))); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$model->answer_1a = "processing emotions"; |
51
|
|
|
expect('getBhvrValidator should return true when there is one answer set for this behavior', $this->assertTrue($validator($model, "user_behavior_id1"))); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$model->answer_1b = "also processing emotions"; |
54
|
|
|
expect('getBhvrValidator should return true when there are two answers set for this behavior', $this->assertTrue($validator($model, "user_behavior_id1"))); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$model->answer_1c = "yep, processing emotions"; |
57
|
|
|
expect('getBhvrValidator should return true when there are three answers set for this behavior', $this->assertTrue($validator($model, "user_behavior_id1"))); |
|
|
|
|
58
|
|
|
|
59
|
|
|
expect('getBhvrValidator should return false when the answers that are set are NOT for this behavior', $this->assertFalse($validator($model, "user_behavior_id3"))); |
|
|
|
|
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetPrefixProps() |
64
|
|
|
{ |
65
|
|
|
$model = $this->container->get('\site\models\QuestionForm'); |
66
|
|
|
|
67
|
|
|
$this->specify('getPrefixProps should function properly', function() use($model) { |
68
|
|
|
expect('getPrefixProps should strip out all the falsy propeties it finds', $this->assertEmpty($model->getPrefixProps('answer'))); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$model->answer_1a = 'processing emotions'; |
71
|
|
|
expect('getPrefixProps should return non-falsy properties that have the given prefix', $this->assertEquals($model->getPrefixProps('answer'), ['answer_1a' => 'processing emotions'])); |
|
|
|
|
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetUserBehaviorIds() |
76
|
|
|
{ |
77
|
|
|
$model = $this->container->get('\site\models\QuestionForm'); |
78
|
|
|
|
79
|
|
|
$model->user_behavior_id1 = false; |
80
|
|
|
$model->user_behavior_id3 = null; |
81
|
|
|
expect('getUserBehaviorIds should strip out all the falsy propeties it finds', $this->assertEmpty($model->getUserBehaviorIds('user_behavior_id'))); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$model->user_behavior_id1 = '323'; |
84
|
|
|
$model->user_behavior_id2 = 122; |
85
|
|
|
$model->user_behavior_id3 = '454'; |
86
|
|
|
$model->answer_1a = 'processing emotions'; |
87
|
|
|
expect('getUserBehaviorIds should return non-falsy properties that have the given prefix', $this->assertEquals($model->getUserBehaviorIds('user_behavior_id'), [323, 122, 454])); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testBehaviorToAnswers() |
91
|
|
|
{ |
92
|
|
|
$this->specify('behaviorToAnswers should function properly', function() { |
93
|
|
|
$model = $this->container->get('\site\models\QuestionForm'); |
94
|
|
|
$model->answer_1a = 'answering question a'; |
95
|
|
|
$model->answer_1b = 'answering question b'; |
96
|
|
|
$model->answer_1c = 'answering question c'; |
97
|
|
|
|
98
|
|
|
$model->answer_2a = 'answering question a'; |
99
|
|
|
$model->answer_2b = 'answering question b'; |
100
|
|
|
$model->answer_2c = 'answering question c'; |
101
|
|
|
|
102
|
|
|
expect('behaviorToAnswers should return the answer properties related to the behavior number supplied', $this->assertEquals($model->behaviorToAnswers(1), [ |
|
|
|
|
103
|
|
|
'a' => 'answering question a', |
104
|
|
|
'b' => 'answering question b', |
105
|
|
|
'c' => 'answering question c' |
106
|
|
|
])); |
107
|
|
|
|
108
|
|
|
expect('behaviorToAnswers should return the the empty set when there are no answers associated with the supplied behavior number', $this->assertEmpty($model->behaviorToAnswers(7))); |
|
|
|
|
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGetAnswers() |
113
|
|
|
{ |
114
|
|
|
$model = $this->container->get('\site\models\QuestionForm'); |
115
|
|
|
|
116
|
|
|
$this->specify('getAnswers should function properly', function() use($model) { |
117
|
|
|
$model->user_behavior_id1 = 'dummy'; |
118
|
|
|
$model->user_behavior_id2 = 'dummy'; |
119
|
|
|
$model->user_behavior_id3 = 'dummy'; |
120
|
|
|
$model->answer_1a = "processing emotions"; |
121
|
|
|
$model->answer_1b = "processing emotions"; |
122
|
|
|
$model->answer_1c = "processing emotions"; |
123
|
|
|
$model->answer_2a = "processing emotions"; |
124
|
|
|
$model->answer_2b = "processing emotions"; |
125
|
|
|
$model->answer_2c = "processing emotions"; |
126
|
|
|
$model->answer_3a = "processing emotions"; |
127
|
|
|
$model->answer_3b = "processing emotions"; |
128
|
|
|
$model->answer_3c = "processing emotions"; |
129
|
|
|
|
130
|
|
|
expect('getAnswers should extract and coerce the data correctly', $this->assertEquals($model->getAnswers([ |
|
|
|
|
131
|
|
|
$this->fakeModel(7, 280, 8), |
132
|
|
|
$this->fakeModel(13, 281, 8), |
133
|
|
|
$this->fakeModel(28, 284, 8) |
134
|
|
|
]), [ [ |
135
|
|
|
'behavior_id' => 280, |
136
|
|
|
'category_id' => 8, |
137
|
|
|
'user_bhvr_id' => 7, |
138
|
|
|
'question_id' => 1, |
139
|
|
|
'answer' => 'processing emotions', |
140
|
|
|
], [ |
141
|
|
|
'behavior_id' => 280, |
142
|
|
|
'category_id' => 8, |
143
|
|
|
'user_bhvr_id' => 7, |
144
|
|
|
'question_id' => 2, |
145
|
|
|
'answer' => 'processing emotions', |
146
|
|
|
], [ |
147
|
|
|
'behavior_id' => 280, |
148
|
|
|
'category_id' => 8, |
149
|
|
|
'user_bhvr_id' => 7, |
150
|
|
|
'question_id' => 3, |
151
|
|
|
'answer' => 'processing emotions', |
152
|
|
|
], [ |
153
|
|
|
'behavior_id' => 281, |
154
|
|
|
'category_id' => 8, |
155
|
|
|
'user_bhvr_id' => 13, |
156
|
|
|
'question_id' => 1, |
157
|
|
|
'answer' => 'processing emotions', |
158
|
|
|
], [ |
159
|
|
|
'behavior_id' => 281, |
160
|
|
|
'category_id' => 8, |
161
|
|
|
'user_bhvr_id' => 13, |
162
|
|
|
'question_id' => 2, |
163
|
|
|
'answer' => 'processing emotions', |
164
|
|
|
], [ |
165
|
|
|
'behavior_id' => 281, |
166
|
|
|
'category_id' => 8, |
167
|
|
|
'user_bhvr_id' => 13, |
168
|
|
|
'question_id' => 3, |
169
|
|
|
'answer' => 'processing emotions', |
170
|
|
|
], [ |
171
|
|
|
'behavior_id' => 284, |
172
|
|
|
'category_id' => 8, |
173
|
|
|
'user_bhvr_id' => 28, |
174
|
|
|
'question_id' => 1, |
175
|
|
|
'answer' => 'processing emotions', |
176
|
|
|
], [ |
177
|
|
|
'behavior_id' => 284, |
178
|
|
|
'category_id' => 8, |
179
|
|
|
'user_bhvr_id' => 28, |
180
|
|
|
'question_id' => 2, |
181
|
|
|
'answer' => 'processing emotions', |
182
|
|
|
], [ |
183
|
|
|
'behavior_id' => 284, |
184
|
|
|
'category_id' => 8, |
185
|
|
|
'user_bhvr_id' => 28, |
186
|
|
|
'question_id' => 3, |
187
|
|
|
'answer' => 'processing emotions', |
188
|
|
|
]])); |
189
|
|
|
}); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testSaveAnswers() |
193
|
|
|
{ |
194
|
|
|
$question = $this->getMockBuilder(\common\models\Question::class) |
195
|
|
|
->setMethods(['save', 'attributes']) |
196
|
|
|
->getMock(); |
197
|
|
|
$question |
198
|
|
|
->method('attributes') |
199
|
|
|
->willReturn(['id', 'user_id', 'behavior_id', 'category_id', 'user_behavior_id', 'question', 'answer', 'date']); |
200
|
|
|
$question |
201
|
|
|
->expects($this->exactly(6)) |
202
|
|
|
->method('save') |
203
|
|
|
->willReturn(true); |
204
|
|
|
|
205
|
|
|
Yii::$container->set(\common\interfaces\QuestionInterface::class, $question); |
206
|
|
|
|
207
|
|
|
$model = $this->getMockBuilder(\site\models\QuestionForm::class) |
208
|
|
|
->setConstructorArgs([$question]) |
209
|
|
|
->setMethods(['getAnswers']) |
210
|
|
|
->getMock(); |
211
|
|
|
$model |
212
|
|
|
->expects($this->once()) |
213
|
|
|
->method('getAnswers') |
214
|
|
|
->willReturn([[ |
215
|
|
|
'behavior_id' => 280, |
216
|
|
|
'category_id' => 8, |
217
|
|
|
'user_bhvr_id' => 7, |
218
|
|
|
'question_id' => 1, |
219
|
|
|
'answer' => 'processing emotions', |
220
|
|
|
], [ |
221
|
|
|
'behavior_id' => 280, |
222
|
|
|
'category_id' => 8, |
223
|
|
|
'user_bhvr_id' => 7, |
224
|
|
|
'question_id' => 2, |
225
|
|
|
'answer' => 'processing emotions', |
226
|
|
|
], [ |
227
|
|
|
'behavior_id' => 280, |
228
|
|
|
'category_id' => 8, |
229
|
|
|
'user_bhvr_id' => 7, |
230
|
|
|
'question_id' => 3, |
231
|
|
|
'answer' => 'processing emotions', |
232
|
|
|
], [ |
233
|
|
|
'behavior_id' => 281, |
234
|
|
|
'category_id' => 8, |
235
|
|
|
'user_bhvr_id' => 13, |
236
|
|
|
'question_id' => 1, |
237
|
|
|
'answer' => 'processing emotions', |
238
|
|
|
], [ |
239
|
|
|
'behavior_id' => 281, |
240
|
|
|
'category_id' => 8, |
241
|
|
|
'user_bhvr_id' => 13, |
242
|
|
|
'question_id' => 2, |
243
|
|
|
'answer' => 'processing emotions', |
244
|
|
|
], [ |
245
|
|
|
'behavior_id' => 281, |
246
|
|
|
'category_id' => 8, |
247
|
|
|
'user_bhvr_id' => 13, |
248
|
|
|
'question_id' => 3, |
249
|
|
|
'answer' => 'processing emotions', |
250
|
|
|
]]); |
251
|
|
|
|
252
|
|
|
expect('saveAnswers should invoke save() the expected number of times and return true', $this->assertTrue($model->saveAnswers(123, []))); // doesn't matter what we pass in, we're mocking getAnswers() |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* This test doesn't work :( |
257
|
|
|
* Phpunit doesn't like to mock static methods |
258
|
|
|
* There may be a way to do this but I don't have the patience |
259
|
|
|
* to figure it out right now. |
260
|
|
|
*/ |
261
|
|
|
/* |
262
|
|
|
public function testDeleteToday() |
263
|
|
|
{ |
264
|
|
|
Yii::$container->set(\common\interfaces\TimeInterface::class, function () { |
265
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
266
|
|
|
}); |
267
|
|
|
$time = Yii::$container->get(\common\interfaces\TimeInterface::class); |
268
|
|
|
list ($start, $end) = $time->getUTCBookends($time->getLocalDate()); |
269
|
|
|
$question = $this->getMockBuilder(\common\models\Question::class) |
270
|
|
|
->setMethods(['save', 'attributes', 'deleteAll']) |
271
|
|
|
->getMock(); |
272
|
|
|
$user_id = 5; |
273
|
|
|
$question |
274
|
|
|
->expects($this->once()) |
275
|
|
|
->method('deleteAll') |
276
|
|
|
->with("user_id=:user_id |
277
|
|
|
AND date > :start_date |
278
|
|
|
AND date < :end_date", |
279
|
|
|
[ |
280
|
|
|
":user_id" => 5, |
281
|
|
|
':start_date' => $start, |
282
|
|
|
":end_date" => $end |
283
|
|
|
]); |
284
|
|
|
$container = new Container; |
285
|
|
|
$form = $container->get(\site\models\QuestionForm::class, [$question]); |
286
|
|
|
|
287
|
|
|
$form->deleteToday(5); |
288
|
|
|
} |
289
|
|
|
*/ |
290
|
|
|
|
291
|
|
|
private function fakeModel($id, $behavior_id, $category_id) { |
292
|
|
|
$class = new \stdClass; |
293
|
|
|
$class->id = $id; |
294
|
|
|
$class->behavior_id = $behavior_id; |
295
|
|
|
$class->category_id = $category_id; |
296
|
|
|
return $class; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.