Passed
Pull Request — master (#340)
by
unknown
12:20
created

ContextTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 71
dl 0
loc 139
rs 10
c 4
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A contextNames() 0 13 1
A testLoadClosest() 0 10 2
A testLoadAll() 0 7 1
A contextLoading() 0 30 1
A testMode() 0 14 1
A testLoadError() 0 3 1
A testLoad() 0 12 1
A testEscape() 0 17 1
1
<?php
2
3
namespace PhpMyAdmin\SqlParser\Tests\Lexer;
4
5
use PhpMyAdmin\SqlParser\Context;
6
use PhpMyAdmin\SqlParser\Tests\TestCase;
7
8
class ContextTest extends TestCase
9
{
10
    public function testLoad()
11
    {
12
        // Default context is 5.7.0.
13
        $this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$loadedContext);
14
        $this->assertArrayHasKey('STORED', Context::$KEYWORDS);
15
        $this->assertArrayNotHasKey('AUTHORS', Context::$KEYWORDS);
16
17
        // Restoring context.
18
        Context::load('');
19
        $this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\ContextMySql50700', Context::$defaultContext);
20
        $this->assertArrayHasKey('STORED', Context::$KEYWORDS);
21
        $this->assertArrayNotHasKey('AUTHORS', Context::$KEYWORDS);
22
    }
23
24
    /**
25
     * Test for loading closest SQL context
26
     *
27
     * @dataProvider contextLoading
28
     */
29
    public function testLoadClosest($context, $expected)
30
    {
31
        $this->assertEquals($expected, Context::loadClosest($context));
32
        if (! is_null($expected)) {
33
            $this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\Context' . $expected, Context::$loadedContext);
34
            $this->assertTrue(class_exists(Context::$loadedContext));
35
        }
36
37
        // Restoring context.
38
        Context::load('');
39
    }
40
41
    public function contextLoading()
42
    {
43
        return array(
44
            'MySQL match' => array(
45
                'MySql50500',
46
                'MySql50500',
47
            ),
48
            'MySQL strip' => array(
49
                'MySql50712',
50
                'MySql50700',
51
            ),
52
            'MySQL fallback' => array(
53
                'MySql99999',
54
                'MySql50700',
55
            ),
56
            'MariaDB match' => array(
57
                'MariaDb100000',
58
                'MariaDb100000',
59
            ),
60
            'MariaDB stripg' => array(
61
                'MariaDb109900',
62
                'MariaDb100000',
63
            ),
64
            'MariaDB fallback' => array(
65
                'MariaDb990000',
66
                'MariaDb100300',
67
            ),
68
            'Invalid' => array(
69
                'Sql',
70
                null,
71
            )
72
        );
73
    }
74
75
    /**
76
     * @dataProvider contextNames
77
     *
78
     * @param mixed $context
79
     */
80
    public function testLoadAll($context)
81
    {
82
        Context::load($context);
83
        $this->assertEquals('\\PhpMyAdmin\\SqlParser\\Contexts\\Context' . $context, Context::$loadedContext);
84
85
        // Restoring context.
86
        Context::load('');
87
    }
88
89
    public function contextNames()
90
    {
91
        return array(
92
            array('MySql50000'),
93
            array('MySql50100'),
94
            array('MySql50500'),
95
            array('MySql50600'),
96
            array('MySql50700'),
97
            array('MySql80000'),
98
            array('MariaDb100000'),
99
            array('MariaDb100100'),
100
            array('MariaDb100200'),
101
            array('MariaDb100300')
102
        );
103
    }
104
105
    /**
106
     * @expectedException \Exception
107
     * @expectedExceptionMessage Specified context ("\PhpMyAdmin\SqlParser\Contexts\ContextFoo") does not exist.
108
     */
109
    public function testLoadError()
110
    {
111
        Context::load('Foo');
112
    }
113
114
    public function testMode()
115
    {
116
        Context::setMode('REAL_AS_FLOAT,ANSI_QUOTES,IGNORE_SPACE');
117
        $this->assertEquals(
118
            Context::SQL_MODE_REAL_AS_FLOAT | Context::SQL_MODE_ANSI_QUOTES | Context::SQL_MODE_IGNORE_SPACE,
119
            Context::$MODE
120
        );
121
        Context::setMode('TRADITIONAL');
122
        $this->assertEquals(
123
            Context::SQL_MODE_TRADITIONAL,
124
            Context::$MODE
125
        );
126
        Context::setMode();
127
        $this->assertEquals(0, Context::$MODE);
128
    }
129
130
    public function testEscape()
131
    {
132
        Context::setMode('NO_ENCLOSING_QUOTES');
133
        $this->assertEquals('test', Context::escape('test'));
134
135
        Context::setMode('ANSI_QUOTES');
136
        $this->assertEquals('"test"', Context::escape('test'));
137
138
        Context::setMode();
139
        $this->assertEquals('`test`', Context::escape('test'));
140
141
        $this->assertEquals(
142
            array(
143
                '`a`',
144
                '`b`',
145
            ),
146
            Context::escape(array('a', 'b'))
147
        );
148
    }
149
}
150