Passed
Push — master ( bb9122...7ad73a )
by William
11:07
created

CharsetsTest::testGetServerCharset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 31
nc 1
nop 0
dl 0
loc 53
rs 9.424
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Tests for MySQL Charsets
4
 */
5
6
declare(strict_types=1);
7
8
namespace PhpMyAdmin\Tests;
9
10
use PhpMyAdmin\Charsets;
11
12
/**
13
 * Tests for MySQL Charsets
14
 */
15
class CharsetsTest extends AbstractTestCase
16
{
17
    protected function setUp(): void
18
    {
19
        parent::setUp();
20
        parent::setGlobalDbi();
21
        $GLOBALS['server'] = 0;
22
        $GLOBALS['cfg']['DBG']['sql'] = false;
23
        $GLOBALS['cfg']['Server']['DisableIS'] = false;
24
    }
25
26
    public function testGetServerCharset(): void
27
    {
28
        $this->dummyDbi->addResult(
29
            'SHOW SESSION VARIABLES LIKE \'character_set_server\';',
30
            [
31
                [
32
                    'character_set_server',
33
                    'utf8mb3',
34
                ],
35
            ],
36
            [
37
                'Variable_name',
38
                'Value',
39
            ]
40
        );
41
        $this->dummyDbi->addResult(
42
            'SHOW SESSION VARIABLES LIKE \'character_set_server\';',
43
            false
44
        );
45
        $this->dummyDbi->addResult(
46
            'SELECT @@character_set_server;',
47
            false
48
        );
49
        $this->dummyDbi->addResult(
50
            'SHOW SESSION VARIABLES LIKE \'character_set_server\';',
51
            false
52
        );
53
        $this->dummyDbi->addResult(
54
            'SELECT @@character_set_server;',
55
            [
56
                ['utf8mb3'],
57
            ]
58
        );
59
60
        $charset = Charsets::getServerCharset(
61
            $GLOBALS['dbi'],
62
            $GLOBALS['cfg']['Server']['DisableIS']
63
        );
64
        $this->assertSame('utf8', $charset->getName());
65
66
        $charset = Charsets::getServerCharset(
67
            $GLOBALS['dbi'],
68
            $GLOBALS['cfg']['Server']['DisableIS']
69
        );
70
        $this->assertSame('Unknown', $charset->getName());
71
72
        $charset = Charsets::getServerCharset(
73
            $GLOBALS['dbi'],
74
            $GLOBALS['cfg']['Server']['DisableIS']
75
        );
76
        $this->assertSame('utf8', $charset->getName());
77
78
        $this->assertAllQueriesConsumed();
79
    }
80
81
    public function testFindCollationByName(): void
82
    {
83
        $this->assertNull(Charsets::findCollationByName(
84
            $GLOBALS['dbi'],
85
            $GLOBALS['cfg']['Server']['DisableIS'],
86
            null
87
        ));
88
89
        $this->assertNull(Charsets::findCollationByName(
90
            $GLOBALS['dbi'],
91
            $GLOBALS['cfg']['Server']['DisableIS'],
92
            ''
93
        ));
94
95
        $this->assertNull(Charsets::findCollationByName(
96
            $GLOBALS['dbi'],
97
            $GLOBALS['cfg']['Server']['DisableIS'],
98
            'invalid'
99
        ));
100
101
        $actual = Charsets::findCollationByName(
102
            $GLOBALS['dbi'],
103
            $GLOBALS['cfg']['Server']['DisableIS'],
104
            'utf8_general_ci'
105
        );
106
107
        $this->assertInstanceOf(Charsets\Collation::class, $actual);
108
109
        $this->assertSame('utf8_general_ci', $actual->getName());
110
    }
111
}
112