Passed
Pull Request — master (#506)
by
unknown
03:01
created

UtfStringTest::testSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Misc;
6
7
use PhpMyAdmin\SqlParser\Tests\TestCase;
8
use PhpMyAdmin\SqlParser\UtfString;
9
use PHPUnit\Framework\Attributes\DataProvider;
10
use Throwable;
11
12
class UtfStringTest extends TestCase
13
{
14
    /**
15
     * Sample phrase in French.
16
     */
17
    public const TEST_PHRASE = 'Les naïfs ægithales hâtifs pondant à Noël où il '
18
        . 'gèle sont sûrs d\'être déçus en voyant leurs drôles d\'œufs abîmés.';
19
20
    /**
21
     * The length of the sample phrase.
22
     */
23
    public const TEST_PHRASE_LEN = 113;
24
25
    public function testArrayAccess(): void
26
    {
27
        $str = new UtfString(self::TEST_PHRASE);
28
29
        // offsetExists
30
        $this->assertArrayHasKey(self::TEST_PHRASE_LEN - 1, $str);
31
        $this->assertArrayNotHasKey(-1, $str);
32
        $this->assertArrayNotHasKey(self::TEST_PHRASE_LEN, $str);
33
34
        // offsetGet
35
        $this->assertEquals('.', $str[self::TEST_PHRASE_LEN - 1]);
36
        $this->assertNull($str[-1]);
37
        $this->assertNull($str[self::TEST_PHRASE_LEN]);
38
    }
39
40
    public function testSet(): void
41
    {
42
        $this->expectExceptionMessage('Not implemented.');
43
        $this->expectException(Throwable::class);
44
        $str = new UtfString('');
45
        $str[0] = 'a';
46
    }
47
48
    public function testUnset(): void
49
    {
50
        $this->expectExceptionMessage('Not implemented.');
51
        $this->expectException(Throwable::class);
52
        $str = new UtfString('');
53
        unset($str[0]);
54
    }
55
56
    public function testToString(): void
57
    {
58
        $str = new UtfString(self::TEST_PHRASE);
59
        $this->assertEquals(self::TEST_PHRASE, (string) $str);
60
    }
61
62
    /**
63
     * Test access to string.
64
     */
65
    #[DataProvider('utf8StringsProvider')]
66
    public function testAccess(string $text, string|null $pos10, string|null $pos20): void
67
    {
68
        $str = new UtfString($text);
69
        $this->assertEquals($pos10, $str->offsetGet(10));
70
        $this->assertEquals($pos20, $str->offsetGet(20));
71
        $this->assertEquals($pos10, $str->offsetGet(10));
72
    }
73
74
    /**
75
     * @return array<string, array<int, string|null>>
76
     * @psalm-return array<string, array{string, (string|null), (string|null)}>
77
     */
78
    public static function utf8StringsProvider(): array
79
    {
80
        return [
81
            'ascii' => [
82
                'abcdefghijklmnopqrstuvwxyz',
83
                'k',
84
                'u',
85
            ],
86
            'unicode' => [
87
                'áéíóúýěřťǔǐǒǎšďȟǰǩľžčǚň',
88
                'ǐ',
89
                'č',
90
            ],
91
            'emoji' => [
92
                '🦋😄😃😀😊😉😍😘😚😗😂👿😮😨😱😠😡😤😖😆😋👯',
93
                '😂',
94
                '😋',
95
            ],
96
            'iso' => [
97
                "P\xf8\xed\xb9ern\xec \xbelu\xbbou\xe8k\xfd k\xf3d \xfap\xecl \xef\xe1belsk\xe9 k\xf3dy",
98
                null,
99
                null,
100
            ],
101
            'random' => [
102
                'xℤⅿↈⅬ⅀ↆℜℝ⅗ℾ℧ⅰℓⅯⅵⅣ⅒21⅞',
103
                'ℾ',
104
                '⅞',
105
            ],
106
        ];
107
    }
108
}
109