Skip to content

Commit 0cff5c3

Browse files
committed
Sqlite: Add import tests
1 parent e48f934 commit 0cff5c3

4 files changed

Lines changed: 156 additions & 2 deletions

File tree

‎behat.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
default:
2+
suites:
3+
default:
4+
contexts:
5+
- SQLiteFeatureContext
6+
paths:
7+
- features
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
use Behat\Behat\Context\Context;
3+
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
4+
use Behat\Gherkin\Node\PyStringNode;
5+
use WP_CLI\Tests\Context\FeatureContext as WPCLIFeatureContext;
6+
7+
class SQLiteFeatureContext extends WPCLIFeatureContext implements Context {
8+
9+
private $db;
10+
11+
/**
12+
* @Given /^a SQL dump file named "([^"]*)" with content:$/
13+
*/
14+
public function aSqlDumpFileNamedWithContent( $filename, PyStringNode $content ) {
15+
$this->create_file( $filename, $content );
16+
}
17+
18+
/**
19+
* @Then /^the SQLite database should contain a table named "([^"]*)"$/
20+
*/
21+
public function theSqliteDatabaseShouldContainATableNamed( $tableName ) {
22+
$this->connectToDatabase();
23+
$result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table' AND name='$tableName'" );
24+
$row = $result->fetchArray();
25+
if ( ! $row ) {
26+
throw new Exception( "Table '$tableName' not found in the database." );
27+
}
28+
}
29+
30+
/**
31+
* @Then /^the "([^"]*)" should contain a row with name "([^"]*)"$/
32+
*/
33+
public function theTableShouldContainARowWithName( $tableName, $name ) {
34+
$this->connectToDatabase();
35+
$result = $this->db->query( "SELECT * FROM $tableName WHERE name='$name'" );
36+
$row = $result->fetchArray();
37+
if ( ! $row ) {
38+
throw new Exception( "Row with name '$name' not found in table '$tableName'." );
39+
}
40+
}
41+
42+
/**
43+
* @Then /^the SQLite database should contain the imported data$/
44+
*/
45+
public function theSqliteDatabaseShouldContainTheImportedData() {
46+
$this->connectToDatabase();
47+
$result = $this->db->query( "SELECT name FROM sqlite_master WHERE type='table'" );
48+
$tables = [];
49+
while ( $row = $result->fetchArray() ) {
50+
$tables[] = $row['name'];
51+
}
52+
if ( empty( $tables ) ) {
53+
throw new Exception( 'No tables found in the database after import.' );
54+
}
55+
}
56+
57+
/**
58+
* @Then /^the file "([^"]*)" should exist$/
59+
*/
60+
public function theFileShouldExist( $filename ) {
61+
$full_path = $this->variables['RUN_DIR'] . '/' . $filename;
62+
if ( ! file_exists( $full_path ) ) {
63+
throw new Exception( "File does not exist: $full_path" );
64+
}
65+
}
66+
67+
/**
68+
* @BeforeScenario
69+
*/
70+
public function beforeScenario( BeforeScenarioScope $scope ) {
71+
parent::beforeScenario( $scope );
72+
$this->variables['DB_TYPE'] = 'sqlite';
73+
}
74+
75+
private function connectToDatabase() {
76+
if ( ! $this->db ) {
77+
$run_dir = $this->variables['RUN_DIR'];
78+
$db_file = $run_dir . '/wp-content/database/.ht.sqlite';
79+
$this->db = new SQLite3( $db_file );
80+
}
81+
}
82+
83+
/**
84+
* Create a file with given filename and content.
85+
*
86+
* @param string $filename
87+
* @param string|PyStringNode $content
88+
*/
89+
protected function create_file( $filename, $content ) {
90+
$content = $this->replace_variables( (string) $content );
91+
$run_dir = $this->variables['RUN_DIR'];
92+
93+
if ( strpos( $filename, '/' ) !== false ) {
94+
$subdir = dirname( $filename );
95+
$run_dir .= "/$subdir";
96+
if ( ! is_dir( $run_dir ) ) {
97+
mkdir( $run_dir, 0777, true );
98+
}
99+
}
100+
101+
$full_path = $run_dir . '/' . basename( $filename );
102+
103+
file_put_contents( $full_path, $content );
104+
}
105+
}

‎features/sqlite_import.feature‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Feature: WP-CLI SQLite Import Command
2+
In order to populate a WordPress SQLite database
3+
As a website administrator
4+
I need to be able to import data into SQLite databases using WP-CLI
5+
6+
Background:
7+
Given a WP installation
8+
9+
Scenario: Successfully import a SQLite database
10+
Given a SQL dump file named "test_import.sql" with content:
11+
"""
12+
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
13+
INSERT INTO test_table (name) VALUES ('Test Name');
14+
"""
15+
When I run `wp sqlite import test_import.sql`
16+
Then STDOUT should contain:
17+
"""
18+
Success: Imported from 'test_import.sql'.
19+
"""
20+
And the SQLite database should contain a table named "test_table"
21+
And the "test_table" should contain a row with name "Test Name"
22+
23+
Scenario: Attempt to import without specifying a file
24+
When I try `wp sqlite import`
25+
Then STDOUT should contain:
26+
"""
27+
usage: wp sqlite import <file>
28+
"""
29+
And the return code should be 1
30+
31+
Scenario: Import from STDIN
32+
Given a SQL dump file named "test_import.sql" with content:
33+
"""
34+
CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT);
35+
INSERT INTO test_table (name) VALUES ('Test Name');
36+
"""
37+
When I run `cat test_import.sql | wp sqlite import -`
38+
Then STDOUT should contain:
39+
"""
40+
Success: Imported from 'STDIN'.
41+
"""
42+
And the SQLite database should contain the imported data

‎src/SQLite_Command.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SQLite_Command extends WP_CLI_Command {
2121
* $ wp sqlite import wordpress_dbase.sql
2222
* Success: Imported from 'import wordpress_dbase.sql'.
2323
*
24-
* @when before_wp_load
24+
* @when after_wp_load
2525
*/
2626
public function import( $args, $assoc_args ) {
2727

@@ -76,7 +76,7 @@ public function import( $args, $assoc_args ) {
7676
* $ wp sqlite export --exclude_tables=wp_posts,wp_users
7777
* Success: Exported to 'wordpress_dbase.sql'.
7878
*
79-
* @when before_wp_load
79+
* @when after_wp_load
8080
*/
8181
public function export( $args, $assoc_args ) {
8282
WP_CLI::success( 'Exporting database...' );

0 commit comments

Comments
 (0)