Passed
Pull Request — master (#311)
by William
12:43
created

TestCase::getTokensList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Bootstrap for tests.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Tests;
8
9
use PhpMyAdmin\SqlParser\Lexer;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\TokensList;
12
use PhpMyAdmin\SqlParser\Context;
13
use PHPUnit\Framework\TestCase as BaseTestCase;
14
15
$GLOBALS['lang'] = 'en';
16
17
/**
18
 * Implements useful methods for testing.
19
 *
20
 * @category   Tests
21
 *
22
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
23
 */
24
abstract class TestCase extends BaseTestCase
25
{
26
    /**
27
     * Gets the token list generated by lexing this query.
28
     *
29
     * @param string $query the query to be lexed
30
     *
31
     * @return TokensList
32
     */
33
    public function getTokensList($query)
34
    {
35
        $lexer = new Lexer($query);
36
37
        return $lexer->list;
38
    }
39
40
    /**
41
     * Gets the errors as an array.
42
     *
43
     * @param Lexer|Parser $obj object containing the errors
44
     *
45
     * @return array
46
     */
47
    public function getErrorsAsArray($obj)
48
    {
49
        $ret = array();
50
        foreach ($obj->errors as $err) {
51
            $ret[] = $obj instanceof Lexer
52
                ? array(
53
                    $err->getMessage(),
54
                    $err->ch,
55
                    $err->pos,
56
                    $err->getCode(),
57
                )
58
                : array(
59
                    $err->getMessage(),
60
                    $err->token,
61
                    $err->getCode()
62
                );
63
        }
64
65
        return $ret;
66
    }
67
68
    /**
69
     * Gets test's input and expected output.
70
     *
71
     * @param string $name the name of the test
72
     *
73
     * @return array
74
     */
75
    public function getData($name)
76
    {
77
        /*
78
         * The unrestricted unserialize() is needed here as we do have
79
         * serialized objects in the tests. There should be no security risk as
80
         * the test data comes with the repository.
81
         */
82
        $data = unserialize(file_get_contents('tests/data/' . $name . '.out'));
83
        $data['query'] = file_get_contents('tests/data/' . $name . '.in');
84
85
        return $data;
86
    }
87
88
    /**
89
     * Runs a test.
90
     *
91
     * @param string $name the name of the test
92
     */
93
    public function runParserTest($name)
94
    {
95
        /**
96
         * Test's data.
97
         *
98
         * @var array
99
         */
100
        $data = $this->getData($name);
101
102
        if (strpos($name, '/ansi/') !== false) {
103
            // set mode if appropriate
104
            Context::setMode('ANSI_QUOTES');
105
        }
106
107
        // Lexer.
108
        $lexer = new Lexer($data['query']);
109
        $lexerErrors = $this->getErrorsAsArray($lexer);
110
        $lexer->errors = array();
111
112
        // Parser.
113
        $parser = empty($data['parser']) ? null : new Parser($lexer->list);
114
        $parserErrors = array();
115
        if ($parser !== null) {
116
            $parserErrors = $this->getErrorsAsArray($parser);
117
            $parser->errors = array();
118
        }
119
120
        // Testing objects.
121
        $this->assertEquals($data['lexer'], $lexer);
122
        $this->assertEquals($data['parser'], $parser);
123
124
        // Testing errors.
125
        $this->assertEquals($data['errors']['parser'], $parserErrors);
126
        $this->assertEquals($data['errors']['lexer'], $lexerErrors);
127
128
        // reset mode after test run
129
        Context::setMode();
130
    }
131
}
132