Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Cleanup CLI script and tests #78

Merged
merged 11 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions includes/class-woocommerce-custom-orders-table-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class WooCommerce_Custom_Orders_Table_CLI extends WP_CLI_Command {
protected $skipped_ids = array();

/**
* Ensure the custom table has been installed.
* Bootstrap the WP-CLI command.
*/
public function __construct() {
add_action( 'woocommerce_caught_exception', 'self::handle_exceptions' );

// Ensure the custom table has been installed.
WooCommerce_Custom_Orders_Table_Install::activate();
}

Expand Down Expand Up @@ -82,11 +85,9 @@ public function count() {
public function migrate( $args = array(), $assoc_args = array() ) {
global $wpdb;

add_action( 'woocommerce_caught_exception', 'self::handle_exceptions' );

$wpdb->suppress_errors();
$order_count = $this->count();

// Abort if there are no orders to migrate.
if ( ! $order_count ) {
return WP_CLI::warning( __( 'There are no orders to migrate, aborting.', 'woocommerce-custom-orders-table' ) );
}
Expand All @@ -107,8 +108,17 @@ public function migrate( $args = array(), $assoc_args = array() ) {
array_merge( $order_types, array( $assoc_args['batch-size'] ) )
);
$order_data = $wpdb->get_col( $order_query ); // WPCS: Unprepared SQL ok, DB call ok.
$batch_count = 1;

while ( ! empty( array_diff( $order_data, $this->skipped_ids ) ) ) {
WP_CLI::debug( sprintf(
/* Translators: %1$d is the batch number, %2$d is the batch size. */
__( 'Beginning batch #%1$d (%2$d orders/batch).', 'woocommerce-custom-orders-table' ),
$batch_count,
$assoc_args['batch-size']
) );

// Iterate over each order in this batch.
foreach ( $order_data as $order_id ) {
try {
$order = wc_get_order( $order_id );
Expand All @@ -122,6 +132,7 @@ public function migrate( $args = array(), $assoc_args = array() ) {
) );
}

// Either an error occurred or wc_get_order() could not find the order.
if ( false === $order ) {
$this->skipped_ids[] = $order_id;

Expand Down Expand Up @@ -161,6 +172,7 @@ public function migrate( $args = array(), $assoc_args = array() ) {
return WP_CLI::error( __( 'Infinite loop detected, aborting.', 'woocommerce-custom-orders-table' ) );
} else {
$order_data = $next_batch;
$batch_count++;
}
}

Expand Down
43 changes: 37 additions & 6 deletions tests/test-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CLITest extends TestCase {
* @before
*/
public function init_cli() {
WP_CLI::reset();

$this->cli = new WooCommerce_Custom_Orders_Table_CLI();
}

Expand All @@ -34,7 +36,7 @@ public function test_count() {
}

/**
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/45
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/45
*/
public function test_count_handles_refunded_orders() {
$this->toggle_use_custom_table( false );
Expand All @@ -47,6 +49,18 @@ public function test_count_handles_refunded_orders() {
$this->assertEquals( 3, $this->cli->count(), 'Expected to see 3 orders to migrate.' );
}

/**
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/57
*/
public function test_count_only_counts_unmigrated_orders() {
$this->toggle_use_custom_table( false );
$this->generate_orders( 3 );
$this->toggle_use_custom_table( true );
$this->generate_orders( 2 );

$this->assertSame( 3, $this->cli->count(), 'Expected to only see 3 orders to migrate.' );
}

public function test_migrate() {
$this->toggle_use_custom_table( false );
$order_ids = $this->generate_orders( 5 );
Expand All @@ -66,6 +80,7 @@ public function test_migrate() {
'Expected to see 5 orders in the custom table.'
);
$this->greaterThanOrEqual( 5, WP_CLI::$__counts['debug'], 'Expected to see at least five calls to WP_CLI::debug().' );
$this->cli->assertReceivedMessage( '5 orders were migrated.', 'success' );
}

public function test_migrate_works_in_batches() {
Expand All @@ -85,6 +100,10 @@ public function test_migrate_works_in_batches() {
$this->count_orders_in_table_with_ids( $order_ids ),
'Expected to see 5 total orders in the custom table.'
);

$this->cli->assertReceivedMessage( 'Beginning batch #1 (2 orders/batch).', 'debug' );
$this->cli->assertReceivedMessage( 'Beginning batch #2 (2 orders/batch).', 'debug' );
$this->cli->assertReceivedMessage( 'Beginning batch #3 (2 orders/batch).', 'debug' );
}

/**
Expand All @@ -93,6 +112,11 @@ public function test_migrate_works_in_batches() {
* @see DataStoreTest::test_populate_from_meta_handles_errors()
*/
public function test_migrate_stops_on_database_error() {
global $wpdb;

// This test will trigger a DB error due to the duplicate order key.
$wpdb->suppress_errors();

$this->toggle_use_custom_table( false );
$order1 = WC_Helper_Order::create_order();
$order1->set_order_key( '' );
Expand All @@ -104,8 +128,7 @@ public function test_migrate_stops_on_database_error() {

$this->cli->migrate();

$error = array_pop( WP_CLI::$__logger );
$this->assertEquals( 'error', $error['level'], 'Expected to see a call to WP_CLI::error().' );
$this->cli->assertReceivedMessageContaining( "Duplicate entry '' for key 'order_key'", 'error' );
}

public function test_migrate_catches_infinite_loops() {
Expand All @@ -131,7 +154,7 @@ public function test_migrate_catches_infinite_loops() {
}

/**
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/43
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/43
*/
public function test_migrate_handles_errors_with_wc_get_order() {
$this->toggle_use_custom_table( false );
Expand All @@ -153,7 +176,7 @@ public function test_migrate_handles_errors_with_wc_get_order() {
}

/**
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/45
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/45
*/
public function test_migrate_handles_refunded_orders() {
$this->toggle_use_custom_table( false );
Expand All @@ -174,7 +197,7 @@ public function test_migrate_handles_refunded_orders() {
}

/**
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/56
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/56
*/
public function test_migrate_handles_exceptions() {
$this->toggle_use_custom_table( false );
Expand Down Expand Up @@ -211,6 +234,14 @@ public function test_migrate_with_duplicate_ids() {
$this->assertEquals( 1, $this->count_orders_in_table_with_ids( $order_id ));
}

public function test_migrate_aborts_if_no_orders_require_migration() {
$this->assertSame( 0, $this->cli->count(), 'Expected to start with 0 orders.' );

$this->cli->migrate();

$this->assertSame( 1, WP_CLI::$__counts['warning'], 'A warning should have been displayed.' );
}

public function test_backfill() {
$order_ids = $this->generate_orders( 5 );
$index = 0;
Expand Down
63 changes: 63 additions & 0 deletions tests/test-tools/class-wp-cli-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,71 @@
* @author Liquid Web
*/

use PHPUnit\Framework\Assert;

if ( ! class_exists( 'WP_CLI_Command' ) ) {
class WP_CLI_Command {

/**
* Assert that the given message was received.
*
* @param string $message The exact message to search for.
* @param string $level Optional message level to filter by.
* @param string $error The PHPUnit error message if the assertion fails.
*/
public function assertReceivedMessage( $message, $level = '', $error = '' ) {
$logger = WP_CLI::$__logger;

if ( ! empty( $level ) ) {
$logger = array_filter( $logger, function ( $log ) use ( $message, $level ) {
return $level === $log['level'] && $message === $log['message'];
} );
} else {
$logger = array_filter( $logger, function ( $log ) use ( $message ) {
return $message === $log['message'];
} );
}

if ( empty( $error ) ) {
if ( ! empty( $level ) ) {
$error = sprintf( 'Did not see %1$s-level message "%2$s".', $level, $message );
} else {
$error = sprintf( 'Did not see message "%2$s".', $message );
}
}

Assert::assertGreaterThan( 0, count( $logger ), $error );
}

/**
* Assert that the given message was received.
*
* @param string $message The substring within a message to search for.
* @param string $level Optional message level to filter by.
* @param string $error The PHPUnit error message if the assertion fails.
*/
public function assertReceivedMessageContaining( $message, $level = '', $error = '' ) {
$logger = WP_CLI::$__logger;

if ( ! empty( $level ) ) {
$logger = array_filter( $logger, function ( $log ) use ( $message, $level ) {
return $level === $log['level'] && false !== strpos( $log['message'], $message );
} );
} else {
$logger = array_filter( $logger, function ( $log ) use ( $message ) {
return false !== strpos( $log['message'], $message );
} );
}

if ( empty( $error ) ) {
if ( ! empty( $level ) ) {
$error = sprintf( 'Did not see a %1$s-level message containing "%2$s".', $level, $message );
} else {
$error = sprintf( 'Did not see a message containing "%2$s".', $message );
}
}

Assert::assertGreaterThan( 0, count( $logger ), $error );
}
}
}
20 changes: 13 additions & 7 deletions tests/test-tools/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
class WP_CLI {
public static $__commands = array();
public static $__logger = array();
public static $__counts = array(
'debug' => 0,
'info' => 0,
'success' => 0,
'warning' => 0,
'error' => 0,
);
public static $__counts = array();

public static function reset() {
self::$__commands = array();
self::$__logger = array();
self::$__counts = array(
'debug' => 0,
'info' => 0,
'success' => 0,
'warning' => 0,
'error' => 0,
);
}

public static function add_command( $name, $callable, $args = array() ) {
self::$__commands[] = func_get_args();
Expand Down
1 change: 1 addition & 0 deletions tests/testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function truncate_table() {

WooCommerce_Custom_Orders_Table_Install::activate();

$wpdb->suppress_errors( false );
$wpdb->query( 'DELETE FROM ' . esc_sql( wc_custom_order_table()->get_table_name() ) );
}

Expand Down