33namespace Automattic \WP_CLI \SQLite ;
44
55use Behat \Behat \Context \Context ;
6- use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
76use Behat \Gherkin \Node \PyStringNode ;
87use WP_CLI \Tests \Context \FeatureContext as WPCLIFeatureContext ;
9- use SQLite3 ;
8+ use PDO ;
109use Exception ;
1110
1211class SQLiteFeatureContext extends WPCLIFeatureContext implements Context {
@@ -25,8 +24,9 @@ public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content )
2524 */
2625 public function theSqliteDatabaseShouldContainATableNamed ( $ table_name ) {
2726 $ this ->connectToDatabase ();
28- $ result = $ this ->db ->query ( "SELECT name FROM sqlite_master WHERE type='table' AND name=' " . $ this ->db ->escapeString ( $ table_name ) . "' " );
29- $ row = $ result ->fetchArray ();
27+ $ stmt = $ this ->db ->prepare ( "SELECT name FROM sqlite_master WHERE type='table' AND name=? " );
28+ $ stmt ->execute ( [ $ table_name ] );
29+ $ row = $ stmt ->fetch ();
3030 if ( ! $ row ) {
3131 throw new Exception ( "Table ' $ table_name' not found in the database. " );
3232 }
@@ -38,8 +38,9 @@ public function theSqliteDatabaseShouldContainATableNamed( $table_name ) {
3838 */
3939 public function theTableShouldContainARowWithName ( $ table_name , $ name ) {
4040 $ this ->connectToDatabase ();
41- $ result = $ this ->db ->query ( "SELECT * FROM $ table_name WHERE name=' " . $ this ->db ->escapeString ( $ name ) . "' " );
42- $ row = $ result ->fetchArray ();
41+ $ stmt = $ this ->db ->prepare ( "SELECT * FROM $ table_name WHERE name=? " );
42+ $ stmt ->execute ( [ $ name ] );
43+ $ row = $ stmt ->fetch ();
4344 if ( ! $ row ) {
4445 throw new Exception ( "Row with name ' $ name' not found in table ' $ table_name'. " );
4546 }
@@ -51,14 +52,7 @@ public function theTableShouldContainARowWithName( $table_name, $name ) {
5152 public function theSqliteDatabaseShouldContainTheImportedData () {
5253 $ this ->connectToDatabase ();
5354 $ result = $ this ->db ->query ( "SELECT name FROM sqlite_master WHERE type='table' " );
54- $ tables = [];
55- while ( true ) {
56- $ row = $ result ->fetchArray ();
57- if ( false === $ row ) {
58- break ;
59- }
60- $ tables [] = $ row ['name ' ];
61- }
55+ $ tables = $ result ->fetchAll ( PDO ::FETCH_COLUMN );
6256 if ( empty ( $ tables ) ) {
6357 throw new Exception ( 'No tables found in the database after import. ' );
6458 }
@@ -78,7 +72,8 @@ private function connectToDatabase() {
7872 if ( ! $ this ->db ) {
7973 $ run_dir = $ this ->variables ['RUN_DIR ' ];
8074 $ db_file = $ run_dir . '/wp-content/database/.ht.sqlite ' ;
81- $ this ->db = new SQLite3 ( $ db_file );
75+ $ this ->db = new PDO ( 'sqlite: ' . $ db_file );
76+ $ this ->db ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
8277 }
8378 }
8479
@@ -105,45 +100,43 @@ protected function create_file( $filename, $content ) {
105100 file_put_contents ( $ full_path , $ content );
106101 }
107102
108-
109- ////
110103 /**
111104 * @Given /^the SQLite database contains some sample data$/
112105 */
113106 public function theSqliteDatabaseContainsSomeSampleData () {
114- $ this ->connectToDatabase ();
115- $ this ->db ->exec (
116- "
117- INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
118- VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
119- "
120- );
121-
122- // Insert or update sample data in wp_users
123- $ this ->db ->exec (
124- "
125- INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
126- VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
127- "
128- );
129-
130- // Insert or update sample data in wp_options
131- $ this ->db ->exec (
132- "
133- INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
134- VALUES
135- (1, 'siteurl', 'http://example.com', 'yes'),
136- (2, 'blogname', 'Test Blog', 'yes'),
137- (3, 'blogdescription', 'Just another WordPress site', 'yes'),
138- (4, 'users_can_register', '0', 'yes'),
139- (5, 'admin_email', 'admin@example.com', 'yes'),
140- (6, 'start_of_week', '1', 'yes'),
141- (7, 'use_balanceTags', '0', 'yes'),
142- (8, 'use_smilies', '1', 'yes'),
143- (9, 'require_name_email', '1', 'yes'),
144- (10, 'comments_notify', '1', 'yes');
145- "
146- );
107+ $ this ->connectToDatabase ();
108+ $ this ->db ->exec (
109+ "
110+ INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
111+ VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
112+ "
113+ );
114+
115+ // Insert or update sample data in wp_users
116+ $ this ->db ->exec (
117+ "
118+ INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
119+ VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
120+ "
121+ );
122+
123+ // Insert or update sample data in wp_options
124+ $ this ->db ->exec (
125+ "
126+ INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
127+ VALUES
128+ (1, 'siteurl', 'http://example.com', 'yes'),
129+ (2, 'blogname', 'Test Blog', 'yes'),
130+ (3, 'blogdescription', 'Just another WordPress site', 'yes'),
131+ (4, 'users_can_register', '0', 'yes'),
132+ (5, 'admin_email', 'admin@example.com', 'yes'),
133+ (6, 'start_of_week', '1', 'yes'),
134+ (7, 'use_balanceTags', '0', 'yes'),
135+ (8, 'use_smilies', '1', 'yes'),
136+ (9, 'require_name_email', '1', 'yes'),
137+ (10, 'comments_notify', '1', 'yes');
138+ "
139+ );
147140 }
148141
149142 /**
0 commit comments