Skip to content

Commit ba49533

Browse files
committed
Sqlite: Add export tests
1 parent 0cff5c3 commit ba49533

3 files changed

Lines changed: 179 additions & 1 deletion

File tree

‎features/bootstrap/SQLiteFeatureContext.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,67 @@ protected function create_file( $filename, $content ) {
102102

103103
file_put_contents( $full_path, $content );
104104
}
105+
106+
107+
////
108+
/**
109+
* @Given /^the SQLite database contains some sample data$/
110+
*/
111+
public function theSqliteDatabaseContainsSomeSampleData() {
112+
$this->connectToDatabase();
113+
$this->db->exec(
114+
"
115+
INSERT OR REPLACE INTO wp_posts (ID, post_title, post_content, post_type, post_status)
116+
VALUES (1, 'Sample Post', 'This is a sample post content.', 'post', 'publish');
117+
"
118+
);
119+
120+
// Insert or update sample data in wp_users
121+
$this->db->exec(
122+
"
123+
INSERT OR REPLACE INTO wp_users (ID, user_login, user_pass, user_nicename, user_email)
124+
VALUES (1, 'testuser', 'password_hash', 'Test User', 'testuser@example.com');
125+
"
126+
);
127+
128+
// Insert or update sample data in wp_options
129+
$this->db->exec(
130+
"
131+
INSERT OR REPLACE INTO wp_options (option_id, option_name, option_value, autoload)
132+
VALUES
133+
(1, 'siteurl', 'http://example.com', 'yes'),
134+
(2, 'blogname', 'Test Blog', 'yes'),
135+
(3, 'blogdescription', 'Just another WordPress site', 'yes'),
136+
(4, 'users_can_register', '0', 'yes'),
137+
(5, 'admin_email', 'admin@example.com', 'yes'),
138+
(6, 'start_of_week', '1', 'yes'),
139+
(7, 'use_balanceTags', '0', 'yes'),
140+
(8, 'use_smilies', '1', 'yes'),
141+
(9, 'require_name_email', '1', 'yes'),
142+
(10, 'comments_notify', '1', 'yes');
143+
"
144+
);
145+
}
146+
147+
/**
148+
* @Then /^the file "([^"]*)" should contain:$/
149+
*/
150+
public function theFileShouldContain( $filename, PyStringNode $content ) {
151+
$full_path = $this->variables['RUN_DIR'] . '/' . $filename;
152+
$file_content = file_get_contents( $full_path );
153+
if ( strpos( $file_content, (string) $content ) === false ) {
154+
throw new Exception( "File does not contain expected content:\n" . $content );
155+
}
156+
}
157+
158+
/**
159+
* @Then /^the file "([^"]*)" should not contain:$/
160+
*/
161+
public function theFileShouldNotContain( $filename, PyStringNode $content ) {
162+
$full_path = $this->variables['RUN_DIR'] . '/' . $filename;
163+
$file_content = file_get_contents( $full_path );
164+
if ( strpos( $file_content, (string) $content ) !== false ) {
165+
throw new Exception( "File contains unexpected content:\n" . $content );
166+
}
167+
}
105168
}

‎features/sqlite_export.feature‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Feature: WP-CLI SQLite Export Command
2+
In order to backup or migrate a WordPress SQLite database
3+
As a website administrator
4+
I need to be able to export data from SQLite databases using WP-CLI
5+
6+
Background:
7+
Given a WP installation
8+
And the SQLite database contains some sample data
9+
10+
Scenario: Successfully export the entire SQLite database
11+
When I run `wp sqlite export test_export.sql`
12+
Then STDOUT should contain:
13+
"""
14+
Success: Exporting database...
15+
Success: Export complete. File written to test_export.sql
16+
"""
17+
And the file "test_export.sql" should exist
18+
And the file "test_export.sql" should contain:
19+
"""
20+
CREATE TABLE
21+
"""
22+
And the file "test_export.sql" should contain:
23+
"""
24+
INSERT INTO
25+
"""
26+
27+
Scenario: Export specific tables
28+
When I run `wp sqlite export test_export_specific.sql --tables=wp_posts,wp_users`
29+
Then STDOUT should contain:
30+
"""
31+
Success: Exporting database...
32+
Success: Export complete. File written to test_export_specific.sql
33+
"""
34+
And the file "test_export_specific.sql" should exist
35+
And the file "test_export_specific.sql" should contain:
36+
"""
37+
CREATE TABLE wp_posts
38+
"""
39+
And the file "test_export_specific.sql" should contain:
40+
"""
41+
CREATE TABLE wp_users
42+
"""
43+
But the file "test_export_specific.sql" should not contain:
44+
"""
45+
CREATE TABLE wp_options
46+
"""
47+
48+
Scenario: Export all tables except specific ones
49+
When I run `wp sqlite export test_export_exclude.sql --exclude_tables=wp_posts,wp_users`
50+
Then STDOUT should contain:
51+
"""
52+
Success: Exporting database...
53+
Success: Export complete. File written to test_export_exclude.sql
54+
"""
55+
And the file "test_export_exclude.sql" should exist
56+
And the file "test_export_exclude.sql" should not contain:
57+
"""
58+
CREATE TABLE wp_posts
59+
"""
60+
And the file "test_export_exclude.sql" should not contain:
61+
"""
62+
CREATE TABLE wp_users
63+
"""
64+
But the file "test_export_exclude.sql" should contain:
65+
"""
66+
CREATE TABLE wp_options
67+
"""
68+
69+
Scenario: Export to STDOUT
70+
When I run `wp sqlite export -`
71+
Then STDOUT should contain:
72+
"""
73+
-- Dumping data for table
74+
"""
75+
And STDOUT should contain:
76+
"""
77+
CREATE TABLE
78+
"""
79+
And STDOUT should contain:
80+
"""
81+
INSERT INTO
82+
"""
83+
84+
Scenario: Export with porcelain flag
85+
When I run `wp sqlite export --porcelain`
86+
Then STDOUT should match /^[\w-]+\.sql$/
87+
And STDOUT should not contain:
88+
"""
89+
Success: Exporting database...
90+
"""
91+
92+
Scenario: Export with both --tables and --exclude_tables
93+
When I run `wp sqlite export test_export_both.sql --tables=wp_posts,wp_users,wp_options --exclude_tables=wp_users`
94+
Then STDOUT should contain:
95+
"""
96+
Success: Exporting database...
97+
Success: Export complete. File written to test_export_both.sql
98+
"""
99+
And the file "test_export_both.sql" should exist
100+
And the file "test_export_both.sql" should contain:
101+
"""
102+
CREATE TABLE wp_posts
103+
"""
104+
And the file "test_export_both.sql" should contain:
105+
"""
106+
CREATE TABLE wp_options
107+
"""
108+
But the file "test_export_both.sql" should not contain:
109+
"""
110+
CREATE TABLE wp_users
111+
"""

‎src/SQLite_Command.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ public function import( $args, $assoc_args ) {
7979
* @when after_wp_load
8080
*/
8181
public function export( $args, $assoc_args ) {
82-
WP_CLI::success( 'Exporting database...' );
82+
$is_porcelain = isset( $assoc_args['porcelain'] );
83+
84+
if ( ! $is_porcelain ) {
85+
WP_CLI::success( 'Exporting database...' );
86+
}
8387
$export = new Export();
8488

8589
if ( ! empty( $args[0] ) ) {

0 commit comments

Comments
 (0)