Skip to content

Commit 7fb4951

Browse files
committed
Update: Fix lint errors
1 parent 889b907 commit 7fb4951

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
10+
class WP_CLI_SQLite_FeatureContext extends WPCLIFeatureContext implements Context {
11+
12+
private $db;
13+
14+
/**
15+
* @Given /^a SQL dump file named "([^"]*)" with content:$/
16+
*/
17+
public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content ) {
18+
$this->create_file( $filename, $content );
19+
}
20+
21+
/**
22+
* @Then /^the SQLite database should contain a table named "([^"]*)"$/
23+
*/
24+
public function theSqliteDatabaseShouldContainATableNamed( $table_name ) {
25+
$this->connectToDatabase();
26+
$result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table' AND name='$table_name'" );
27+
$row = $result->fetchArray();
28+
if ( ! $row ) {
29+
throw new Exception( "Table '$table_name' not found in the database." );
30+
}
31+
}
32+
33+
/**
34+
* @Then /^the "([^"]*)" should contain a row with name "([^"]*)"$/
35+
*/
36+
public function theTableShouldContainARowWithName( $table_name, $name ) {
37+
$this->connectToDatabase();
38+
$result = $this->db->query( "SELECT * FROM $table_name WHERE name='$name'" );
39+
$row = $result->fetchArray();
40+
if ( ! $row ) {
41+
throw new Exception( "Row with name '$name' not found in table '$table_name'." );
42+
}
43+
}
44+
45+
/**
46+
* @Then /^the SQLite database should contain the imported data$/
47+
*/
48+
public function theSqliteDatabaseShouldContainTheImportedData() {
49+
$this->connectToDatabase();
50+
$result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table'" );
51+
$tables = [];
52+
while ( $row = $result->fetchArray() ) {
53+
$tables[] = $row['name'];
54+
}
55+
if ( empty( $tables ) ) {
56+
throw new Exception( 'No tables found in the database after import.' );
57+
}
58+
}
59+
60+
/**
61+
* @Then /^the file "([^"]*)" should exist$/
62+
*/
63+
public function theFileShouldExist( $filename ) {
64+
$full_path = $this->variables['RUN_DIR'] . '/' . $filename;
65+
if ( ! file_exists( $full_path ) ) {
66+
throw new Exception( "File does not exist: $full_path" );
67+
}
68+
}
69+
70+
/**
71+
* @BeforeScenario
72+
*/
73+
public function beforeScenario( BeforeScenarioScope $scope ) {
74+
parent::beforeScenario( $scope );
75+
$this->variables['DB_TYPE'] = 'sqlite';
76+
}
77+
78+
private function connectToDatabase() {
79+
if ( ! $this->db ) {
80+
$run_dir = $this->variables['RUN_DIR'];
81+
$db_file = $run_dir . '/wp-content/database/.ht.sqlite';
82+
$this->db = new SQLite3( $db_file );
83+
}
84+
}
85+
86+
/**
87+
* Create a file with given filename and content.
88+
*
89+
* @param string $filename
90+
* @param string|PyStringNode $content
91+
*/
92+
protected function create_file( $filename, $content ) {
93+
$content = $this->replace_variables( (string) $content );
94+
$run_dir = $this->variables['RUN_DIR'];
95+
96+
if ( strpos( $filename, '/' ) !== false ) {
97+
$subdir = dirname( $filename );
98+
$run_dir .= "/$subdir";
99+
if ( ! is_dir( $run_dir ) ) {
100+
mkdir( $run_dir, 0777, true );
101+
}
102+
}
103+
104+
$full_path = $run_dir . '/' . basename( $filename );
105+
106+
file_put_contents( $full_path, $content );
107+
}
108+
109+
110+
////
111+
/**
112+
* @Given /^the SQLite database contains some sample data$/
113+
*/
114+
public function theSqliteDatabaseContainsSomeSampleData() {
115+
$this->connectToDatabase();
116+
$this->db->exec(
117+
"
118+
INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
119+
VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
120+
"
121+
);
122+
123+
// Insert or update sample data in wp_users
124+
$this->db->exec(
125+
"
126+
INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
127+
VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
128+
"
129+
);
130+
131+
// Insert or update sample data in wp_options
132+
$this->db->exec(
133+
"
134+
INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
135+
VALUES
136+
(1, 'siteurl', 'http://example.com', 'yes'),
137+
(2, 'blogname', 'Test Blog', 'yes'),
138+
(3, 'blogdescription', 'Just another WordPress site', 'yes'),
139+
(4, 'users_can_register', '0', 'yes'),
140+
(5, 'admin_email', 'admin@example.com', 'yes'),
141+
(6, 'start_of_week', '1', 'yes'),
142+
(7, 'use_balanceTags', '0', 'yes'),
143+
(8, 'use_smilies', '1', 'yes'),
144+
(9, 'require_name_email', '1', 'yes'),
145+
(10, 'comments_notify', '1', 'yes');
146+
"
147+
);
148+
}
149+
150+
/**
151+
* @Then /^the file "([^"]*)" should contain:$/
152+
*/
153+
public function theFileShouldContain( $filename, PyStringNode $content ) {
154+
$full_path = $this->variables['RUN_DIR'] . '/' . $filename;
155+
$file_content = file_get_contents( $full_path );
156+
if ( strpos( $file_content, (string) $content ) === false ) {
157+
throw new Exception( "File does not contain expected content:\n" . $content );
158+
}
159+
}
160+
161+
/**
162+
* @Then /^the file "([^"]*)" should not contain:$/
163+
*/
164+
public function theFileShouldNotContain( $filename, PyStringNode $content ) {
165+
$full_path = $this->variables['RUN_DIR'] . '/' . $filename;
166+
$file_content = file_get_contents( $full_path );
167+
if ( strpos( $file_content, (string) $content ) !== false ) {
168+
throw new Exception( "File contains unexpected content:\n" . $content );
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)