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

Commit

Permalink
Merge 31b75f5 into 74318bd
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegrunwell committed Dec 15, 2018
2 parents 74318bd + 31b75f5 commit 8d7f243
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
6 changes: 6 additions & 0 deletions includes/class-woocommerce-custom-orders-table-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public function migrate( $args = array(), $assoc_args = array() ) {
$order_id
) );
}

WP_CLI::debug( sprintf(
/* Translators: %1$d is the migrated order ID. */
__( 'Order ID %1$d has been migrated.', 'woocommerce-custom-orders-table' ),
$order_id
) );
}

$progress->tick();
Expand Down
14 changes: 14 additions & 0 deletions tests/test-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function test_plugin_only_loads_after_woocommerce() {
// Test bootstrapping may have initialized it for us.
$wc_custom_order_table = null;

$this->assertSame( 10, has_action( 'woocommerce_init', 'wc_custom_order_table' ) );

do_action( 'woocommerce_init' );

$this->assertInstanceOf(
Expand All @@ -22,4 +24,16 @@ public function test_plugin_only_loads_after_woocommerce() {
'The plugin should not be bootstrapped until woocommerce_init.'
);
}

/**
* @testWith ["WC_CUSTOM_ORDER_TABLE_URL"]
* ["WC_CUSTOM_ORDER_TABLE_PATH"]
*/
public function test_constants_are_defined( $constant ) {
$this->assertTrue( defined( $constant ) );
}

public function test_autoloader_registered() {
$this->assertContains( 'wc_custom_order_table_autoload', spl_autoload_functions() );
}
}
64 changes: 64 additions & 0 deletions tests/test-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public function init_cli() {
WP_CLI::reset();

$this->cli = new WooCommerce_Custom_Orders_Table_CLI();

// Reset the WP_CLI counts.
WP_CLI::$__logger = array();
WP_CLI::$__counts = array(
'debug' => 0,
'info' => 0,
'success' => 0,
'warning' => 0,
'error' => 0,
);
}

public function test_count() {
Expand Down Expand Up @@ -106,6 +116,14 @@ public function test_migrate_works_in_batches() {
$this->cli->assertReceivedMessage( 'Beginning batch #3 (2 orders/batch).', 'debug' );
}

public function test_migrate_warns_if_no_orders_need_migrating() {
$this->assertEquals( 0, $this->cli->count(), 'Expected no orders to need migration.' );

$this->cli->migrate();

$this->assertEquals( 1, WP_CLI::$__counts['warning'], 'Expected to see a warning if no orders require migration.' );
}

/**
* Trigger a database error in the same way as the test_populate_from_meta_handles_errors test.
*
Expand Down Expand Up @@ -175,6 +193,22 @@ public function test_migrate_handles_errors_with_wc_get_order() {
);
}

public function test_migrate_warns_if_no_orders_were_successfully_migrated() {
$this->toggle_use_custom_table( false );
$order = WC_Helper_Order::create_order();
$this->toggle_use_custom_table( true );

// For the first item, cause wc_get_order() to break due to a non-existent class.
add_filter( 'woocommerce_order_class', function ( $classname ) {
return 'SomeNonExistentClassName';
} );

$this->cli->migrate();

$this->assertNull( $this->get_order_row( $order->get_id() ) );
$this->assertEquals( 1, WP_CLI::$__counts['warning'], 'Expected to see a warning if no orders were migrated.' );
}

/**
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/45
*/
Expand Down Expand Up @@ -342,6 +376,14 @@ public function test_backfill() {
}
}

public function test_backfill_warns_if_no_orders_need_migrating() {
$this->assertEquals( 0, $this->cli->count(), 'Expected no orders to need migration.' );

$this->cli->backfill();

$this->assertEquals( 1, WP_CLI::$__counts['warning'], 'Expected to see a warning if no orders require migration.' );
}

public function test_backfill_when_an_order_has_been_deleted() {
$order1 = WC_Helper_Order::create_order();
$order2 = WC_Helper_Order::create_order();
Expand All @@ -355,6 +397,28 @@ public function test_backfill_when_an_order_has_been_deleted() {
$this->assertNotEmpty( get_post_meta( $order2->get_id(), '_billing_email', true ) );
}

public function test_backfill_if_no_orders_were_backfilled() {
$this->toggle_use_custom_table( false );
WC_Helper_Order::create_order();

$this->cli->backfill();

$this->assertEquals( 1, WP_CLI::$__counts['warning'], 'Expected to see a warning if no orders were backfilled.' );
}

public function test_handle_exceptions() {
$exception = new Exception( uniqid() );

try {
WooCommerce_Custom_Orders_Table_CLI::handle_exceptions( $exception );
} catch ( Exception $e ) {
$this->assertSame( $exception, $e );
return;
}

$this->fail( 'Did not receive the expected exception.' );
}

/**
* Retrieve the array of skipped IDs from the CLI instance.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/test-customer-data-store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Tests for the WC_Customer_Data_Store_Custom_Table class.
*
* @package WooCommerce_Custom_Orders_Table
* @author Liquid Web
*/

class CustomerDataStoreTest extends TestCase {

/**
* @testWith ["some-value", "wc-some-value"]
* ["wc-some-value", "wc-some-value"]
*/
public function test_prefix_wc_status( $value, $expected ) {
$this->assertSame(
$expected,
WC_Customer_Data_Store_Custom_Table::prefix_wc_status( $value )
);
}
}
26 changes: 26 additions & 0 deletions tests/test-order-data-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,32 @@ public function test_populate_from_meta_handles_wc_data_exceptions() {
remove_action( 'woocommerce_order_object_updated_props', array( $this, 'throw_wc_data_exception' ) );
}

public function test_backfill_postmeta() {
$order = WC_Helper_Order::create_order();

$order->get_data_store()->backfill_postmeta( $order );

$meta = get_post_meta( $order->get_id() );

$this->assertEmpty(
array_diff(
array_keys( $meta ),
WooCommerce_Custom_Orders_Table::get_postmeta_mapping()
),
'The only post meta the order should have is what was backfilled from the custom table.'
);
}

public function test_backfill_postmeta_does_nothing_if_the_order_row_is_empty() {
$this->toggle_use_custom_table( false );
$order = WC_Helper_Order::create_order();
$this->toggle_use_custom_table( true );

$order = wc_get_order( $order->get_id() );

$this->assertNull( $order->get_data_store()->backfill_postmeta( $order ) );
}

/**
* Hooked method that simply throws a WC_Data_Exception exception.
*
Expand Down

0 comments on commit 8d7f243

Please sign in to comment.