blob: fb6af8d6728506e8df2ecd3f1caa18f37d209739 (
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
|
<?php
/**
* Bootstrap for tests.
*/
namespace PhpMyAdmin\SqlParser\Tests;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\TokensList;
use PHPUnit\Framework\TestCase as BaseTestCase;
$GLOBALS['lang'] = 'en';
/**
* Implements useful methods for testing.
*
* @category Tests
*
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
*/
abstract class TestCase extends BaseTestCase
{
/**
* Gets the token list generated by lexing this query.
*
* @param string $query the query to be lexed
*
* @return TokensList
*/
public function getTokensList($query)
{
$lexer = new Lexer($query);
return $lexer->list;
}
/**
* Gets the errors as an array.
*
* @param Lexer|Parser $obj object containing the errors
*
* @return array
*/
public function getErrorsAsArray($obj)
{
$ret = array();
foreach ($obj->errors as $err) {
$ret[] = $obj instanceof Lexer
? array(
$err->getMessage(),
$err->ch,
$err->pos,
$err->getCode(),
)
: array(
$err->getMessage(),
$err->token,
$err->getCode()
);
}
return $ret;
}
/**
* Gets test's input and expected output.
*
* @param string $name the name of the test
*
* @return array
*/
public function getData($name)
{
/*
* The unrestricted unserialize() is needed here as we do have
* serialized objects in the tests. There should be no security risk as
* the test data comes with the repository.
*/
$data = unserialize(file_get_contents('tests/data/' . $name . '.out'));
$data['query'] = file_get_contents('tests/data/' . $name . '.in');
return $data;
}
/**
* Runs a test.
*
* @param string $name the name of the test
*/
public function runParserTest($name)
{
/**
* Test's data.
*
* @var array
*/
$data = $this->getData($name);
// Lexer.
$lexer = new Lexer($data['query']);
$lexerErrors = $this->getErrorsAsArray($lexer);
$lexer->errors = array();
// Parser.
$parser = empty($data['parser']) ? null : new Parser($lexer->list);
$parserErrors = array();
if ($parser !== null) {
$parserErrors = $this->getErrorsAsArray($parser);
$parser->errors = array();
}
// Testing objects.
$this->assertEquals($data['lexer'], $lexer);
$this->assertEquals($data['parser'], $parser);
// Testing errors.
$this->assertEquals($data['errors']['parser'], $parserErrors);
$this->assertEquals($data['errors']['lexer'], $lexerErrors);
}
}
|