|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +class WP_SQLite_Driver_Compatibility_Tests extends TestCase { |
| 6 | + /** @var WP_SQLite_Driver */ |
| 7 | + private $driver; |
| 8 | + |
| 9 | + /** @var PDO */ |
| 10 | + private $sqlite; |
| 11 | + |
| 12 | + public function setUp(): void { |
| 13 | + $pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class; |
| 14 | + $this->sqlite = new $pdo_class( 'sqlite::memory:' ); |
| 15 | + $this->driver = new WP_SQLite_Driver( |
| 16 | + new WP_SQLite_Connection( array( 'pdo' => $this->sqlite ) ), |
| 17 | + 'wp' |
| 18 | + ); |
| 19 | + $this->driver->query( 'CREATE TABLE t (id INT AUTO_INCREMENT PRIMARY KEY, value VARCHAR(255))' ); |
| 20 | + } |
| 21 | + |
| 22 | + public function test_wraps_pdo_driver(): void { |
| 23 | + $get_driver = Closure::bind( |
| 24 | + function () { |
| 25 | + return $this->mysql_on_sqlite_driver; |
| 26 | + }, |
| 27 | + $this->driver, |
| 28 | + WP_SQLite_Driver::class |
| 29 | + ); |
| 30 | + |
| 31 | + $this->assertInstanceOf( WP_PDO_MySQL_On_SQLite::class, $get_driver() ); |
| 32 | + $this->assertSame( $this->sqlite, $this->driver->get_connection()->get_pdo() ); |
| 33 | + $this->assertSame( $this->driver->get_sqlite_version(), $this->driver->client_info ); |
| 34 | + $this->assertSame( SQLITE_DRIVER_VERSION, $this->driver->get_saved_driver_version() ); |
| 35 | + $this->assertTrue( $this->driver->is_sql_mode_active( 'STRICT_TRANS_TABLES' ) ); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_preserves_legacy_query_results(): void { |
| 39 | + $this->assertSame( 1, $this->driver->query( "INSERT INTO t (value) VALUES ('first')" ) ); |
| 40 | + $this->assertSame( 1, $this->driver->get_insert_id() ); |
| 41 | + |
| 42 | + $result = $this->driver->query( 'SELECT id, value FROM t', PDO::FETCH_ASSOC ); |
| 43 | + $this->assertSame( |
| 44 | + array( |
| 45 | + array( |
| 46 | + 'id' => '1', |
| 47 | + 'value' => 'first', |
| 48 | + ), |
| 49 | + ), |
| 50 | + $result |
| 51 | + ); |
| 52 | + $this->assertSame( $result, $this->driver->get_query_results() ); |
| 53 | + $this->assertSame( $result, $this->driver->get_last_return_value() ); |
| 54 | + $this->assertSame( 2, $this->driver->get_last_column_count() ); |
| 55 | + $this->assertCount( 2, $this->driver->get_last_column_meta() ); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_delegates_diagnostics_and_native_queries(): void { |
| 59 | + $this->driver->query( 'SELECT 1' ); |
| 60 | + |
| 61 | + $this->assertSame( 'SELECT 1', $this->driver->get_last_mysql_query() ); |
| 62 | + $this->assertNotEmpty( $this->driver->get_last_sqlite_queries() ); |
| 63 | + $this->assertInstanceOf( WP_MySQL_Parser::class, $this->driver->create_parser( 'SELECT 1' ) ); |
| 64 | + $this->assertSame( '42', $this->driver->execute_sqlite_query( 'SELECT 42' )->fetchColumn() ); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_preserves_transaction_method_aliases(): void { |
| 68 | + $this->driver->begin_transaction(); |
| 69 | + $this->driver->query( "INSERT INTO t (value) VALUES ('rolled back')" ); |
| 70 | + $this->driver->rollback(); |
| 71 | + $this->assertSame( '0', $this->driver->query( 'SELECT COUNT(*) FROM t' )[0]->{'COUNT(*)'} ); |
| 72 | + |
| 73 | + $this->driver->beginTransaction(); |
| 74 | + $this->driver->query( "INSERT INTO t (value) VALUES ('committed')" ); |
| 75 | + $this->driver->commit(); |
| 76 | + $this->assertSame( '1', $this->driver->query( 'SELECT COUNT(*) FROM t' )[0]->{'COUNT(*)'} ); |
| 77 | + } |
| 78 | + |
| 79 | + public function test_proxies_legacy_test_helpers(): void { |
| 80 | + $this->driver->main_db_name = 'wp_test_new'; |
| 81 | + $result = $this->driver->query( |
| 82 | + 'SELECT schema_name FROM information_schema.schemata ORDER BY schema_name' |
| 83 | + ); |
| 84 | + $this->assertSame( 'wp_test_new', $result[1]->SCHEMA_NAME ); |
| 85 | + |
| 86 | + $quote = Closure::bind( |
| 87 | + function ( string $value ) { |
| 88 | + return $this->quote_mysql_utf8_string_literal( $value ); |
| 89 | + }, |
| 90 | + $this->driver, |
| 91 | + WP_SQLite_Driver::class |
| 92 | + ); |
| 93 | + $this->assertSame( "'abc''xyz'", $quote( "abc'xyz" ) ); |
| 94 | + } |
| 95 | +} |
0 commit comments