Passed
Push — master ( 5f8f14...173a37 )
by William
09:55
created

UtfStringTest::testArrayAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
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 Throwable;
10
use function chr;
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 testGetCharLength(): void
57
    {
58
        $this->assertEquals(1, UtfString::getCharLength(chr(0x00))); // 00000000
59
        $this->assertEquals(1, UtfString::getCharLength(chr(0x7F))); // 01111111
60
61
        $this->assertEquals(2, UtfString::getCharLength(chr(0xC0))); // 11000000
62
        $this->assertEquals(2, UtfString::getCharLength(chr(0xDF))); // 11011111
63
64
        $this->assertEquals(3, UtfString::getCharLength(chr(0xE0))); // 11100000
65
        $this->assertEquals(3, UtfString::getCharLength(chr(0xEF))); // 11101111
66
67
        $this->assertEquals(4, UtfString::getCharLength(chr(0xF0))); // 11110000
68
        $this->assertEquals(4, UtfString::getCharLength(chr(0xF7))); // 11110111
69
70
        $this->assertEquals(5, UtfString::getCharLength(chr(0xF8))); // 11111000
71
        $this->assertEquals(5, UtfString::getCharLength(chr(0xFB))); // 11111011
72
73
        $this->assertEquals(6, UtfString::getCharLength(chr(0xFC))); // 11111100
74
        $this->assertEquals(6, UtfString::getCharLength(chr(0xFD))); // 11111101
75
    }
76
77
    public function testToString(): void
78
    {
79
        $str = new UtfString(self::TEST_PHRASE);
80
        $this->assertEquals(self::TEST_PHRASE, (string) $str);
81
    }
82
83
    /**
84
     * Test access to string.
85
     *
86
     * @param mixed $text
87
     * @param mixed $pos10
88
     * @param mixed $pos20
89
     *
90
     * @dataProvider utf8StringsProvider
91
     */
92
    public function testAccess($text, $pos10, $pos20): void
93
    {
94
        $str = new UtfString($text);
95
        $this->assertEquals($pos10, $str->offsetGet(10));
96
        $this->assertEquals($pos20, $str->offsetGet(20));
97
        $this->assertEquals($pos10, $str->offsetGet(10));
98
    }
99
100
    public function utf8StringsProvider(): array
101
    {
102
        return [
103
            'ascii' => [
104
                'abcdefghijklmnopqrstuvwxyz',
105
                'k',
106
                'u',
107
            ],
108
            'unicode' => [
109
                'áéíóúýěřťǔǐǒǎšďȟǰǩľžčǚň',
110
                'ǐ',
111
                'č',
112
            ],
113
            'emoji' => [
114
                '😂😄😃😀😊😉😍😘😚😗😂👿😮😨😱😠😡😤😖😆😋👯',
115
                '😂',
116
                '😋',
117
            ],
118
            'iso' => [
119
                "P\xf8\xed\xb9ern\xec \xbelu\xbbou\xe8k\xfd k\xf3d \xfap\xecl \xef\xe1belsk\xe9 k\xf3dy",
120
                null,
121
                null,
122
            ],
123
        ];
124
    }
125
}
126