1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Libraries\Search;
7
8use App\Exceptions\InvariantException;
9use App\Libraries\Search\BeatmapsetSearchRequestParams;
10use App\Models\User;
11use Tests\TestCase;
12
13class BeatmapsetSearchRequestParamsTest extends TestCase
14{
15 /**
16 * @dataProvider cursorsDataProvider
17 */
18 public function testCursors(?string $sort, ?array $cursor, bool $throws, ?array $expected)
19 {
20 $requestParams = [];
21 if ($sort !== null) {
22 $requestParams['sort'] = $sort;
23 }
24
25 if ($cursor !== null) {
26 $requestParams['cursor'] = $cursor;
27 }
28
29 if ($throws) {
30 $this->expectException(InvariantException::class);
31 }
32
33 $user = User::factory()->create();
34 $searchAfter = (new BeatmapsetSearchRequestParams($requestParams, $user))->searchAfter;
35
36 $this->assertSame($expected, $searchAfter);
37 }
38
39 /**
40 * @dataProvider cursorsGuestDataProvider
41 */
42 public function testCursorsGuest(?string $sort, ?array $cursor, bool $throws, ?array $expected)
43 {
44 $requestParams = [];
45 if ($sort !== null) {
46 $requestParams['sort'] = $sort;
47 }
48
49 if ($cursor !== null) {
50 $requestParams['cursor'] = $cursor;
51 }
52
53 if ($throws) {
54 $this->expectException(InvariantException::class);
55 }
56
57 $searchAfter = (new BeatmapsetSearchRequestParams($requestParams))->searchAfter;
58
59 $this->assertSame($expected, $searchAfter);
60 }
61
62 public static function cursorsDataProvider()
63 {
64 return [
65 [null, null, false, null],
66 ['', null, false, null],
67 [null, [], true, null],
68 ['', [], true, null],
69 ['title_desc', null, false, null],
70 ['title_desc', ['title.raw' => 'a'], true, null],
71 ['title_desc', ['title.raw' => 'a', 'id' => 1], false, ['a', 1]],
72 ['title_desc', ['id' => 1, 'title.raw' => 'a'], false, ['a', 1]],
73 ['title_desc', ['ignored' => 'hi', 'title.raw' => 'a', 'id' => 1], false, ['a', 1]],
74 ];
75 }
76
77 public static function cursorsGuestDataProvider()
78 {
79 return [
80 [null, null, false, null],
81 ['', null, false, null],
82 [null, [], true, null],
83 ['', [], true, null],
84 ['title_desc', null, false, null],
85 ['title_desc', ['title.raw' => 'a'], true, null],
86 ['title_desc', ['title.raw' => 'a', 'id' => 1], true, null],
87 ['title_desc', ['id' => 1, 'title.raw' => 'a'], true, null],
88 ['title_desc', ['ignored' => 'hi', 'title.raw' => 'a', 'id' => 1], true, null],
89 ];
90 }
91}