Skip to content

Commit 10a1f72

Browse files
Copilotswissspidy
andcommitted
Refactor status command for better maintainability and test robustness
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 9da36cf commit 10a1f72

2 files changed

Lines changed: 26 additions & 53 deletions

File tree

features/db-status.feature

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,18 @@ Feature: Display database status overview
66
When I run `wp db status`
77
Then STDOUT should contain:
88
"""
9-
Database Name:
10-
"""
11-
And STDOUT should contain:
12-
"""
13-
Tables:
14-
"""
15-
And STDOUT should contain:
16-
"""
17-
Total Size:
9+
Database Name: wp_cli_test
1810
"""
11+
And STDOUT should match /^Tables:\s+\d+$/m
12+
And STDOUT should match /^Total Size:\s+[\d.]+ \wB$/m
1913
And STDOUT should contain:
2014
"""
2115
Prefix: wp_
2216
"""
17+
And STDOUT should match /^Engine:\s+\w+$/m
18+
And STDOUT should match /^Charset:\s+\w+$/m
19+
And STDOUT should match /^Collation:\s+\w+$/m
2320
And STDOUT should contain:
24-
"""
25-
Engine:
26-
"""
27-
And STDOUT should contain:
28-
"""
29-
Charset:
30-
"""
31-
And STDOUT should contain:
32-
"""
33-
Collation:
34-
"""
35-
And STDOUT should contain:
36-
"""
37-
Check Status:
38-
"""
39-
40-
Scenario: Verify database status shows correct database name
41-
Given a WP install
42-
43-
When I run `wp db status`
44-
Then STDOUT should contain:
45-
"""
46-
wp_cli_test
47-
"""
48-
49-
Scenario: Verify database status shows check status as OK
50-
Given a WP install
51-
52-
When I run `wp db status`
53-
Then STDOUT should contain:
5421
"""
5522
Check Status: OK
5623
"""

src/DB_Command.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,14 +1292,14 @@ public function status( $_, $assoc_args ) {
12921292
)
12931293
);
12941294

1295-
if ( empty( $db_size_bytes ) || $db_size_bytes <= 0 ) {
1296-
$db_size = '0 B';
1297-
} else {
1298-
$size_key = floor( log( $db_size_bytes ) / log( 1000 ) );
1295+
if ( ! empty( $db_size_bytes ) && $db_size_bytes > 0 ) {
1296+
$size_key = floor( log( (float) $db_size_bytes ) / log( 1000 ) );
12991297
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
1300-
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];
1298+
$size_format = $sizes[ $size_key ] ?? $sizes[0];
13011299
$divisor = pow( 1000, $size_key );
1302-
$db_size = round( $db_size_bytes / $divisor, 2 ) . ' ' . $size_format;
1300+
$db_size = round( (int) $db_size_bytes / $divisor, 2 ) . ' ' . $size_format;
1301+
} else {
1302+
$db_size = '0 B';
13031303
}
13041304

13051305
$prefix = $wpdb->prefix;
@@ -1375,14 +1375,20 @@ public function status( $_, $assoc_args ) {
13751375
$check_status = 'N/A';
13761376
}
13771377

1378-
WP_CLI::log( sprintf( '%-18s %s', 'Database Name:', DB_NAME ) );
1379-
WP_CLI::log( sprintf( '%-18s %d', 'Tables:', $table_count ) );
1380-
WP_CLI::log( sprintf( '%-18s %s', 'Total Size:', $db_size ) );
1381-
WP_CLI::log( sprintf( '%-18s %s', 'Prefix:', $prefix ) );
1382-
WP_CLI::log( sprintf( '%-18s %s', 'Engine:', $engine ) );
1383-
WP_CLI::log( sprintf( '%-18s %s', 'Charset:', $charset ) );
1384-
WP_CLI::log( sprintf( '%-18s %s', 'Collation:', $collation ) );
1385-
WP_CLI::log( sprintf( '%-18s %s', 'Check Status:', $check_status ) );
1378+
$status_items = [
1379+
'Database Name' => DB_NAME,
1380+
'Tables' => $table_count,
1381+
'Total Size' => $db_size,
1382+
'Prefix' => $prefix,
1383+
'Engine' => $engine,
1384+
'Charset' => $charset,
1385+
'Collation' => $collation,
1386+
'Check Status' => $check_status,
1387+
];
1388+
1389+
foreach ( $status_items as $label => $value ) {
1390+
WP_CLI::log( sprintf( '%-18s %s', $label . ':', $value ) );
1391+
}
13861392
}
13871393

13881394
/**

0 commit comments

Comments
 (0)