1<?php 2 3declare(strict_types=1); 4 5namespace phpweb\Test\Unit\UserNotes; 6 7use PHPUnit\Framework; 8use phpweb\UserNotes\Sorter; 9use phpweb\UserNotes\UserNote; 10 11#[Framework\Attributes\CoversClass(Sorter::class)] 12#[Framework\Attributes\UsesClass(UserNote::class)] 13final class SorterTest extends Framework\TestCase 14{ 15 public function testSortSortsNotesWhenNotesAreEmpty(): void 16 { 17 $notes = []; 18 19 $sorter = new Sorter(); 20 21 $sorter->sort($notes); 22 23 self::assertSame([], $notes); 24 } 25 26 public function testSortSortsSingleNoteWithNoVotes(): void 27 { 28 $notes = [ 29 new UserNote('1', '', '', '1613487094', '', '', 0, 0), 30 ]; 31 32 $sorter = new Sorter(); 33 34 $sorter->sort($notes); 35 36 $normalized = array_map(static function (UserNote $note): array { 37 return self::normalize($note); 38 }, $notes); 39 40 $expected = [ 41 0 => [ 42 'downvotes' => 0, 43 'id' => '1', 44 'ts' => '1613487094', 45 'upvotes' => 0, 46 ], 47 ]; 48 49 self::assertSame($expected, $normalized); 50 } 51 52 public function testSortSortsSomeNotes(): void 53 { 54 $notes = [ 55 new UserNote('1', '', '', '1613487094', '', '', 0, 2), 56 new UserNote('2', '', '', '1508180150', '', '', 0, 0), 57 new UserNote('3', '', '', '1508179844', '', '', 14, 3), 58 new UserNote('4', '', '', '1508179844', '', '', 14, 3), 59 ]; 60 61 $sorter = new Sorter(); 62 63 $sorter->sort($notes); 64 65 $normalized = array_map(static function (UserNote $note): array { 66 return self::normalize($note); 67 }, $notes); 68 69 $expected = [ 70 2 => [ 71 'downvotes' => 3, 72 'id' => '3', 73 'ts' => '1508179844', 74 'upvotes' => 14, 75 ], 76 3 => [ 77 'downvotes' => 3, 78 'id' => '4', 79 'ts' => '1508179844', 80 'upvotes' => 14, 81 ], 82 1 => [ 83 'downvotes' => 0, 84 'id' => '2', 85 'ts' => '1508180150', 86 'upvotes' => 0, 87 ], 88 0 => [ 89 'downvotes' => 2, 90 'id' => '1', 91 'ts' => '1613487094', 92 'upvotes' => 0, 93 ], 94 ]; 95 96 self::assertSame($expected, $normalized); 97 } 98 99 public function testSortSortsFullNotes(): void 100 { 101 $file = file(__DIR__ . '/../../../backend/notes/d7/d7742c269d23ea86'); 102 103 $notes = []; 104 105 foreach ($file as $line) { 106 @list($id, $sect, $rate, $ts, $user, $note, $up, $down) = explode('|', $line); 107 $notes[$id] = new UserNote($id, $sect, $rate, $ts, $user, base64_decode($note, true), (int) $up, (int) $down); 108 } 109 110 $sorter = new Sorter(); 111 112 $sorter->sort($notes); 113 114 $normalized = array_map(static function (UserNote $note): array { 115 return self::normalize($note); 116 }, $notes); 117 118 $expected = [ 119 110464 => [ 120 'downvotes' => 2, 121 'id' => '110464', 122 'ts' => '1351105628', 123 'upvotes' => 10, 124 ], 125 93816 => [ 126 'downvotes' => 1, 127 'id' => '93816', 128 'ts' => '1254343074', 129 'upvotes' => 4, 130 ], 131 92849 => [ 132 'downvotes' => 1, 133 'id' => '92849', 134 'ts' => '1249997359', 135 'upvotes' => 4, 136 ], 137 70394 => [ 138 'downvotes' => 3, 139 'id' => '70394', 140 'ts' => '1160823504', 141 'upvotes' => 7, 142 ], 143 106407 => [ 144 'downvotes' => 2, 145 'id' => '106407', 146 'ts' => '1320695958', 147 'upvotes' => 5, 148 ], 149 87868 => [ 150 'downvotes' => 2, 151 'id' => '87868', 152 'ts' => '1230396484', 153 'upvotes' => 5, 154 ], 155 82229 => [ 156 'downvotes' => 1, 157 'id' => '82229', 158 'ts' => '1207066654', 159 'upvotes' => 3, 160 ], 161 80363 => [ 162 'downvotes' => 1, 163 'id' => '80363', 164 'ts' => '1200066332', 165 'upvotes' => 3, 166 ], 167 75146 => [ 168 'downvotes' => 1, 169 'id' => '75146', 170 'ts' => '1179195708', 171 'upvotes' => 3, 172 ], 173 102773 => [ 174 'downvotes' => 3, 175 'id' => '102773', 176 'ts' => '1299300266', 177 'upvotes' => 6, 178 ], 179 111422 => [ 180 'downvotes' => 2, 181 'id' => '111422', 182 'ts' => '1361224553', 183 'upvotes' => 4, 184 ], 185 94469 => [ 186 'downvotes' => 2, 187 'id' => '94469', 188 'ts' => '1257516214', 189 'upvotes' => 4, 190 ], 191 99476 => [ 192 'downvotes' => 1, 193 'id' => '99476', 194 'ts' => '1282186230', 195 'upvotes' => 2, 196 ], 197 99332 => [ 198 'downvotes' => 1, 199 'id' => '99332', 200 'ts' => '1281503061', 201 'upvotes' => 2, 202 ], 203 96926 => [ 204 'downvotes' => 1, 205 'id' => '96926', 206 'ts' => '1269330508', 207 'upvotes' => 2, 208 ], 209 93887 => [ 210 'downvotes' => 1, 211 'id' => '93887', 212 'ts' => '1254733546', 213 'upvotes' => 2, 214 ], 215 87061 => [ 216 'downvotes' => 1, 217 'id' => '87061', 218 'ts' => '1226944352', 219 'upvotes' => 2, 220 ], 221 85835 => [ 222 'downvotes' => 1, 223 'id' => '85835', 224 'ts' => '1221823065', 225 'upvotes' => 2, 226 ], 227 72466 => [ 228 'downvotes' => 1, 229 'id' => '72466', 230 'ts' => '1169208947', 231 'upvotes' => 2, 232 ], 233 69927 => [ 234 'downvotes' => 1, 235 'id' => '69927', 236 'ts' => '1159299208', 237 'upvotes' => 2, 238 ], 239 41762 => [ 240 'downvotes' => 1, 241 'id' => '41762', 242 'ts' => '1082561916', 243 'upvotes' => 2, 244 ], 245 107678 => [ 246 'downvotes' => 2, 247 'id' => '107678', 248 'ts' => '1330185500', 249 'upvotes' => 3, 250 ], 251 89788 => [ 252 'downvotes' => 2, 253 'id' => '89788', 254 'ts' => '1237801686', 255 'upvotes' => 3, 256 ], 257 74286 => [ 258 'downvotes' => 2, 259 'id' => '74286', 260 'ts' => '1175594279', 261 'upvotes' => 3, 262 ], 263 58688 => [ 264 'downvotes' => 2, 265 'id' => '58688', 266 'ts' => '1131719326', 267 'upvotes' => 3, 268 ], 269 45088 => [ 270 'downvotes' => 2, 271 'id' => '45088', 272 'ts' => '1093449145', 273 'upvotes' => 3, 274 ], 275 49739 => [ 276 'downvotes' => 0, 277 'id' => '49739', 278 'ts' => '1107758025', 279 'upvotes' => 2, 280 ], 281 108426 => [ 282 'downvotes' => 2, 283 'id' => '108426', 284 'ts' => '1335372412', 285 'upvotes' => 2, 286 ], 287 107240 => [ 288 'downvotes' => 2, 289 'id' => '107240', 290 'ts' => '1327390683', 291 'upvotes' => 2, 292 ], 293 105984 => [ 294 'downvotes' => 2, 295 'id' => '105984', 296 'ts' => '1317340435', 297 'upvotes' => 2, 298 ], 299 99440 => [ 300 'downvotes' => 4, 301 'id' => '99440', 302 'ts' => '1282058725', 303 'upvotes' => 4, 304 ], 305 93566 => [ 306 'downvotes' => 2, 307 'id' => '93566', 308 'ts' => '1253094436', 309 'upvotes' => 2, 310 ], 311 88798 => [ 312 'downvotes' => 1, 313 'id' => '88798', 314 'ts' => '1234090865', 315 'upvotes' => 1, 316 ], 317 84910 => [ 318 'downvotes' => 2, 319 'id' => '84910', 320 'ts' => '1217938595', 321 'upvotes' => 2, 322 ], 323 83914 => [ 324 'downvotes' => 1, 325 'id' => '83914', 326 'ts' => '1213760931', 327 'upvotes' => 1, 328 ], 329 78483 => [ 330 'downvotes' => 1, 331 'id' => '78483', 332 'ts' => '1192337362', 333 'upvotes' => 1, 334 ], 335 74763 => [ 336 'downvotes' => 1, 337 'id' => '74763', 338 'ts' => '1177577911', 339 'upvotes' => 1, 340 ], 341 74432 => [ 342 'downvotes' => 1, 343 'id' => '74432', 344 'ts' => '1176269720', 345 'upvotes' => 1, 346 ], 347 47879 => [ 348 'downvotes' => 1, 349 'id' => '47879', 350 'ts' => '1102066114', 351 'upvotes' => 1, 352 ], 353 40617 => [ 354 'downvotes' => 0, 355 'id' => '40617', 356 'ts' => '1078853206', 357 'upvotes' => 0, 358 ], 359 38375 => [ 360 'downvotes' => 1, 361 'id' => '38375', 362 'ts' => '1071743640', 363 'upvotes' => 1, 364 ], 365 106295 => [ 366 'downvotes' => 3, 367 'id' => '106295', 368 'ts' => '1319574977', 369 'upvotes' => 2, 370 ], 371 95875 => [ 372 'downvotes' => 3, 373 'id' => '95875', 374 'ts' => '1264517173', 375 'upvotes' => 2, 376 ], 377 102336 => [ 378 'downvotes' => 2, 379 'id' => '102336', 380 'ts' => '1297217360', 381 'upvotes' => 1, 382 ], 383 93781 => [ 384 'downvotes' => 2, 385 'id' => '93781', 386 'ts' => '1254189367', 387 'upvotes' => 1, 388 ], 389 90065 => [ 390 'downvotes' => 2, 391 'id' => '90065', 392 'ts' => '1238827503', 393 'upvotes' => 1, 394 ], 395 ]; 396 397 self::assertSame($expected, $normalized); 398 } 399 400 private static function normalize(UserNote $note): array 401 { 402 return [ 403 'downvotes' => $note->downvotes, 404 'id' => $note->id, 405 'ts' => $note->ts, 406 'upvotes' => $note->upvotes, 407 ]; 408 } 409} 410