From c612f85c6cb22c9b74a7f8ec3ded9d4196074152 Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Tue, 11 Dec 2018 12:39:44 +0100 Subject: [PATCH 1/3] PHPCS updates. --- .../tests/api/reports-products-stats.php | 4 ++ .../reports/class-wc-tests-reports-orders.php | 42 ++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/plugins/woocommerce-admin/tests/api/reports-products-stats.php b/plugins/woocommerce-admin/tests/api/reports-products-stats.php index 0443a751a8c..224ac60786e 100644 --- a/plugins/woocommerce-admin/tests/api/reports-products-stats.php +++ b/plugins/woocommerce-admin/tests/api/reports-products-stats.php @@ -5,6 +5,10 @@ * @package WooCommerce\Tests\API * @since 3.5.0 */ + +/** + * Class WC_Tests_API_Reports_Products_Stats + */ class WC_Tests_API_Reports_Products_Stats extends WC_REST_Unit_Test_Case { /** diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php index bf358381dea..ef0e8a88340 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php @@ -1,10 +1,12 @@ assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'Product includes, negative filter for 2 product: ' . $wpdb->last_query ); // Combinations: match all - // status_is + product_includes + // status_is + product_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -1630,7 +1632,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is + coupon_includes + // status_is + coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -1706,7 +1708,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // product_includes + coupon_includes + // product_includes + coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -1778,7 +1780,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is + product_includes + coupon_includes + // status_is + product_includes + coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -1853,7 +1855,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is + status_is_not + product_includes + // status_is + status_is_not + product_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -1932,7 +1934,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is + status_is_not + product_includes + product_excludes + // status_is + status_is_not + product_includes + product_excludes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2012,7 +2014,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is + status_is_not + product_includes + product_excludes + coupon_includes + // status_is + status_is_not + product_includes + product_excludes + coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2094,7 +2096,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is + status_is_not + product_includes + product_excludes + coupon_includes + coupon_excludes + // status_is + status_is_not + product_includes + product_excludes + coupon_includes + coupon_excludes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2181,7 +2183,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); // Combinations: match any - // status_is + status_is_not, all orders + // status_is + status_is_not, all orders. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2258,7 +2260,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR product_includes + // status_is OR product_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2335,7 +2337,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR coupon_includes + // status_is OR coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2412,7 +2414,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR coupon_excludes + // status_is OR coupon_excludes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2489,7 +2491,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // product_includes OR coupon_includes + // product_includes OR coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2566,7 +2568,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR product_includes OR coupon_includes + // status_is OR product_includes OR coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2646,7 +2648,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR status_is_not OR product_includes + // status_is OR status_is_not OR product_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2729,7 +2731,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR status_is_not OR product_includes OR product_excludes + // status_is OR status_is_not OR product_includes OR product_excludes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2812,7 +2814,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes + // status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), @@ -2898,7 +2900,7 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $query_args ) ), true ), 'No filters' ); - // status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes OR coupon_excludes + // status_is OR status_is_not OR product_includes OR product_excludes OR coupon_includes OR coupon_excludes. $query_args = array( 'after' => $current_hour->format( WC_Admin_Reports_Interval::$sql_datetime_format ), 'before' => $now->format( WC_Admin_Reports_Interval::$sql_datetime_format ), From deba4a354a5c6e437c97bd0f4caf896649512dea Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Tue, 11 Dec 2018 12:41:35 +0100 Subject: [PATCH 2/3] PHPCS updates. --- .../class-wc-tests-reports-revenue-stats.php | 117 +----------------- 1 file changed, 4 insertions(+), 113 deletions(-) diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php index f01f5d95911..6344ac06302 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php @@ -1,11 +1,14 @@ assertEquals( $expected_stats, json_decode( json_encode( $query->get_data() ), true ) ); } - - /** - * Test the calculations and querying works correctly for the case of multiple orders. - */ - /*public function test_populate_and_query_multiple_intervals() { - // Populate all of the data. - $product1 = new WC_Product_Simple(); - $product1->set_name( 'Test Product' ); - $product1->set_regular_price( 25 ); - $product1->save(); - - $product2 = new WC_Product_Simple(); - $product2->set_name( 'Test Product 2' ); - $product2->set_regular_price( 10 ); - $product2->save(); - - $order1_time = time() - ( 2 * HOUR_IN_SECONDS ); - - $order1 = WC_Helper_Order::create_order( 1, $product1 ); - $order1->set_date_created( $order1_time ); - $order1->set_status( 'completed' ); - $order1->set_shipping_total( 10 ); - $order1->set_discount_total( 20 ); - $order1->set_discount_tax( 0 ); - $order1->set_cart_tax( 5 ); - $order1->set_shipping_tax( 2 ); - $order1->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax. - $order1->save(); - - $order2_time = time() - HOUR_IN_SECONDS + 1; - - $order2 = WC_Helper_Order::create_order( 1, $product2 ); - $order2->set_date_created( $order2_time ); - $order2->set_status( 'processing' ); - $order2->set_shipping_total( 5 ); - $order2->set_discount_total( 0 ); - $order2->set_discount_tax( 0 ); - $order2->set_cart_tax( 3 ); - $order2->set_shipping_tax( 1 ); - $order2->set_total( 49 ); // $10x4 products + $5 shipping + $4 tax. - $order2->save(); - - // Test the calculations. - $start_time = $order1_time; - $end_time = $order2_time + HOUR_IN_SECONDS; - - // Test aggregate raw summary data for both orders. - $data = WC_Order_Stats::summarize_orders( $start_time, $end_time ); - $expected_data = array( - 'num_orders' => 2, - 'num_items_sold' => 8, - 'orders_gross_total' => 146.0, - 'orders_coupon_total' => 20.0, - 'orders_refund_total' => 0, - 'orders_tax_total' => 11.0, - 'orders_shipping_total' => 15.0, - 'orders_net_total' => 120.0, - ); - $this->assertEquals( $expected_data, $data ); - - // Calculate stats for each hour and save to DB. - $data = WC_Order_Stats::summarize_orders( $order1_time, $order1_time + HOUR_IN_SECONDS ); - WC_Order_Stats::update( $order1_time, $data ); - $data = WC_Order_Stats::summarize_orders( $order2_time, $order2_time + HOUR_IN_SECONDS ); - WC_Order_Stats::update( $order2_time, $data ); - - // Test querying by hourly intervals. - $stats = WC_Order_Stats::query( $start_time, $end_time ); - $first_hour_stats = $stats[0]; - $expected_first_hour_stats = array( - 'start_time' => date( 'Y-m-d H:00:00', $order1_time ), - 'num_orders' => 1, - 'num_items_sold' => 4, - 'orders_gross_total' => 97, - 'orders_coupon_total' => 20, - 'orders_refund_total' => 0, - 'orders_tax_total' => 7, - 'orders_shipping_total' => 10, - 'orders_net_total' => 80, - ); - $this->assertEquals( $expected_first_hour_stats, $first_hour_stats ); - - $second_hour_stats = $stats[1]; - $expected_second_hour_stats = array( - 'start_time' => date( 'Y-m-d H:00:00', $order2_time ), - 'num_orders' => 1, - 'num_items_sold' => 4, - 'orders_gross_total' => 49, - 'orders_coupon_total' => 0, - 'orders_refund_total' => 0, - 'orders_tax_total' => 4, - 'orders_shipping_total' => 5, - 'orders_net_total' => 40, - ); - $this->assertEquals( $expected_second_hour_stats, $second_hour_stats ); - - // Test querying by a weekly interval. - $stats = WC_Order_Stats::query( $start_time, $end_time, array( 'interval' => 'week' ) ); - $first_week_stats = $stats[0]; - $expected_first_week_stats = array( - 'start_time' => date( 'Y-m-d H:00:00', $order1_time ), - 'num_orders' => '2', - 'num_items_sold' => '8', - 'orders_gross_total' => '146', - 'orders_coupon_total' => '20', - 'orders_refund_total' => '0', - 'orders_tax_total' => '11', - 'orders_shipping_total' => '15', - 'orders_net_total' => '120', - ); - $this->assertEquals( $expected_first_week_stats, $first_week_stats ); - }*/ } From 256a7fd708b709c863e05c5752d4e87a70bca71f Mon Sep 17 00:00:00 2001 From: Peter Fabian Date: Tue, 11 Dec 2018 12:41:47 +0100 Subject: [PATCH 3/3] Test should run without explicitly calling update for lookups. Same as code in general, tests should also check whether the lookups are updated correctly without explicitly needing to call update on data stores. --- plugins/woocommerce-admin/tests/api/reports-products-stats.php | 2 -- .../tests/reports/class-wc-tests-reports-orders.php | 1 - .../tests/reports/class-wc-tests-reports-revenue-stats.php | 1 - 3 files changed, 4 deletions(-) diff --git a/plugins/woocommerce-admin/tests/api/reports-products-stats.php b/plugins/woocommerce-admin/tests/api/reports-products-stats.php index 224ac60786e..9aadd3196b1 100644 --- a/plugins/woocommerce-admin/tests/api/reports-products-stats.php +++ b/plugins/woocommerce-admin/tests/api/reports-products-stats.php @@ -71,8 +71,6 @@ class WC_Tests_API_Reports_Products_Stats extends WC_REST_Unit_Test_Case { $order->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax. $order->save(); - WC_Admin_Reports_Orders_Data_Store::update( $order ); - $request = new WP_REST_Request( 'GET', $this->endpoint ); $request->set_query_params( array( diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php index ef0e8a88340..9376aa0292d 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-orders.php @@ -42,7 +42,6 @@ class WC_Tests_Reports_Orders extends WC_Unit_Test_Case { ); $data_store = new WC_Admin_Reports_Orders_Data_Store(); - $data_store::update( $order ); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() ); diff --git a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php index 6344ac06302..8e291981b9a 100644 --- a/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php +++ b/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-revenue-stats.php @@ -39,7 +39,6 @@ class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case { // /reports/revenue/stats is mapped to Orders_Data_Store. $data_store = new WC_Admin_Reports_Orders_Data_Store(); - $data_store::update( $order ); $start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() ); $end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() );