1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace common\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use Codeception\Specify; |
7
|
|
|
|
8
|
|
|
date_default_timezone_set('UTC'); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Time test |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
class UserBehaviorTest extends \Codeception\Test\Unit { |
15
|
|
|
use Specify; |
16
|
|
|
|
17
|
|
|
public $singleSimpleBehaviorNoBehavior = [ |
18
|
|
|
[ |
19
|
|
|
'id' => 396, |
20
|
|
|
'user_id' => 2, |
21
|
|
|
'behavior_id' => 107, |
22
|
|
|
'date' => '2016-06-17 04:12:43', |
23
|
|
|
], |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public $badSingleSimpleBehaviorNoBehavior = [ |
27
|
|
|
[ |
28
|
|
|
'id' => 396, |
29
|
|
|
'user_id' => 2, |
30
|
|
|
'behavior_id' => 99999, |
31
|
|
|
'date' => '2016-06-17 04:12:43', |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
public function setUp() { |
35
|
|
|
// pull in test data |
36
|
|
|
$data = require(__DIR__.'/../data/checkinData.php'); |
37
|
|
|
$this->singleBhvr = $data['singleBhvr']; |
|
|
|
|
38
|
|
|
$this->manyBhvrs = $data['manyBhvrs']; |
|
|
|
|
39
|
|
|
$this->allBhvrs = $data['allBhvrs']; |
|
|
|
|
40
|
|
|
$this->multipleDates = $data['multipleDates']; |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
43
|
|
|
$this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser'); |
44
|
|
|
$this->container->set('common\interfaces\QuestionInterface', '\site\tests\_support\MockQuestion'); |
45
|
|
|
$this->container->set('common\interfaces\BehaviorInterface', 'common\models\Behavior'); |
46
|
|
|
$this->container->set('common\interfaces\TimeInterface', function () { |
47
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$time = $this->container->get('common\interfaces\TimeInterface'); |
51
|
|
|
$behavior = $this->container->get('common\interfaces\BehaviorInterface'); |
52
|
|
|
$user = $this->container->get('common\interfaces\UserInterface'); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->user_behavior = $this |
|
|
|
|
55
|
|
|
->getMockBuilder("common\models\UserBehavior") |
56
|
|
|
->setConstructorArgs([$behavior, $time]) |
57
|
|
|
->setMethods(['getIsNewRecord', 'save', 'getBehaviorsByDate', 'getBehaviorsWithCounts']) |
58
|
|
|
->getMock(); |
59
|
|
|
parent::setUp(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function tearDown() { |
63
|
|
|
$this->user_behavior = null; |
|
|
|
|
64
|
|
|
parent::tearDown(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testDecorate() { |
68
|
|
|
expect('decorate should add Behavior data to an array of UserBehaviors', $this->assertEquals($this->user_behavior->decorate($this->singleSimpleBehaviorNoBehavior), |
|
|
|
|
69
|
|
|
[['id' => 396, |
70
|
|
|
'user_id' => 2, |
71
|
|
|
'behavior_id' => 107, |
72
|
|
|
'date' => '2016-06-17 04:12:43', |
73
|
|
|
'behavior' => [ |
74
|
|
|
'id' => 107, |
75
|
|
|
'name' => 'numb', |
76
|
|
|
'category_id' => 6, |
77
|
|
|
]]])); |
78
|
|
|
|
79
|
|
|
expect('decorate SHOULD NOT add Behavior data when the provided behavior_id is invalid', |
80
|
|
|
$this->assertEquals( |
|
|
|
|
81
|
|
|
$this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior), |
82
|
|
|
[['id' => 396, |
83
|
|
|
'user_id' => 2, |
84
|
|
|
'behavior_id' => 99999, |
85
|
|
|
'date' => '2016-06-17 04:12:43']])); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testDecorateWithCategory() { |
89
|
|
|
expect('decorate should add Behavior data and Category data to an array of UserBehaviors', |
90
|
|
|
$this->assertEquals( |
|
|
|
|
91
|
|
|
$this->user_behavior->decorateWithCategory($this->singleSimpleBehaviorNoBehavior), |
92
|
|
|
[['id' => 396, |
93
|
|
|
'user_id' => 2, |
94
|
|
|
'behavior_id' => 107, |
95
|
|
|
'date' => '2016-06-17 04:12:43', |
96
|
|
|
'behavior' => [ |
97
|
|
|
'id' => 107, |
98
|
|
|
'name' => 'numb', |
99
|
|
|
'category_id' => 6, |
100
|
|
|
'category' => [ |
101
|
|
|
'id' => 6, |
102
|
|
|
'name' => 'Exhausted', |
103
|
|
|
] |
104
|
|
|
]]])); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetTopBehaviors() { |
108
|
|
|
$this |
109
|
|
|
->user_behavior |
110
|
|
|
->method('getBehaviorsWithCounts') |
111
|
|
|
->willReturnOnConsecutiveCalls([], [ |
112
|
|
|
[ |
113
|
|
|
'user_id' => 1, |
114
|
|
|
'behavior_id' => 1, |
115
|
|
|
'count' => 5 |
116
|
|
|
], [ |
117
|
|
|
'user_id' => 1, |
118
|
|
|
'behavior_id' => 2, |
119
|
|
|
'count' => 4 |
120
|
|
|
], [ |
121
|
|
|
'user_id' => 1, |
122
|
|
|
'behavior_id' => 3, |
123
|
|
|
'count' => 3 |
124
|
|
|
] |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
expect('getTopBehaviors to return the empty array if the user has not logged any behaviors', $this->assertEquals([], $this->user_behavior->getTopBehaviors())); |
|
|
|
|
128
|
|
|
expect('getTopBehaviors to return embelished data corresponding to the most commonly selected behaviors', $this->assertEquals( |
|
|
|
|
129
|
|
|
[[ |
130
|
|
|
'user_id' => 1, |
131
|
|
|
'behavior_id' => 1, |
132
|
|
|
'count' => 5, |
133
|
|
|
'behavior' => [ |
134
|
|
|
'id' => 1, |
135
|
|
|
'name' => 'no current secrets', |
136
|
|
|
'category_id' => 1, |
137
|
|
|
'category' => [ |
138
|
|
|
'id' => 1, |
139
|
|
|
'name' => 'Restoration', |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
], [ |
143
|
|
|
'user_id' => 1, |
144
|
|
|
'behavior_id' => 2, |
145
|
|
|
'count' => 4, |
146
|
|
|
'behavior' => [ |
147
|
|
|
'id' => 2, |
148
|
|
|
'name' => 'resolving problems', |
149
|
|
|
'category_id' => 1, |
150
|
|
|
'category' => [ |
151
|
|
|
'id' => 1, |
152
|
|
|
'name' => 'Restoration', |
153
|
|
|
], |
154
|
|
|
], |
155
|
|
|
], [ |
156
|
|
|
'user_id' => 1, |
157
|
|
|
'behavior_id' => 3, |
158
|
|
|
'count' => 3, |
159
|
|
|
'behavior' => [ |
160
|
|
|
'id' => 3, |
161
|
|
|
'name' => 'identifying fears and feelings', |
162
|
|
|
'category_id' => 1, |
163
|
|
|
'category' => [ |
164
|
|
|
'id' => 1, |
165
|
|
|
'name' => 'Restoration', |
166
|
|
|
], |
167
|
|
|
], |
168
|
|
|
]], $this->user_behavior->getTopBehaviors())); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetBehaviorsByCategory() { |
172
|
|
|
$this |
173
|
|
|
->user_behavior |
174
|
|
|
->method('getBehaviorsWithCounts') |
175
|
|
|
->willReturnOnConsecutiveCalls([], [ |
176
|
|
|
[ |
177
|
|
|
'user_id' => 1, |
178
|
|
|
'behavior_id' => 1, |
179
|
|
|
'count' => 5 |
180
|
|
|
], [ |
181
|
|
|
'user_id' => 1, |
182
|
|
|
'behavior_id' => 20, |
183
|
|
|
'count' => 4 |
184
|
|
|
], [ |
185
|
|
|
'user_id' => 1, |
186
|
|
|
'behavior_id' => 50, |
187
|
|
|
'count' => 3 |
188
|
|
|
] |
189
|
|
|
]); |
190
|
|
|
|
191
|
|
|
expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([], $this->user_behavior->getBehaviorsByCategory())); |
|
|
|
|
192
|
|
|
expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([ |
|
|
|
|
193
|
|
|
1 => [ |
194
|
|
|
'name' => 'Restoration', |
195
|
|
|
'count' => 5, |
196
|
|
|
'color' => '#008000', |
197
|
|
|
'highlight' => '#199919' |
198
|
|
|
], |
199
|
|
|
2 => [ |
200
|
|
|
'name' => 'Forgetting Priorities', |
201
|
|
|
'count' => 4, |
202
|
|
|
'color' => '#4CA100', |
203
|
|
|
'highlight' => '#61B219' |
204
|
|
|
], |
205
|
|
|
4 => [ |
206
|
|
|
'name' => 'Speeding Up', |
207
|
|
|
'count' => 3, |
208
|
|
|
'color' => '#E5E500', |
209
|
|
|
'highlight' => '#E5E533' |
210
|
|
|
] |
211
|
|
|
], $this->user_behavior->getBehaviorsByCategory())); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|