8
8
namespace Tests \Models \Multiplayer ;
9
9
10
10
use App \Models \Multiplayer \PlaylistItem ;
11
+ use App \Models \Multiplayer \PlaylistItemUserHighScore ;
11
12
use App \Models \Multiplayer \Room ;
12
13
use App \Models \Multiplayer \UserScoreAggregate ;
13
14
use App \Models \User ;
16
17
17
18
class UserScoreAggregateTest extends TestCase
18
19
{
19
- private Room $ room ;
20
+ private static function createPlaylistItem (Room $ room ): PlaylistItem
21
+ {
22
+ return PlaylistItem::factory ()->create ([
23
+ 'owner_id ' => $ room ->host ,
24
+ 'room_id ' => $ room ,
25
+ ]);
26
+ }
20
27
21
28
public function testAddingHigherScore (): void
22
29
{
23
30
$ user = User::factory ()->create ();
24
- $ playlistItem = $ this ->createPlaylistItem ();
31
+ $ room = Room::factory ()->create ();
32
+ $ playlistItem = self ::createPlaylistItem ($ room );
25
33
26
34
// first play
27
35
$ scoreLink = $ this ->roomAddPlay ($ user , $ playlistItem , [
@@ -30,7 +38,7 @@ public function testAddingHigherScore(): void
30
38
'total_score ' => 10 ,
31
39
]);
32
40
33
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
41
+ $ agg = UserScoreAggregate::new ($ user , $ room );
34
42
$ this ->assertSame (1 , $ agg ->completed );
35
43
$ this ->assertSame (0.5 , $ agg ->accuracy );
36
44
$ this ->assertSame (10 , $ agg ->total_score );
@@ -53,7 +61,8 @@ public function testAddingHigherScore(): void
53
61
public function testAddingLowerScore (): void
54
62
{
55
63
$ user = User::factory ()->create ();
56
- $ playlistItem = $ this ->createPlaylistItem ();
64
+ $ room = Room::factory ()->create ();
65
+ $ playlistItem = self ::createPlaylistItem ($ room );
57
66
58
67
// first play
59
68
$ scoreLink = $ this ->roomAddPlay ($ user , $ playlistItem , [
@@ -62,7 +71,7 @@ public function testAddingLowerScore(): void
62
71
'total_score ' => 10 ,
63
72
]);
64
73
65
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
74
+ $ agg = UserScoreAggregate::new ($ user , $ room );
66
75
$ this ->assertSame (1 , $ agg ->completed );
67
76
$ this ->assertSame (0.5 , $ agg ->accuracy );
68
77
$ this ->assertSame (10 , $ agg ->total_score );
@@ -86,7 +95,8 @@ public function testAddingEqualScore(): void
86
95
{
87
96
$ firstUser = User::factory ()->create ();
88
97
$ secondUser = User::factory ()->create ();
89
- $ playlistItem = $ this ->createPlaylistItem ();
98
+ $ room = Room::factory ()->create ();
99
+ $ playlistItem = self ::createPlaylistItem ($ room );
90
100
91
101
// first user sets play
92
102
$ firstUserPlay = $ this ->roomAddPlay ($ firstUser , $ playlistItem , [
@@ -95,7 +105,7 @@ public function testAddingEqualScore(): void
95
105
'total_score ' => 10 ,
96
106
]);
97
107
98
- $ firstUserAgg = UserScoreAggregate::new ($ firstUser , $ this -> room );
108
+ $ firstUserAgg = UserScoreAggregate::new ($ firstUser , $ room );
99
109
$ this ->assertSame (1 , $ firstUserAgg ->completed );
100
110
$ this ->assertSame (0.5 , $ firstUserAgg ->accuracy );
101
111
$ this ->assertSame (10 , $ firstUserAgg ->total_score );
@@ -109,7 +119,7 @@ public function testAddingEqualScore(): void
109
119
'total_score ' => 10 ,
110
120
]);
111
121
112
- $ secondUserAgg = UserScoreAggregate::new ($ secondUser , $ this -> room );
122
+ $ secondUserAgg = UserScoreAggregate::new ($ secondUser , $ room );
113
123
$ this ->assertSame (1 , $ secondUserAgg ->completed );
114
124
$ this ->assertSame (0.5 , $ secondUserAgg ->accuracy );
115
125
$ this ->assertSame (10 , $ secondUserAgg ->total_score );
@@ -137,8 +147,9 @@ public function testAddingEqualScore(): void
137
147
public function testAddingMultiplePlaylistItems (): void
138
148
{
139
149
$ user = User::factory ()->create ();
140
- $ playlistItem = $ this ->createPlaylistItem ();
141
- $ playlistItem2 = $ this ->createPlaylistItem ();
150
+ $ room = Room::factory ()->create ();
151
+ $ playlistItem = self ::createPlaylistItem ($ room );
152
+ $ playlistItem2 = self ::createPlaylistItem ($ room );
142
153
143
154
// first playlist item
144
155
$ this ->roomAddPlay ($ user , $ playlistItem , [
@@ -147,7 +158,7 @@ public function testAddingMultiplePlaylistItems(): void
147
158
'total_score ' => 10 ,
148
159
]);
149
160
150
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
161
+ $ agg = UserScoreAggregate::new ($ user , $ room );
151
162
$ this ->assertSame (1 , $ agg ->completed );
152
163
$ this ->assertSame (0.5 , $ agg ->accuracy );
153
164
$ this ->assertSame (0.5 , $ agg ->averageAccuracy ());
@@ -171,10 +182,11 @@ public function testAddingMultiplePlaylistItems(): void
171
182
public function testStartingPlayIncreasesAttempts (): void
172
183
{
173
184
$ user = User::factory ()->create ();
174
- $ playlistItem = $ this ->createPlaylistItem ();
185
+ $ room = Room::factory ()->create ();
186
+ $ playlistItem = self ::createPlaylistItem ($ room );
175
187
176
188
static ::roomStartPlay ($ user , $ playlistItem );
177
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
189
+ $ agg = UserScoreAggregate::new ($ user , $ room );
178
190
179
191
$ this ->assertSame (1 , $ agg ->attempts );
180
192
$ this ->assertSame (0 , $ agg ->completed );
@@ -183,22 +195,23 @@ public function testStartingPlayIncreasesAttempts(): void
183
195
public function testFailedScoresAreAttemptsOnly (): void
184
196
{
185
197
$ user = User::factory ()->create ();
186
- $ playlistItem = $ this ->createPlaylistItem ();
198
+ $ room = Room::factory ()->create ();
199
+ $ playlistItem = self ::createPlaylistItem ($ room );
187
200
188
201
$ this ->roomAddPlay ($ user , $ playlistItem , [
189
202
'accuracy ' => 0.1 ,
190
203
'passed ' => false ,
191
204
'total_score ' => 10 ,
192
205
]);
193
206
194
- $ playlistItem2 = $ this -> createPlaylistItem ();
207
+ $ playlistItem2 = self :: createPlaylistItem ($ room );
195
208
$ this ->roomAddPlay ($ user , $ playlistItem2 , [
196
209
'accuracy ' => 1 ,
197
210
'passed ' => true ,
198
211
'total_score ' => 1 ,
199
212
]);
200
213
201
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
214
+ $ agg = UserScoreAggregate::new ($ user , $ room );
202
215
203
216
$ this ->assertSame (2 , $ agg ->attempts );
204
217
$ this ->assertSame (1 , $ agg ->completed );
@@ -209,15 +222,16 @@ public function testFailedScoresAreAttemptsOnly(): void
209
222
public function testPassedScoresIncrementsCompletedCount (): void
210
223
{
211
224
$ user = User::factory ()->create ();
212
- $ playlistItem = $ this ->createPlaylistItem ();
225
+ $ room = Room::factory ()->create ();
226
+ $ playlistItem = self ::createPlaylistItem ($ room );
213
227
214
228
$ this ->roomAddPlay ($ user , $ playlistItem , [
215
229
'accuracy ' => 1 ,
216
230
'passed ' => true ,
217
231
'total_score ' => 1 ,
218
232
]);
219
233
220
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
234
+ $ agg = UserScoreAggregate::new ($ user , $ room );
221
235
222
236
$ this ->assertSame (1 , $ agg ->completed );
223
237
$ this ->assertSame (1 , $ agg ->total_score );
@@ -226,8 +240,9 @@ public function testPassedScoresIncrementsCompletedCount(): void
226
240
public function testPassedScoresAreAveragedInTransformer (): void
227
241
{
228
242
$ user = User::factory ()->create ();
229
- $ playlistItem = $ this ->createPlaylistItem ();
230
- $ playlistItem2 = $ this ->createPlaylistItem ();
243
+ $ room = Room::factory ()->create ();
244
+ $ playlistItem = self ::createPlaylistItem ($ room );
245
+ $ playlistItem2 = self ::createPlaylistItem ($ room );
231
246
232
247
$ this ->roomAddPlay ($ user , $ playlistItem , [
233
248
'accuracy ' => 0.1 ,
@@ -253,7 +268,7 @@ public function testPassedScoresAreAveragedInTransformer(): void
253
268
'total_score ' => 1 ,
254
269
]);
255
270
256
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
271
+ $ agg = UserScoreAggregate::new ($ user , $ room );
257
272
258
273
$ result = json_item ($ agg , new UserScoreAggregateTransformer ());
259
274
@@ -262,14 +277,15 @@ public function testPassedScoresAreAveragedInTransformer(): void
262
277
263
278
public function testRecalculate (): void
264
279
{
265
- $ playlistItem = $ this ->createPlaylistItem ();
280
+ $ room = Room::factory ()->create ();
281
+ $ playlistItem = self ::createPlaylistItem ($ room );
266
282
$ user = User::factory ()->create ();
267
283
$ this ->roomAddPlay ($ user , $ playlistItem , [
268
284
'accuracy ' => 0.3 ,
269
285
'passed ' => true ,
270
286
'total_score ' => 1 ,
271
287
]);
272
- $ agg = UserScoreAggregate::new ($ user , $ this -> room );
288
+ $ agg = UserScoreAggregate::new ($ user , $ room );
273
289
$ agg ->recalculate ();
274
290
$ agg ->refresh ();
275
291
@@ -279,18 +295,55 @@ public function testRecalculate(): void
279
295
$ this ->assertSame (1 , $ agg ->completed );
280
296
}
281
297
282
- protected function setUp (): void
298
+ public function testFailedScoresCountToAggregateInRealtime (): void
283
299
{
284
- parent ::setUp ();
300
+ $ user = User::factory ()->create ();
301
+ $ room = Room::factory ()->create ([
302
+ 'type ' => 'head_to_head ' ,
303
+ ]);
304
+ $ playlistItem = self ::createPlaylistItem ($ room );
285
305
286
- $ this ->room = Room::factory ()->create ();
306
+ $ scoreLink = $ this ->roomAddPlay ($ user , $ playlistItem , [
307
+ 'accuracy ' => 0.6 ,
308
+ 'passed ' => false ,
309
+ 'total_score ' => 1000 ,
310
+ ]);
311
+
312
+ $ agg = UserScoreAggregate::new ($ user , $ room );
313
+
314
+ $ this ->assertSame (1 , $ agg ->completed );
315
+ $ this ->assertSame (1000 , $ agg ->total_score );
316
+
317
+ $ userHighScore = PlaylistItemUserHighScore::where ([
318
+ 'playlist_item_id ' => $ playlistItem ->getKey (),
319
+ 'user_id ' => $ user ->getKey (),
320
+ ])->first ();
321
+ $ this ->assertSame ($ scoreLink ->score_id , $ userHighScore ->score_id );
287
322
}
288
323
289
- private function createPlaylistItem (): PlaylistItem
324
+ public function testFailedScoresDoNotCountToAggregateInPlaylists (): void
290
325
{
291
- return PlaylistItem ::factory ()->create ([
292
- ' owner_id ' => $ this -> room -> host ,
293
- 'room_id ' => $ this -> room ,
326
+ $ user = User ::factory ()->create ();
327
+ $ room = Room:: factory ()-> create ([
328
+ 'type ' => ' playlists ' ,
294
329
]);
330
+ $ playlistItem = self ::createPlaylistItem ($ room );
331
+
332
+ $ this ->roomAddPlay ($ user , $ playlistItem , [
333
+ 'accuracy ' => 0.6 ,
334
+ 'passed ' => false ,
335
+ 'total_score ' => 1000 ,
336
+ ]);
337
+
338
+ $ agg = UserScoreAggregate::new ($ user , $ room );
339
+
340
+ $ this ->assertSame (0 , $ agg ->completed );
341
+ $ this ->assertSame (0 , $ agg ->total_score );
342
+
343
+ $ userHighScore = PlaylistItemUserHighScore::where ([
344
+ 'playlist_item_id ' => $ playlistItem ->getKey (),
345
+ 'user_id ' => $ user ->getKey (),
346
+ ])->first ();
347
+ $ this ->assertNull ($ userHighScore ->score_id );
295
348
}
296
349
}
0 commit comments