|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +class WP_SQLite_Information_Schema_Reconstructor_Tests extends TestCase { |
| 6 | + /** @var WP_SQLite_Driver */ |
| 7 | + private $engine; |
| 8 | + |
| 9 | + /** @var WP_SQLite_Information_Schema_Reconstructor */ |
| 10 | + private $reconstructor; |
| 11 | + |
| 12 | + /** @var PDO */ |
| 13 | + private $sqlite; |
| 14 | + |
| 15 | + public static function setUpBeforeClass(): void { |
| 16 | + // if ( ! defined( 'PDO_DEBUG' )) { |
| 17 | + // define( 'PDO_DEBUG', true ); |
| 18 | + // } |
| 19 | + if ( ! defined( 'FQDB' ) ) { |
| 20 | + define( 'FQDB', ':memory:' ); |
| 21 | + define( 'FQDBDIR', __DIR__ . '/../testdb' ); |
| 22 | + } |
| 23 | + error_reporting( E_ALL & ~E_DEPRECATED ); |
| 24 | + if ( ! isset( $GLOBALS['table_prefix'] ) ) { |
| 25 | + $GLOBALS['table_prefix'] = 'wptests_'; |
| 26 | + } |
| 27 | + if ( ! isset( $GLOBALS['wpdb'] ) ) { |
| 28 | + $GLOBALS['wpdb'] = new stdClass(); |
| 29 | + $GLOBALS['wpdb']->suppress_errors = false; |
| 30 | + $GLOBALS['wpdb']->show_errors = true; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Before each test, we create a new database |
| 35 | + public function setUp(): void { |
| 36 | + $this->sqlite = new PDO( 'sqlite::memory:' ); |
| 37 | + $this->engine = new WP_SQLite_Driver( |
| 38 | + array( |
| 39 | + 'connection' => $this->sqlite, |
| 40 | + 'database' => 'wp', |
| 41 | + ) |
| 42 | + ); |
| 43 | + |
| 44 | + $builder = new WP_SQLite_Information_Schema_Builder( |
| 45 | + 'wp', |
| 46 | + WP_SQLite_Driver::RESERVED_PREFIX, |
| 47 | + array( $this->engine, 'execute_sqlite_query' ) |
| 48 | + ); |
| 49 | + |
| 50 | + $this->reconstructor = new WP_SQLite_Information_Schema_Reconstructor( |
| 51 | + $this->engine, |
| 52 | + $builder |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function testReconstructInformationSchemaTable(): void { |
| 57 | + $this->engine->get_pdo()->exec( |
| 58 | + ' |
| 59 | + CREATE TABLE t ( |
| 60 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 61 | + email TEXT NOT NULL UNIQUE, |
| 62 | + name TEXT NOT NULL, |
| 63 | + role TEXT, |
| 64 | + score REAL, |
| 65 | + priority INTEGER DEFAULT 0, |
| 66 | + data BLOB, |
| 67 | + UNIQUE (name) |
| 68 | + ) |
| 69 | + ' |
| 70 | + ); |
| 71 | + $this->engine->get_pdo()->exec( 'CREATE INDEX idx_score ON t (score)' ); |
| 72 | + $this->engine->get_pdo()->exec( 'CREATE INDEX idx_role_score ON t (role, priority)' ); |
| 73 | + $result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "t"' ); |
| 74 | + $this->assertEquals( 0, count( $result ) ); |
| 75 | + |
| 76 | + $this->reconstructor->ensure_correct_information_schema(); |
| 77 | + $result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "t"' ); |
| 78 | + $this->assertEquals( 1, count( $result ) ); |
| 79 | + |
| 80 | + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); |
| 81 | + $this->assertSame( |
| 82 | + implode( |
| 83 | + "\n", |
| 84 | + array( |
| 85 | + 'CREATE TABLE `t` (', |
| 86 | + ' `id` int NOT NULL AUTO_INCREMENT,', |
| 87 | + ' `email` text NOT NULL,', |
| 88 | + ' `name` text NOT NULL,', |
| 89 | + ' `role` text DEFAULT NULL,', |
| 90 | + ' `score` float DEFAULT NULL,', |
| 91 | + " `priority` int DEFAULT '0',", |
| 92 | + ' `data` blob DEFAULT NULL,', |
| 93 | + ' PRIMARY KEY (`id`),', |
| 94 | + ' KEY `idx_role_score` (`role`(100), `priority`),', |
| 95 | + ' KEY `idx_score` (`score`),', |
| 96 | + ' UNIQUE KEY `sqlite_autoindex_t_2` (`name`(100)),', |
| 97 | + ' UNIQUE KEY `sqlite_autoindex_t_1` (`email`(100))', |
| 98 | + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci', |
| 99 | + ) |
| 100 | + ), |
| 101 | + $result[0]->{'Create Table'} |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private function assertQuery( $sql ) { |
| 106 | + $retval = $this->engine->query( $sql ); |
| 107 | + $this->assertNotFalse( $retval ); |
| 108 | + return $retval; |
| 109 | + } |
| 110 | +} |
0 commit comments