blob: 502763c2dd50a09061892695799ff37df945cbe1 (
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
|
<?php
/**
* Bootstrap for tests.
*
* @package SqlParser
* @subpackage Tests
*/
namespace SqlParser\Tests;
use SqlParser\Lexer;
use SqlParser\Parser;
require_once 'vendor/autoload.php';
/**
* Implements useful methods for testing.
*
* @category Tests
* @package SqlParser
* @subpackage Tests
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* 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)
{
$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.
*
* @return void
*/
public function runParserTest($name)
{
/**
* Test's data.
*
* @var array $data
*/
$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);
}
}
|