Skip to content

Commit 9dd8078

Browse files
authored
Merge pull request #4 from Automattic/add/bdd_tests
Sqlite-command: Add/bdd tests
2 parents e48f934 + ab2f44a commit 9dd8078

8 files changed

Lines changed: 418 additions & 41 deletions

File tree

‎.github/workflows/testing.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Testing
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
- master
9+
schedule:
10+
- cron: "17 1 * * *" # Run every day on a seemly random time.
11+
12+
jobs:
13+
test:
14+
uses: wp-cli/.github/.github/workflows/reusable-testing.yml@main
15+
with:
16+
minimum-php: "7.4"

‎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+
- Automattic\WP_CLI\SQLite\SQLiteFeatureContext
6+
paths:
7+
- features

‎composer.json‎

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
{
2-
"name": "automattic/wp-cli-sqlite-command",
3-
"description": "A command for importing and exporting from an SQLite database",
4-
"homepage": "https://github.com/Automattic/wp-cli-sqlite-command",
5-
"type": "wp-cli-package",
6-
"license": "GPL-2.0-or-later",
7-
"autoload": {
8-
"files": [ "command.php" ],
9-
"classmap": ["src/" ]
10-
},
11-
"authors": [
12-
{
13-
"name": "Jeroen Pfeil",
14-
"email": "jeroen.pfeil@automattic.com"
15-
}
2+
"name": "automattic/wp-cli-sqlite-command",
3+
"description": "A command for importing and exporting from an SQLite database",
4+
"homepage": "https://github.com/Automattic/wp-cli-sqlite-command",
5+
"type": "wp-cli-package",
6+
"license": "GPL-2.0-or-later",
7+
"autoload": {
8+
"files": [
9+
"command.php"
1610
],
17-
"require": {
18-
"php": ">=7.4",
19-
"wp-cli/wp-cli": "^2.5"
20-
},
21-
"require-dev": {
22-
"wp-cli/wp-cli-tests": "^4"
23-
},
24-
"scripts": {
25-
"behat": "run-behat-tests",
26-
"behat-rerun": "rerun-behat-tests",
27-
"lint": "run-linter-tests",
28-
"phpcs": "run-phpcs-tests",
29-
"phpcbf": "run-phpcbf-cleanup",
30-
"phpunit": "run-php-unit-tests",
31-
"prepare-tests": "install-package-tests",
32-
"test": [
33-
"@lint",
34-
"@phpcs",
35-
"@phpunit",
36-
"@behat"
37-
]
38-
},
39-
"config": {
40-
"allow-plugins": {
41-
"dealerdirect/phpcodesniffer-composer-installer": true
42-
}
11+
"classmap": [
12+
"src/"
13+
]
14+
},
15+
"autoload-dev": {
16+
"psr-4": {
17+
"Automattic\\WP_CLI\\SQLite\\": "features/bootstrap/"
4318
}
19+
},
20+
"authors": [
21+
{
22+
"name": "Jeroen Pfeil",
23+
"email": "jeroen.pfeil@automattic.com"
24+
}
25+
],
26+
"require": {
27+
"php": ">=7.4",
28+
"wp-cli/wp-cli": "^2.5"
29+
},
30+
"require-dev": {
31+
"wp-cli/wp-cli-tests": "^4.3.2",
32+
"phpcompatibility/php-compatibility": "dev-develop"
33+
},
34+
"scripts": {
35+
"behat": "run-behat-tests",
36+
"behat-rerun": "rerun-behat-tests",
37+
"lint": "run-linter-tests",
38+
"phpcs": "run-phpcs-tests",
39+
"phpcbf": "run-phpcbf-cleanup",
40+
"phpunit": "run-php-unit-tests",
41+
"prepare-tests": "install-package-tests",
42+
"test": [
43+
"@lint",
44+
"@phpcs",
45+
"@phpunit",
46+
"@behat"
47+
]
48+
},
49+
"config": {
50+
"allow-plugins": {
51+
"dealerdirect/phpcodesniffer-composer-installer": true
52+
}
53+
}
4454
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

‎features/pass.feature‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Always Passing Test
2+
Scenario: Evaluate true expression
3+
Given a WP install
4+
5+
When I run `wp eval "var_export(true);"`
6+
Then STDOUT should contain:
7+
"""
8+
true
9+
"""

0 commit comments

Comments
 (0)