|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Automattic\WP_CLI\SQLite; |
| 4 | + |
| 5 | +use Behat\Behat\Context\Context; |
| 6 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
| 7 | +use Behat\Gherkin\Node\PyStringNode; |
| 8 | +use WP_CLI\Tests\Context\FeatureContext as WPCLIFeatureContext; |
| 9 | +use SQLite3; |
| 10 | +use Exception; |
| 11 | + |
| 12 | +class SQLiteFeatureContext extends WPCLIFeatureContext implements Context { |
| 13 | + |
| 14 | + private $db; |
| 15 | + |
| 16 | + /** |
| 17 | + * @Given /^a SQL dump file named "([^"]*)" with content:$/ |
| 18 | + */ |
| 19 | + public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content ) { |
| 20 | + $this->create_file( $filename, $content ); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @Then /^the SQLite database should contain a table named "([^"]*)"$/ |
| 25 | + */ |
| 26 | + public function theSqliteDatabaseShouldContainATableNamed( $table_name ) { |
| 27 | + $this->connectToDatabase(); |
| 28 | + $result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table' AND name='$table_name'" ); |
| 29 | + $row = $result->fetchArray(); |
| 30 | + if ( ! $row ) { |
| 31 | + throw new Exception( "Table '$table_name' not found in the database." ); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @Then /^the "([^"]*)" should contain a row with name "([^"]*)"$/ |
| 37 | + */ |
| 38 | + public function theTableShouldContainARowWithName( $table_name, $name ) { |
| 39 | + $this->connectToDatabase(); |
| 40 | + $result = $this->db->query( "SELECT * FROM $table_name WHERE name='$name'" ); |
| 41 | + $row = $result->fetchArray(); |
| 42 | + if ( ! $row ) { |
| 43 | + throw new Exception( "Row with name '$name' not found in table '$table_name'." ); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @Then /^the SQLite database should contain the imported data$/ |
| 49 | + */ |
| 50 | + public function theSqliteDatabaseShouldContainTheImportedData() { |
| 51 | + $this->connectToDatabase(); |
| 52 | + $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 | + } |
| 61 | + if ( empty( $tables ) ) { |
| 62 | + throw new Exception( 'No tables found in the database after import.' ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @Then /^the file "([^"]*)" should exist$/ |
| 68 | + */ |
| 69 | + public function theFileShouldExist( $filename ) { |
| 70 | + $full_path = $this->variables['RUN_DIR'] . '/' . $filename; |
| 71 | + if ( ! file_exists( $full_path ) ) { |
| 72 | + throw new Exception( "File does not exist: $full_path" ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private function connectToDatabase() { |
| 77 | + if ( ! $this->db ) { |
| 78 | + $run_dir = $this->variables['RUN_DIR']; |
| 79 | + $db_file = $run_dir . '/wp-content/database/.ht.sqlite'; |
| 80 | + $this->db = new SQLite3( $db_file ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Create a file with given filename and content. |
| 86 | + * |
| 87 | + * @param string $filename |
| 88 | + * @param string|PyStringNode $content |
| 89 | + */ |
| 90 | + protected function create_file( $filename, $content ) { |
| 91 | + $content = $this->replace_variables( (string) $content ); |
| 92 | + $run_dir = $this->variables['RUN_DIR']; |
| 93 | + |
| 94 | + if ( strpos( $filename, '/' ) !== false ) { |
| 95 | + $subdir = dirname( $filename ); |
| 96 | + $run_dir .= "/$subdir"; |
| 97 | + if ( ! is_dir( $run_dir ) ) { |
| 98 | + mkdir( $run_dir, 0777, true ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + $full_path = $run_dir . '/' . basename( $filename ); |
| 103 | + |
| 104 | + file_put_contents( $full_path, $content ); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + //// |
| 109 | + /** |
| 110 | + * @Given /^the SQLite database contains some sample data$/ |
| 111 | + */ |
| 112 | + 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 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @Then /^the file "([^"]*)" should contain:$/ |
| 150 | + */ |
| 151 | + public function theFileShouldContain( $filename, PyStringNode $content ) { |
| 152 | + $full_path = $this->variables['RUN_DIR'] . '/' . $filename; |
| 153 | + $file_content = file_get_contents( $full_path ); |
| 154 | + if ( strpos( $file_content, (string) $content ) === false ) { |
| 155 | + throw new Exception( "File does not contain expected content:\n" . $content ); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @Then /^the file "([^"]*)" should not contain:$/ |
| 161 | + */ |
| 162 | + public function theFileShouldNotContain( $filename, PyStringNode $content ) { |
| 163 | + $full_path = $this->variables['RUN_DIR'] . '/' . $filename; |
| 164 | + $file_content = file_get_contents( $full_path ); |
| 165 | + if ( strpos( $file_content, (string) $content ) !== false ) { |
| 166 | + throw new Exception( "File contains unexpected content:\n" . $content ); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments