|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/WP_SQLite_Translator_Tests.php'; |
| 4 | +require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-driver.php'; |
| 5 | +require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-expression.php'; |
| 6 | +require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-token-factory.php'; |
| 7 | +require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-token.php'; |
| 8 | +require_once __DIR__ . '/../wp-includes/sqlite-ast/class-wp-sqlite-query-builder.php'; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use WIP\WP_SQLite_Query_Builder; |
| 12 | + |
| 13 | +class WP_SQLite_Driver_Translator_Tests extends TestCase { |
| 14 | + const GRAMMAR_PATH = __DIR__ . '/../wp-includes/mysql/mysql-grammar.php'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var WP_Parser_Grammar |
| 18 | + */ |
| 19 | + private static $grammar; |
| 20 | + |
| 21 | + public static function setUpBeforeClass(): void { |
| 22 | + self::$grammar = new WP_Parser_Grammar( include self::GRAMMAR_PATH ); |
| 23 | + } |
| 24 | + |
| 25 | + public function testSelect(): void { |
| 26 | + $this->assertTranslation( 'SELECT 1', 'SELECT 1' ); |
| 27 | + $this->assertTranslation( 'SELECT * FROM "t"', 'SELECT * FROM t' ); |
| 28 | + $this->assertTranslation( 'SELECT "c" FROM "t"', 'SELECT c FROM t' ); |
| 29 | + $this->assertTranslation( 'SELECT "t"."c" FROM "t"', 'SELECT t.c FROM t' ); |
| 30 | + $this->assertTranslation( |
| 31 | + 'SELECT * FROM "t1" LEFT JOIN "t2" ON "t1"."id" = "t2"."t1_id" WHERE "t1"."name" = \'abc\'', |
| 32 | + "SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.t1_id WHERE t1.name = 'abc'" |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function testInsert(): void { |
| 37 | + $this->assertTranslation( 'INSERT INTO "t" ("c") VALUES (1)', 'INSERT INTO t (c) VALUES (1)' ); |
| 38 | + } |
| 39 | + |
| 40 | + private function assertTranslation( string $expected, string $query ) { |
| 41 | + $lexer = new WP_MySQL_Lexer( $query ); |
| 42 | + $tokens = $lexer->remaining_tokens(); |
| 43 | + |
| 44 | + $parser = new WP_MySQL_Parser( self::$grammar, $tokens ); |
| 45 | + $ast = $parser->parse(); |
| 46 | + |
| 47 | + $translator = new WIP\WP_SQLite_Translator(); |
| 48 | + $this->assertSame( $expected, WP_SQLite_Query_Builder::stringify( $translator->translate( $ast ) ) ); |
| 49 | + } |
| 50 | +} |
0 commit comments