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
6declare(strict_types=1);
7
8namespace Tests;
9
10use App\Models\Country;
11
12class HelpersTest extends TestCase
13{
14 /**
15 * @dataProvider dataForGetStringSplit
16 */
17 public function testGetStringSplit(string $input, array $expected): void
18 {
19 $this->assertSame($expected, get_string_split($input));
20 }
21
22 /**
23 * @dataProvider dataForClassWithModifiers
24 */
25 public function testClassWithModifiers($class, $modifiers, $expected): void
26 {
27 $this->assertSame($expected, class_with_modifiers($class, ...$modifiers));
28 }
29
30 public function testIsSqlUniqueException(): void
31 {
32 $baseParams = [
33 'rankedscore' => 0,
34 'playcount' => 0,
35 'usercount' => 0,
36 ];
37
38 (new Country([
39 ...$baseParams,
40 'acronym' => 'AA',
41 'name' => '1',
42 ]))->saveOrExplode();
43
44 try {
45 (new Country([
46 ...$baseParams,
47 'acronym' => 'AA',
48 'name' => '2',
49 ]))->saveOrExplode();
50 } catch (\Throwable $e) {
51 $exception = $e;
52 }
53
54 $this->assertTrue(is_sql_unique_exception($exception));
55 }
56
57 /**
58 * @dataProvider dataForGetLengthSeconds
59 */
60 public function testGetLengthSeconds(string $input, array $expected): void
61 {
62 $this->assertSame($expected, get_length_seconds($input));
63 }
64
65 public static function dataForGetLengthSeconds(): array
66 {
67 return [
68 ['23s', ['value' => 23.0, 'min_scale' => 1]],
69 ['9m', ['value' => 9.0 * 60, 'min_scale' => 60]],
70 ['0.25h', ['value' => 15.0 * 60, 'min_scale' => 3600]],
71 ['1h20s', ['value' => (60 * 60) + 20.0, 'min_scale' => 1]],
72 ['6h5m', ['value' => (60 * 60 * 6) + (5.0 * 60), 'min_scale' => 60]],
73 ['6', ['value' => 6.0, 'min_scale' => 1]],
74 ['1:2:3', ['value' => (60 * 60) + (2 * 60) + 3.0, 'min_scale' => 1]],
75 ['1h2m3.5s', ['value' => (60 * 60) + (2 * 60) + 3.5, 'min_scale' => 1]],
76 ['600ms', ['value' => 600 * 0.001, 'min_scale' => 0.001]],
77 ];
78 }
79
80 public static function dataForClassWithModifiers(): array
81 {
82 return [
83 'no modifiers' =>
84 ['cl', [], 'cl'],
85 'string (one)' =>
86 ['cl', ['hello'], 'cl cl--hello'],
87 'string (two)' =>
88 ['cl', ['hello', 'world'], 'cl cl--hello cl--world'],
89 'array (empty)' =>
90 ['cl', [[]], 'cl'],
91 'array (one) with two strings' =>
92 ['cl', [['hello', 'world']], 'cl cl--hello cl--world'],
93 'array (one) with one string and two nulls' =>
94 ['cl', [[null, 'hello', null]], 'cl cl--hello'],
95 'array (two) with mixed strings and null' =>
96 ['cl', [['hello', null, 'world'], ['foo', null]], 'cl cl--hello cl--world cl--foo'],
97 'hash (one) with two true strings' =>
98 ['cl', [['hello' => true, 'world' => true]], 'cl cl--hello cl--world'],
99 'hash (one) with one true string and one false string' =>
100 ['cl', [['hello' => true, 'world' => false]], 'cl cl--hello'],
101 'hash (two) with mixed strings each' =>
102 ['cl', [['hello' => true, 'world' => false], ['foo' => false, 'bar' => true]], 'cl cl--hello cl--bar'],
103 'mixed' =>
104 ['cl', ['hello', ['world' => true, 'foo' => false], ['bar', null]], 'cl cl--hello cl--world cl--bar'],
105 ];
106 }
107
108 public static function dataForGetStringSplit(): array
109 {
110 return [
111 ["hello\nworld\n!", ['hello', 'world', '!']],
112 ["hello\rworld\n!", ['hello', 'world', '!']],
113 ["hello\r\nworld\r!", ['hello', 'world', '!']],
114 [" hello \r\n world \n ! ", ['hello', 'world', '!']],
115 ['hello world', ['hello world']],
116 ["\nhello world\n\n\r", ['hello world']],
117 ];
118 }
119}