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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\SqlParser\Tests;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Exceptions\LexerException;
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\Tools\CustomJsonSerializer;
use PHPUnit\Framework\TestCase as BaseTestCase;
use function file_get_contents;
use function str_contains;
use function strpos;
use function substr;
/**
* Implements useful methods for testing.
*/
abstract class TestCase extends BaseTestCase
{
public function setUp(): void
{
global $lang;
// This line makes sure the test suite uses English so we can assert
// on the error messages, if it is not here you will need to use
// LC_ALL=C ./vendor/bin/phpunit
// Users can have French language as default on their OS
// That would make the assertions fail
$lang = 'en';
Context::load();
}
/**
* Gets the token list generated by lexing this query.
*
* @param string $query the query to be lexed
*/
public function getTokensList(string $query): TokensList
{
$lexer = new Lexer($query);
return $lexer->list;
}
/**
* Gets the errors as an array.
*
* @param Lexer|Parser $obj object containing the errors
*
* @return array<int, array<int, Token|string|int>>
* @psalm-return (
* $obj is Lexer
* ? list<array{string, string, int, int}>
* : list<array{string, Token|null, int}>
* )
*/
public function getErrorsAsArray($obj): array
{
$ret = [];
if ($obj instanceof Lexer) {
/** @var LexerException $err */
foreach ($obj->errors as $err) {
$ret[] = [$err->getMessage(), $err->ch, $err->pos, (int) $err->getCode()];
}
} elseif ($obj instanceof Parser) {
/** @var ParserException $err */
foreach ($obj->errors as $err) {
$ret[] = [$err->getMessage(), $err->token, (int) $err->getCode()];
}
}
return $ret;
}
/**
* Gets test's input and expected output.
*
* @param string $name the name of the test
*
* @return array<string, string|Lexer|Parser|array<string, array<int, int|string|Token>[]>|null>
* @psalm-return array{
* query: string,
* lexer: Lexer,
* parser: Parser|null,
* errors: array{lexer: list<array{string, string, int, int}>, parser: list<array{string, Token, int}>}
* }
*/
public function getData(string $name): array
{
$serializedData = file_get_contents('tests/data/' . $name . '.out');
$this->assertIsString($serializedData);
$serializer = new CustomJsonSerializer();
$data = $serializer->unserialize($serializedData);
$this->assertIsArray($data);
$this->assertArrayHasKey('query', $data);
$this->assertArrayHasKey('lexer', $data);
$this->assertArrayHasKey('parser', $data);
$this->assertArrayHasKey('errors', $data);
$this->assertIsString($data['query']);
$this->assertInstanceOf(Lexer::class, $data['lexer']);
if ($data['parser'] !== null) {
$this->assertInstanceOf(Parser::class, $data['parser']);
}
$this->assertIsArray($data['errors']);
$this->assertArrayHasKey('lexer', $data['errors']);
$this->assertArrayHasKey('parser', $data['errors']);
$this->assertIsArray($data['errors']['lexer']);
$this->assertIsArray($data['errors']['parser']);
$data['query'] = file_get_contents('tests/data/' . $name . '.in');
$this->assertIsString($data['query']);
return $data;
}
/**
* Runs a test.
*
* @param string $name the name of the test
*/
public function runParserTest(string $name): void
{
/**
* Test's data.
*/
$data = $this->getData($name);
if (str_contains($name, '/ansi/')) {
// set mode if appropriate
Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
}
$mariaDbPos = strpos($name, '_mariadb_');
if ($mariaDbPos !== false) {// Keep in sync with TestGenerator.php
// set context
$mariaDbVersion = (int) substr($name, $mariaDbPos + 9, 6);
Context::load('MariaDb' . $mariaDbVersion);
}
// Lexer.
$lexer = new Lexer($data['query']);
$lexerErrors = $this->getErrorsAsArray($lexer);
$lexer->errors = [];
// Parser.
$parser = empty($data['parser']) ? null : new Parser($lexer->list);
$parserErrors = [];
if ($parser !== null) {
$parserErrors = $this->getErrorsAsArray($parser);
$parser->errors = [];
}
// 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);
// reset mode after test run
Context::setMode();
}
}
|