55use Behat \Behat \Context \Context ;
66use Behat \Gherkin \Node \PyStringNode ;
77use WP_CLI \Tests \Context \FeatureContext as WPCLIFeatureContext ;
8- use SQLite3 ;
8+ use PDO ;
99use Exception ;
1010
1111class SQLiteFeatureContext extends WPCLIFeatureContext implements Context {
@@ -24,8 +24,9 @@ public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content )
2424 */
2525 public function theSqliteDatabaseShouldContainATableNamed ( $ table_name ) {
2626 $ this ->connectToDatabase ();
27- $ result = $ this ->db ->query ( "SELECT name FROM sqlite_master WHERE type='table' AND name=' " . $ this ->db ->escapeString ( $ table_name ) . "' " );
28- $ 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 ();
2930 if ( ! $ row ) {
3031 throw new Exception ( "Table ' $ table_name' not found in the database. " );
3132 }
@@ -37,8 +38,9 @@ public function theSqliteDatabaseShouldContainATableNamed( $table_name ) {
3738 */
3839 public function theTableShouldContainARowWithName ( $ table_name , $ name ) {
3940 $ this ->connectToDatabase ();
40- $ result = $ this ->db ->query ( "SELECT * FROM $ table_name WHERE name=' " . $ this ->db ->escapeString ( $ name ) . "' " );
41- $ row = $ result ->fetchArray ();
41+ $ stmt = $ this ->db ->prepare ( "SELECT * FROM $ table_name WHERE name=? " );
42+ $ stmt ->execute ( [ $ name ] );
43+ $ row = $ stmt ->fetch ();
4244 if ( ! $ row ) {
4345 throw new Exception ( "Row with name ' $ name' not found in table ' $ table_name'. " );
4446 }
@@ -50,14 +52,7 @@ public function theTableShouldContainARowWithName( $table_name, $name ) {
5052 public function theSqliteDatabaseShouldContainTheImportedData () {
5153 $ this ->connectToDatabase ();
5254 $ result = $ this ->db ->query ( "SELECT name FROM sqlite_master WHERE type='table' " );
53- $ tables = [];
54- while ( true ) {
55- $ row = $ result ->fetchArray ();
56- if ( false === $ row ) {
57- break ;
58- }
59- $ tables [] = $ row ['name ' ];
60- }
55+ $ tables = $ result ->fetchAll ( PDO ::FETCH_COLUMN );
6156 if ( empty ( $ tables ) ) {
6257 throw new Exception ( 'No tables found in the database after import. ' );
6358 }
@@ -77,7 +72,8 @@ private function connectToDatabase() {
7772 if ( ! $ this ->db ) {
7873 $ run_dir = $ this ->variables ['RUN_DIR ' ];
7974 $ db_file = $ run_dir . '/wp-content/database/.ht.sqlite ' ;
80- $ this ->db = new SQLite3 ( $ db_file );
75+ $ this ->db = new PDO ( 'sqlite: ' . $ db_file );
76+ $ this ->db ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
8177 }
8278 }
8379
@@ -104,45 +100,43 @@ protected function create_file( $filename, $content ) {
104100 file_put_contents ( $ full_path , $ content );
105101 }
106102
107-
108- ////
109103 /**
110104 * @Given /^the SQLite database contains some sample data$/
111105 */
112106 public function theSqliteDatabaseContainsSomeSampleData () {
113- $ this ->connectToDatabase ();
114- $ this ->db ->exec (
115- "
116- INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
117- VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
118- "
119- );
120-
121- // Insert or update sample data in wp_users
122- $ this ->db ->exec (
123- "
124- INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
125- VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
126- "
127- );
128-
129- // Insert or update sample data in wp_options
130- $ this ->db ->exec (
131- "
132- INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
133- VALUES
134- (1, 'siteurl', 'http://example.com', 'yes'),
135- (2, 'blogname', 'Test Blog', 'yes'),
136- (3, 'blogdescription', 'Just another WordPress site', 'yes'),
137- (4, 'users_can_register', '0', 'yes'),
138- (5, 'admin_email', 'admin@example.com', 'yes'),
139- (6, 'start_of_week', '1', 'yes'),
140- (7, 'use_balanceTags', '0', 'yes'),
141- (8, 'use_smilies', '1', 'yes'),
142- (9, 'require_name_email', '1', 'yes'),
143- (10, 'comments_notify', '1', 'yes');
144- "
145- );
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+ );
146140 }
147141
148142 /**
0 commit comments