summaryrefslogtreecommitdiffstats
path: root/tests/Utils/CLITest.php
blob: 435a2c8ddb109a9438819686c9377f54b1808db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

namespace PhpMyAdmin\SqlParser\Tests\Utils;

use PhpMyAdmin\SqlParser\Tests\TestCase;

class CLITest extends TestCase
{
    private function getCLI($getopt)
    {
        $cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(array('getopt'))->getMock();
        $cli->method('getopt')->willReturn($getopt);

        return $cli;
    }

    /**
     * @dataProvider highlightParams
     */
    public function testRunHighlight($getopt, $output, $result)
    {
        $cli = $this->getCLI($getopt);
        $this->expectOutputString($output);
        $this->assertEquals($result, $cli->runHighlight());
    }

    public function highlightParams()
    {
        return array(
            array(
                array('q' => 'SELECT 1'),
                "\x1b[35mSELECT\n    \x1b[92m1\x1b[0m\n",
                0,
            ),
            array(
                array('query' => 'SELECT 1'),
                "\x1b[35mSELECT\n    \x1b[92m1\x1b[0m\n",
                0,
            ),
            array(
                array('q' => 'SELECT /* comment */ 1 /* other */', 'f' => 'text'),
                "SELECT\n    /* comment */ 1 /* other */\n",
                0,
            ),
            array(
                array('q' => 'SELECT 1', 'f' => 'foo'),
                "ERROR: Invalid value for format!\n",
                1,
            ),
            array(
                array('q' => 'SELECT 1', 'f' => 'html'),
                '<span class="sql-reserved">SELECT</span>' . '<br/>' .
                '&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>' . "\n",
                0,
            ),
            array(
                array('h' => true),
                'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n",
                0,
            ),
            array(
                array(),
                'ERROR: Missing parameters!' . "\n" .
                'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n",
                1,
            ),
            array(
                false,
                '',
                1,
            ),
        );
    }

    /**
     * @dataProvider lintParams
     */
    public function testRunLint($getopt, $output, $result)
    {
        $cli = $this->getCLI($getopt);
        $this->expectOutputString($output);
        $this->assertEquals($result, $cli->runLint());
    }

    public function lintParams()
    {
        return array(
            array(
                array('q' => 'SELECT 1'),
                '',
                0,
            ),
            array(
                array('query' => 'SELECT 1'),
                '',
                0,
            ),
            array(
                array('q' => 'SELECT SELECT'),
                '#1: An expression was expected. (near "SELECT" at position 7)' . "\n" .
                '#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" .
                '#3: An expression was expected. (near "" at position 0)' . "\n",
                10,
            ),
            array(
                array('h' => true),
                'Usage: lint-query --query SQL' . "\n",
                0,
            ),
            array(
                array(),
                'ERROR: Missing parameters!' . "\n" .
                'Usage: lint-query --query SQL' . "\n",
                1,
            ),
            array(
                false,
                '',
                1,
            ),
        );
    }
}