Skip to content
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
<directory suffix=".php">tests/</directory>
<!-- Exclude test tools. -->
<exclude>tests/tools</exclude>
<!-- Exclude new parser tests for now. -->
<exclude>tests/parser</exclude>
</testsuite>
</testsuites>
</phpunit>
7 changes: 2 additions & 5 deletions tests/WP_SQLite_Driver_Metadata_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ public function testCountTables() {
$this->assertQuery( 'CREATE TABLE t2 (id INT)' );

$result = $this->assertQuery( "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'wp'" );
$this->assertEquals( array( (object) array( 'COUNT ( * )' => '2' ) ), $result );
$this->assertEquals( array( (object) array( 'COUNT(*)' => '2' ) ), $result );

$result = $this->assertQuery( "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'other'" );
$this->assertEquals( array( (object) array( 'COUNT ( * )' => '0' ) ), $result );

// @TODO: The result key should be "COUNT(*)" instead of "COUNT ( * )".
// The spacing was probably inserted by the translator.
$this->assertEquals( array( (object) array( 'COUNT(*)' => '0' ) ), $result );
}

public function testInformationSchemaTables() {
Expand Down
218 changes: 218 additions & 0 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6020,4 +6020,222 @@ public function testDatabaseNameMismatchWithExistingInformationSchemaTableData()
$this->expectExceptionMessage( "Incorrect database name. The database was created with name 'db-one', but 'db-two' is used in the current session." );
new WP_SQLite_Driver( $connection, 'db-two' );
}

public function testSelectColumnNames(): void {
$this->assertQuery( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$this->assertQuery( 'INSERT INTO t (id, name) VALUES (1, "John"), (2, "Jane")' );

// Columns (no explicit alias).
$result = $this->assertQuery( 'SELECT id, name FROM t' );
$this->assertSame( array( 'id', 'name' ), array_keys( (array) $result[0] ) );

// Columns with an explicit alias.
$result = $this->assertQuery( 'SELECT id AS alias_id, name AS alias_name FROM t' );
$this->assertSame( array( 'alias_id', 'alias_name' ), array_keys( (array) $result[0] ) );

// Expressions (no explicit alias).
$result = $this->assertQuery( 'SELECT id + 1, (2 + 3) FROM t' );
$this->assertSame( array( 'id + 1', '(2 + 3)' ), array_keys( (array) $result[0] ) );

// Expressions with an explicit alias.
$result = $this->assertQuery( 'SELECT id + 1 AS alias_id, (2 + 3) AS alias_numbers FROM t' );
$this->assertSame( array( 'alias_id', 'alias_numbers' ), array_keys( (array) $result[0] ) );

// Function calls (no explicit alias).
$result = $this->assertQuery( "SELECT CONCAT('a', 'b')" );
$this->assertSame( array( "CONCAT('a', 'b')" ), array_keys( (array) $result[0] ) );

// Function calls with an explicit alias.
$result = $this->assertQuery( "SELECT CONCAT('a', 'b') AS alias_concat" );
$this->assertSame( array( 'alias_concat' ), array_keys( (array) $result[0] ) );
}

public function testSetStatement(): void {
$this->assertQuery( 'SET NAMES utf8mb4' );
$this->assertQuery( 'SET CHARSET utf8mb4' );
$this->assertQuery( 'SET CHARACTER SET utf8mb4' );
}

public function testSessionSystemVariables(): void {
$this->assertQuery( "SET character_set_client = 'latin1'" );
$result = $this->assertQuery( 'SELECT @@character_set_client' );
$this->assertSame( 'latin1', $result[0]->{'@@character_set_client'} );

$this->assertQuery( "SET @@character_set_client = 'utf8mb3'" );
$result = $this->assertQuery( 'SELECT @@character_set_client' );
$this->assertSame( 'utf8mb3', $result[0]->{'@@character_set_client'} );

$this->assertQuery( "SET @@session.character_set_client = 'utf8mb4'" );
$result = $this->assertQuery( 'SELECT @@session.character_set_client' );
$this->assertSame( 'utf8mb4', $result[0]->{'@@session.character_set_client'} );
}

public function testSystemVariablesWithKeywords(): void {
$this->assertQuery( 'SET default_storage_engine = InnoDB' );
$result = $this->assertQuery( 'SELECT @@default_storage_engine' );
$this->assertSame( 'InnoDB', $result[0]->{'@@default_storage_engine'} );

$this->assertQuery( 'SET default_collation_for_utf8mb4 = utf8mb4_0900_ai_ci' );
$result = $this->assertQuery( 'SELECT @@default_collation_for_utf8mb4' );
$this->assertSame( 'utf8mb4_0900_ai_ci', $result[0]->{'@@default_collation_for_utf8mb4'} );

$this->assertQuery( 'SET resultset_metadata = FULL' );
$result = $this->assertQuery( 'SELECT @@resultset_metadata' );
$this->assertSame( 'FULL', $result[0]->{'@@resultset_metadata'} );

$this->assertQuery( 'SET session_track_gtids = OWN_GTID' );
$result = $this->assertQuery( 'SELECT @@session_track_gtids' );
$this->assertSame( 'OWN_GTID', $result[0]->{'@@session_track_gtids'} );

$this->assertQuery( 'SET session_track_transaction_info = STATE' );
$result = $this->assertQuery( 'SELECT @@session_track_transaction_info' );
$this->assertSame( 'STATE', $result[0]->{'@@session_track_transaction_info'} );

$this->assertQuery( 'SET transaction_isolation = SERIALIZABLE' );
$result = $this->assertQuery( 'SELECT @@transaction_isolation' );
$this->assertSame( 'SERIALIZABLE', $result[0]->{'@@transaction_isolation'} );

$this->assertQuery( 'SET use_secondary_engine = FORCED' );
$result = $this->assertQuery( 'SELECT @@use_secondary_engine' );
$this->assertSame( 'FORCED', $result[0]->{'@@use_secondary_engine'} );
}

public function testSystemVariablesWithBooleanValues(): void {
$this->assertQuery( 'SET autocommit = ON, big_tables = OFF' );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( 'SET autocommit = on, big_tables = off' );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( "SET autocommit = 'ON', big_tables = 'OFF'" );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( "SET autocommit = 'on', big_tables = 'off'" );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( 'SET autocommit = TRUE, big_tables = FALSE' );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( 'SET autocommit = true, big_tables = false' );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( 'SET autocommit = 1, big_tables = 0' );
$result = $this->assertQuery( 'SELECT @@autocommit, @@big_tables' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );
}

public function testSystemVariablesWithOnOffValues(): void {
$this->assertQuery( 'SET autocommit = ON' );
$result = $this->assertQuery( 'SELECT @@autocommit' );
$this->assertSame( '1', $result[0]->{'@@autocommit'} );

$this->assertQuery( 'SET big_tables = OFF' );
$result = $this->assertQuery( 'SELECT @@big_tables' );
$this->assertSame( '0', $result[0]->{'@@big_tables'} );

$this->assertQuery( 'SET end_markers_in_json = ON' );
$result = $this->assertQuery( 'SELECT @@end_markers_in_json' );
$this->assertSame( '1', $result[0]->{'@@end_markers_in_json'} );

$this->assertQuery( 'SET explicit_defaults_for_timestamp = OFF' );
$result = $this->assertQuery( 'SELECT @@explicit_defaults_for_timestamp' );
$this->assertSame( '0', $result[0]->{'@@explicit_defaults_for_timestamp'} );

$this->assertQuery( 'SET keep_files_on_create = ON' );
$result = $this->assertQuery( 'SELECT @@keep_files_on_create' );
$this->assertSame( '1', $result[0]->{'@@keep_files_on_create'} );

$this->assertQuery( 'SET old_alter_table = OFF' );
$result = $this->assertQuery( 'SELECT @@old_alter_table' );
$this->assertSame( '0', $result[0]->{'@@old_alter_table'} );

$this->assertQuery( 'SET print_identified_with_as_hex = ON' );
$result = $this->assertQuery( 'SELECT @@print_identified_with_as_hex' );
$this->assertSame( '1', $result[0]->{'@@print_identified_with_as_hex'} );

$this->assertQuery( 'SET require_row_format = OFF' );
$result = $this->assertQuery( 'SELECT @@require_row_format' );
$this->assertSame( '0', $result[0]->{'@@require_row_format'} );

$this->assertQuery( 'SET select_into_disk_sync = ON' );
$result = $this->assertQuery( 'SELECT @@select_into_disk_sync' );
$this->assertSame( '1', $result[0]->{'@@select_into_disk_sync'} );

$this->assertQuery( 'SET session_track_gtids = OFF' );
$result = $this->assertQuery( 'SELECT @@session_track_gtids' );
// @TODO: For session_track_gtids, the value should be OFF, not 0.
//$this->assertSame( 'OFF', $result[0]->{'@@session_track_gtids'} );

$this->assertQuery( 'SET session_track_schema = ON' );
$result = $this->assertQuery( 'SELECT @@session_track_schema' );
$this->assertSame( '1', $result[0]->{'@@session_track_schema'} );

$this->assertQuery( 'SET session_track_state_change = OFF' );
$result = $this->assertQuery( 'SELECT @@session_track_state_change' );
$this->assertSame( '0', $result[0]->{'@@session_track_state_change'} );

$this->assertQuery( 'SET session_track_transaction_info = OFF' );
$result = $this->assertQuery( 'SELECT @@session_track_transaction_info' );
// @TODO: For session_track_transaction_info, the value should be OFF, not 0.
//$this->assertSame( 'OFF', $result[0]->{'@@session_track_transaction_info'} );

$this->assertQuery( 'SET show_create_table_skip_secondary_engine = ON' );
$result = $this->assertQuery( 'SELECT @@show_create_table_skip_secondary_engine' );
$this->assertSame( '1', $result[0]->{'@@show_create_table_skip_secondary_engine'} );

$this->assertQuery( 'SET show_create_table_verbosity = OFF' );
$result = $this->assertQuery( 'SELECT @@show_create_table_verbosity' );
$this->assertSame( '0', $result[0]->{'@@show_create_table_verbosity'} );

$this->assertQuery( 'SET sql_auto_is_null = ON' );
$result = $this->assertQuery( 'SELECT @@sql_auto_is_null' );
$this->assertSame( '1', $result[0]->{'@@sql_auto_is_null'} );

$this->assertQuery( 'SET sql_big_selects = OFF' );
$result = $this->assertQuery( 'SELECT @@sql_big_selects' );
$this->assertSame( '0', $result[0]->{'@@sql_big_selects'} );

$this->assertQuery( 'SET sql_buffer_result = ON' );
$result = $this->assertQuery( 'SELECT @@sql_buffer_result' );
$this->assertSame( '1', $result[0]->{'@@sql_buffer_result'} );

$this->assertQuery( 'SET sql_safe_updates = OFF' );
$result = $this->assertQuery( 'SELECT @@sql_safe_updates' );
$this->assertSame( '0', $result[0]->{'@@sql_safe_updates'} );

$this->assertQuery( 'SET sql_warnings = ON' );
$result = $this->assertQuery( 'SELECT @@sql_warnings' );
$this->assertSame( '1', $result[0]->{'@@sql_warnings'} );

$this->assertQuery( 'SET transaction_read_only = OFF' );
$result = $this->assertQuery( 'SELECT @@transaction_read_only' );
$this->assertSame( '0', $result[0]->{'@@transaction_read_only'} );
}

public function testUserVariables(): void {
$this->assertQuery( 'SET @my_var = 1' );
$result = $this->assertQuery( 'SELECT @my_var' );
$this->assertEquals( 1, $result[0]->{'@my_var'} );

$this->assertQuery( 'SET @my_var = @my_var + 1' );
$result = $this->assertQuery( 'SELECT @my_var' );
$this->assertEquals( 2, $result[0]->{'@my_var'} );

$this->assertQuery( 'SET @my_var = @my_var + 1' );
$result = $this->assertQuery( 'SELECT @my_var' );
$this->assertEquals( 3, $result[0]->{'@my_var'} );
}
}
2 changes: 1 addition & 1 deletion tests/WP_SQLite_Driver_Translation_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ public function testSystemVariables(): void {

public function testConcatFunction(): void {
$this->assertQuery(
"SELECT ('a' || 'b' || 'c')",
"SELECT ('a' || 'b' || 'c') AS `CONCAT(\"a\", \"b\", \"c\")`",
'SELECT CONCAT("a", "b", "c")'
);
}
Expand Down
30 changes: 15 additions & 15 deletions tests/parser/WP_Parser_Node_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ public function testEmptyChildren(): void {
}

public function testNodeTree(): void {
$input = 'SELECT 1 + 2, 2';

// Prepare nodes and tokens.
$root = new WP_Parser_Node( 1, 'root' );
$n_keyword = new WP_Parser_Node( 2, 'keyword' );
$n_expr_a = new WP_Parser_Node( 3, 'expr' );
$n_expr_b = new WP_Parser_Node( 3, 'expr' );
$n_expr_c = new WP_Parser_Node( 3, 'expr' );
$t_select = new WP_Parser_Token( 100, 'SELECT' );
$t_comma = new WP_Parser_Token( 200, ',' );
$t_plus = new WP_Parser_Token( 300, '+' );
$t_one = new WP_Parser_Token( 400, '1' );
$t_two_a = new WP_Parser_Token( 400, '2' );
$t_two_b = new WP_Parser_Token( 400, '2' );
$t_eof = new WP_Parser_Token( 500, '' );
$t_select = new WP_Parser_Token( 100, 0, 6, $input );
$t_comma = new WP_Parser_Token( 200, 12, 1, $input );
$t_plus = new WP_Parser_Token( 300, 9, 1, $input );
$t_one = new WP_Parser_Token( 400, 7, 1, $input );
$t_two_a = new WP_Parser_Token( 400, 11, 1, $input );
$t_two_b = new WP_Parser_Token( 400, 14, 1, $input );
$t_eof = new WP_Parser_Token( 500, 15, 0, $input );

// Prepare a tree.
//
Expand Down Expand Up @@ -102,38 +104,36 @@ public function testNodeTree(): void {
$this->assertSame( array(), $root->get_child_tokens( 100 ) );

// Test single descendant methods.
// @TODO: Consider breadth-first search vs depth-first search.
$this->assertSame( $n_keyword, $root->get_first_descendant_node() );
$this->assertSame( $n_expr_a, $root->get_first_descendant_node( 'expr' ) );
$this->assertSame( null, $root->get_first_descendant_node( 'root' ) );
$this->assertSame( $t_comma, $root->get_first_descendant_token() );
$this->assertSame( $t_select, $root->get_first_descendant_token() );
$this->assertSame( $t_one, $root->get_first_descendant_token( 400 ) );
$this->assertSame( null, $root->get_first_descendant_token( 123 ) );

// Test multiple descendant methods.
// @TODO: Consider breadth-first search vs depth-first search.
$this->assertSame(
array( $n_keyword, $n_expr_a, $t_comma, $n_expr_b, $t_eof, $t_select, $t_one, $t_plus, $n_expr_c, $t_two_a, $t_two_b ),
array( $n_keyword, $t_select, $n_expr_a, $t_one, $t_plus, $n_expr_c, $t_two_b, $t_comma, $n_expr_b, $t_two_a, $t_eof ),
$root->get_descendants()
);
$this->assertSame(
array( $n_keyword, $n_expr_a, $n_expr_b, $n_expr_c ),
array( $n_keyword, $n_expr_a, $n_expr_c, $n_expr_b ),
$root->get_descendant_nodes()
);
$this->assertSame(
array( $n_expr_a, $n_expr_b, $n_expr_c ),
array( $n_expr_a, $n_expr_c, $n_expr_b ),
$root->get_descendant_nodes( 'expr' )
);
$this->assertSame(
array(),
$root->get_descendant_nodes( 'root' )
);
$this->assertSame(
array( $t_comma, $t_eof, $t_select, $t_one, $t_plus, $t_two_a, $t_two_b ),
array( $t_select, $t_one, $t_plus, $t_two_b, $t_comma, $t_two_a, $t_eof ),
$root->get_descendant_tokens()
);
$this->assertSame(
array( $t_one, $t_two_a, $t_two_b ),
array( $t_one, $t_two_b, $t_two_a ),
$root->get_descendant_tokens( 400 )
);
$this->assertSame(
Expand Down
Loading