From 2c884c000332b0ed80bd377130800154db6c32fd Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Mon, 18 Dec 2017 14:45:39 -0800 Subject: [PATCH 1/4] Progress on order item tests --- .../order-items/order-item-product.php | 103 +++++++++++++++--- 1 file changed, 89 insertions(+), 14 deletions(-) diff --git a/tests/unit-tests/order-items/order-item-product.php b/tests/unit-tests/order-items/order-item-product.php index c3627bf4a00..2d6eed10255 100644 --- a/tests/unit-tests/order-items/order-item-product.php +++ b/tests/unit-tests/order-items/order-item-product.php @@ -13,13 +13,13 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { * @since 3.2.0 */ public function test_generic_setters_getters() { - $simple_product = new WC_Product_Simple; + $simple_product = new WC_Product_Simple(); $simple_product->save(); - $variation_product = new WC_Product_Variation; + $variation_product = new WC_Product_Variation(); $variation_product->save(); - $product_item = new WC_Order_Item_Product; + $product_item = new WC_Order_Item_Product(); $product_item->set_quantity( 3 ); $this->assertEquals( 3, $product_item->get_quantity() ); @@ -52,7 +52,7 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { * @since 3.2.0 */ public function test_set_get_taxes() { - $product_item = new WC_Order_Item_Product; + $product_item = new WC_Order_Item_Product(); $taxes = array( 'total' => array( '10', '2.4' ), @@ -70,22 +70,23 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { * @since 3.2.0 */ public function test_set_get_product() { - $simple_product = new WC_Product_Simple; + $simple_product = new WC_Product_Simple(); $simple_product->set_name( 'Test Simple' ); $simple_product->set_tax_class( 'reduced-rate' ); $simple_product->save(); - $parent_product = new WC_Product_Variable; + $parent_product = new WC_Product_Variable(); $parent_product->set_name( 'Test Parent' ); $parent_product->save(); - $variation_product = new WC_Product_Variation; + $variation_product = new WC_Product_Variation(); $variation_product->set_name( 'Test Variation' ); $variation_product->set_parent_id( $parent_product->get_id() ); + $variation_product->set_attributes( array( 'color' => 'Green' ) ); $variation_product->save(); // Simple product. - $product_item = new WC_Order_Item_Product; + $product_item = new WC_Order_Item_Product(); $product_item->set_product( $simple_product ); $this->assertEquals( 'Test Simple', $product_item->get_name() ); $this->assertEquals( $simple_product->get_id(), $product_item->get_product_id() ); @@ -96,13 +97,13 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { $this->assertEquals( $simple_product->get_id(), $retrieved->get_id() ); // Variation product. - $product_item = new WC_Order_Item_Product; + $product_item = new WC_Order_Item_Product(); $product_item->set_product( $variation_product ); - $this->assertEquals( 'Test Parent', $product_item->get_name() ); + $this->assertEquals( 'Test Parent - Green', $product_item->get_name() ); $this->assertEquals( $parent_product->get_id(), $product_item->get_product_id() ); $this->assertEquals( $variation_product->get_id(), $product_item->get_variation_id() ); $this->assertEquals( '', $product_item->get_tax_class() ); - + $this->assertEquals( 'Green', $product_item->get_meta( 'color' ) ); $retrieved = $product_item->get_product(); $this->assertEquals( $variation_product->get_id(), $retrieved->get_id() ); } @@ -113,18 +114,92 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { * @since 3.2.0 */ public function test_get_item_download_url() { - $product = new WC_Product_Simple; + $product = new WC_Product_Simple(); $product->save(); - $order = new WC_Order; + $order = new WC_Order(); $order->set_billing_email( 'test@woocommerce.com' ); $order->save(); - $product_item = new WC_Order_Item_Product; + $product_item = new WC_Order_Item_Product(); $product_item->set_product( $product ); $product_item->set_order_id( $order->get_id() ); $expected_regex = '/download_file=.*&order=wc_order_.*&email=test%40woocommerce.com&key=100/'; $this->assertRegexp( $expected_regex, $product_item->get_item_download_url( 100 ) ); } + + public function test_get_formatted_meta_data() { + + $parent_product = new WC_Product_Variable(); + $parent_product->set_name( 'Test Parent' ); + $parent_product->save(); + + $variation_product = new WC_Product_Variation(); + $variation_product->set_name( 'Test Variation' ); + $variation_product->set_parent_id( $parent_product->get_id() ); + $variation_product->set_attributes( array( 'color' => 'Green', 'size' => 'Large' ) ); + $variation_product->save(); + + $product_item = new WC_Order_Item_Product(); + $product_item->set_product( $variation_product ); + $product_item->add_meta_data( 'testkey', 'testval', true ); + $product_item->save(); + + // Test with show_all on. + $formatted = $product_item->get_formatted_meta_data( '_', true ); + $formatted_as_array = array(); + foreach ( $formatted as $f ) { + $formatted_as_array[] = (array) $f; + } + $this->assertEquals( + array( + array( + "key" => "color", + "value" => "Green", + "display_key" => "color", + "display_value" => "

Green

\n", + ), + array( + "key" => "size", + "value" => "Large", + "display_key" => "size", + "display_value" => "

Large

\n", + ), + array( + "key" => "testkey", + "value" => "testval", + "display_key" => "testkey", + "display_value" => "

testval

\n", + ), + ), + $formatted_as_array + ); + + // Test with show_all off. + $formatted = $product_item->get_formatted_meta_data( '_', false ); + $formatted_as_array = array(); + foreach ( $formatted as $f ) { + $formatted_as_array[] = (array) $f; + } + $this->assertEquals( + array( + array( + "key" => "testkey", + "value" => "testval", + "display_key" => "testkey", + "display_value" => "

testval

\n", + ), + ), + $formatted_as_array + ); + + // Test with an exclude prefix. Should exclude everything since they're either in the title or in the exclude prefix. + $formatted = $product_item->get_formatted_meta_data( 'test', false ); + $this->assertEmpty( $formatted ); + } + + public function test_offset_set_get_exists() { + + } } From 3461fd2a3a218630276c3264864367526b4e66c7 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Tue, 19 Dec 2017 09:22:17 -0800 Subject: [PATCH 2/4] Array access tests --- .../order-items/order-item-product.php | 76 ++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/tests/unit-tests/order-items/order-item-product.php b/tests/unit-tests/order-items/order-item-product.php index 2d6eed10255..a7f1e938916 100644 --- a/tests/unit-tests/order-items/order-item-product.php +++ b/tests/unit-tests/order-items/order-item-product.php @@ -129,8 +129,12 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { $this->assertRegexp( $expected_regex, $product_item->get_item_download_url( 100 ) ); } + /** + * Test the get_formatted_meta_data method. + * + * @since 3.3.0 + */ public function test_get_formatted_meta_data() { - $parent_product = new WC_Product_Variable(); $parent_product->set_name( 'Test Parent' ); $parent_product->save(); @@ -199,7 +203,75 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { $this->assertEmpty( $formatted ); } - public function test_offset_set_get_exists() { + /** + * Test the Array Access methods. + * + * @since 3.3.0 + */ + public function test_arrayaccess() { + $item = new WC_Order_Item_Product(); + // Test line_subtotal. + $this->assertTrue( isset( $item['line_subtotal'] ) ); + $item['line_subtotal'] = 50; + $this->assertEquals( 50, $item->get_subtotal() ); + $this->assertEquals( $item->get_subtotal(), $item['line_subtotal'] ); + + // Test line_subtotal_tax. + $this->assertTrue( isset( $item['line_subtotal_tax'] ) ); + $item['line_subtotal_tax'] = 5; + $this->assertEquals( 5, $item->get_subtotal_tax() ); + $this->assertEquals( $item->get_subtotal_tax(), $item['line_subtotal_tax'] ); + + // Test line_total. + $this->assertTrue( isset( $item['line_total'] ) ); + $item['line_total'] = 55; + $this->assertEquals( 55, $item->get_total() ); + $this->assertEquals( $item->get_total(), $item['line_total'] ); + + // Test line_tax. + $this->assertTrue( isset( $item['line_tax'] ) ); + $item['line_tax'] = 5; + $this->assertEquals( 5, $item->get_total_tax() ); + $this->assertEquals( $item->get_total_tax(), $item['line_tax'] ); + + // Test line_tax_data. + $this->assertTrue( isset( $item['line_tax_data'] ) ); + $item['line_tax_data'] = array( + 'total' => array( 5 ), + 'subtotal' => array( 5 ), + ); + $this->assertEquals( + array( + 'total' => array( 5 ), + 'subtotal' => array( 5 ), + ), + $item->get_taxes() + ); + $this->assertEquals( $item->get_taxes(), $item['line_tax_data'] ); + + // Test qty. + $this->assertTrue( isset( $item['qty'] ) ); + $item['qty'] = 150; + $this->assertEquals( 150, $item->get_quantity() ); + $this->assertEquals( $item->get_quantity(), $item['qty'] ); + + // Test item_meta_array. + $this->assertTrue( isset( $item['item_meta_array'] ) ); + $item['item_meta_array'] = array( + 9999 => (object) array( + 'key' => 'test', + 'value' => 'val' + ), + ); + $this->assertInstanceOf( 'WC_Meta_Data', current( $item->get_meta_data() ) ); + $this->assertEquals( current( $item->get_meta_data() ), current( $item['item_meta_array'] ) ); + unset( $item['item_meta_array'] ); + $this->assertEquals( array(), $item->get_meta_data() ); + + // Test default. + $this->assertFalse( isset( $item['foo'] ) ); + $item['foo'] = 'bar'; + $this->assertEquals( 'bar', $item->get_meta( 'foo' ) ); } } From cff662ac479cea42bd8c43fa56eec4077a923d54 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Tue, 19 Dec 2017 09:30:02 -0800 Subject: [PATCH 3/4] phpcs --- ... => class-wc-tests-order-item-product.php} | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) rename tests/unit-tests/order-items/{order-item-product.php => class-wc-tests-order-item-product.php} (91%) diff --git a/tests/unit-tests/order-items/order-item-product.php b/tests/unit-tests/order-items/class-wc-tests-order-item-product.php similarity index 91% rename from tests/unit-tests/order-items/order-item-product.php rename to tests/unit-tests/order-items/class-wc-tests-order-item-product.php index a7f1e938916..cec9c90da50 100644 --- a/tests/unit-tests/order-items/order-item-product.php +++ b/tests/unit-tests/order-items/class-wc-tests-order-item-product.php @@ -1,10 +1,14 @@ set_name( 'Test Variation' ); $variation_product->set_parent_id( $parent_product->get_id() ); - $variation_product->set_attributes( array( 'color' => 'Green', 'size' => 'Large' ) ); + $variation_product->set_attributes( array( + 'color' => 'Green', + 'size' => 'Large', + ) ); $variation_product->save(); $product_item = new WC_Order_Item_Product(); @@ -159,22 +166,22 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { $this->assertEquals( array( array( - "key" => "color", - "value" => "Green", - "display_key" => "color", - "display_value" => "

Green

\n", + 'key' => 'color', + 'value' => 'Green', + 'display_key' => 'color', + 'display_value' => "

Green

\n", ), array( - "key" => "size", - "value" => "Large", - "display_key" => "size", - "display_value" => "

Large

\n", + 'key' => 'size', + 'value' => 'Large', + 'display_key' => 'size', + 'display_value' => "

Large

\n", ), array( - "key" => "testkey", - "value" => "testval", - "display_key" => "testkey", - "display_value" => "

testval

\n", + 'key' => 'testkey', + 'value' => 'testval', + 'display_key' => 'testkey', + 'display_value' => "

testval

\n", ), ), $formatted_as_array @@ -189,10 +196,10 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { $this->assertEquals( array( array( - "key" => "testkey", - "value" => "testval", - "display_key" => "testkey", - "display_value" => "

testval

\n", + 'key' => 'testkey', + 'value' => 'testval', + 'display_key' => 'testkey', + 'display_value' => "

testval

\n", ), ), $formatted_as_array @@ -261,7 +268,7 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { $item['item_meta_array'] = array( 9999 => (object) array( 'key' => 'test', - 'value' => 'val' + 'value' => 'val', ), ); $this->assertInstanceOf( 'WC_Meta_Data', current( $item->get_meta_data() ) ); From b7c659ca07da949c668c9cfa4d2536c3d926dcf1 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Tue, 19 Dec 2017 11:37:37 -0800 Subject: [PATCH 4/4] Fix php <=5.6 errors --- .../order-items/class-wc-tests-order-item-product.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit-tests/order-items/class-wc-tests-order-item-product.php b/tests/unit-tests/order-items/class-wc-tests-order-item-product.php index cec9c90da50..47501f98640 100644 --- a/tests/unit-tests/order-items/class-wc-tests-order-item-product.php +++ b/tests/unit-tests/order-items/class-wc-tests-order-item-product.php @@ -266,13 +266,13 @@ class WC_Tests_Order_Item_Product extends WC_Unit_Test_Case { // Test item_meta_array. $this->assertTrue( isset( $item['item_meta_array'] ) ); $item['item_meta_array'] = array( - 9999 => (object) array( + 0 => (object) array( 'key' => 'test', 'value' => 'val', ), ); $this->assertInstanceOf( 'WC_Meta_Data', current( $item->get_meta_data() ) ); - $this->assertEquals( current( $item->get_meta_data() ), current( $item['item_meta_array'] ) ); + $this->assertEquals( current( $item->get_meta_data() ), $item['item_meta_array'][''] ); unset( $item['item_meta_array'] ); $this->assertEquals( array(), $item->get_meta_data() );