From 4246ac10ecdf320b5bd17902016d9fb0cd0c9b08 Mon Sep 17 00:00:00 2001 From: Paul Kilmurray Date: Fri, 5 Jan 2024 20:45:29 +0100 Subject: [PATCH] fix tests --- tests/Helpers/ProductHelper.php | 30 +++++++++---------- tests/includes/API/Test_Orders_Controller.php | 2 +- .../Test_Product_Variations_Controller.php | 9 +++--- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tests/Helpers/ProductHelper.php b/tests/Helpers/ProductHelper.php index 328eb8f..c923dcb 100644 --- a/tests/Helpers/ProductHelper.php +++ b/tests/Helpers/ProductHelper.php @@ -49,7 +49,7 @@ public static function create_simple_product( $args = array(), $save = true ) { 'name' => 'Dummy Product', 'regular_price' => 10, 'price' => 10, - 'sku' => 'DUMMY SKU', + 'sku' => uniqid( 'DUMMY SKU' ), 'manage_stock' => false, 'tax_status' => 'taxable', 'downloadable' => false, @@ -57,13 +57,13 @@ public static function create_simple_product( $args = array(), $save = true ) { 'stock_status' => 'instock', 'weight' => '1.1', ); - + // Merge default properties with user-provided args $props = wp_parse_args( $args, $defaults ); - + $product = new WC_Product_Simple(); $product->set_props( $props ); - + if ( $save ) { $product->save(); @@ -72,7 +72,7 @@ public static function create_simple_product( $args = array(), $save = true ) { return $product; } - + /** * Create external product. @@ -87,7 +87,7 @@ public static function create_external_product() { array( 'name' => 'Dummy External Product', 'regular_price' => 10, - 'sku' => 'DUMMY EXTERNAL SKU', + 'sku' => uniqid( 'DUMMY EXTERNAL SKU' ), 'product_url' => 'http://woocommerce.com', 'button_text' => 'Buy external product', ) @@ -111,7 +111,7 @@ public static function create_grouped_product() { $product->set_props( array( 'name' => 'Dummy Grouped Product', - 'sku' => 'DUMMY GROUPED SKU', + 'sku' => uniqid( 'DUMMY GROUPED SKU' ), ) ); $product->set_children( array( $simple_product_1->get_id(), $simple_product_2->get_id() ) ); @@ -132,7 +132,7 @@ public static function create_variation_product() { $product->set_props( array( 'name' => 'Dummy Variable Product', - 'sku' => 'DUMMY VARIABLE SKU', + 'sku' => uniqid( 'DUMMY VARIABLE SKU' ), ) ); @@ -154,7 +154,7 @@ public static function create_variation_product() { $variation_1->set_props( array( 'parent_id' => $product->get_id(), - 'sku' => 'DUMMY SKU VARIABLE SMALL', + 'sku' => uniqid( 'DUMMY SKU VARIABLE SMALL' ), 'regular_price' => 10, ) ); @@ -165,7 +165,7 @@ public static function create_variation_product() { $variation_2->set_props( array( 'parent_id' => $product->get_id(), - 'sku' => 'DUMMY SKU VARIABLE LARGE', + 'sku' => uniqid( 'DUMMY SKU VARIABLE LARGE' ), 'regular_price' => 15, ) ); @@ -322,7 +322,7 @@ public static function create_product_review( $product_id, $review_content = 'Re public static function save_post_test_update_meta_data_direct( $id ): void { update_post_meta( $id, '_test2', 'world' ); } - + /** * Create a product category. * @@ -333,7 +333,7 @@ public static function save_post_test_update_meta_data_direct( $id ): void { */ public static function create_product_category( string $name, ?int $parent = null ) { // Create a new product category - $args = $parent ? array('parent' => $parent) : array(); + $args = $parent ? array( 'parent' => $parent ) : array(); return wp_insert_term( $name, 'product_cat', $args ); } @@ -348,7 +348,7 @@ public static function create_product_category( string $name, ?int $parent = nul */ public static function delete_product_category( int $term_id, bool $force = false ) { // Delete the product category - return wp_delete_term( $term_id, 'product_cat', array('force_default' => $force) ); + return wp_delete_term( $term_id, 'product_cat', array( 'force_default' => $force ) ); } /** @@ -361,7 +361,7 @@ public static function delete_product_category( int $term_id, bool $force = fals */ public static function create_product_tag( string $name, ?int $parent = null ) { // Create a new product tag - $args = $parent ? array('parent' => $parent) : array(); + $args = $parent ? array( 'parent' => $parent ) : array(); return wp_insert_term( $name, 'product_tag', $args ); } @@ -376,6 +376,6 @@ public static function create_product_tag( string $name, ?int $parent = null ) { */ public static function delete_product_tag( int $term_id, bool $force = false ) { // Delete the product category - return wp_delete_term( $term_id, 'product_tag', array('force_default' => $force) ); + return wp_delete_term( $term_id, 'product_tag', array( 'force_default' => $force ) ); } } diff --git a/tests/includes/API/Test_Orders_Controller.php b/tests/includes/API/Test_Orders_Controller.php index 657ecfe..3174fa7 100644 --- a/tests/includes/API/Test_Orders_Controller.php +++ b/tests/includes/API/Test_Orders_Controller.php @@ -770,7 +770,7 @@ public function test_order_search_by_billing_first_name_with_excludes() { $response = $this->server->dispatch( $request ); $data = $response->get_data(); $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 0, \count( $data ) ); + $this->assertEquals( 1, \count( $data ) ); $ids = wp_list_pluck( $data, 'id' ); $this->assertEquals( array( $order2->get_id() ), $ids ); diff --git a/tests/includes/API/Test_Product_Variations_Controller.php b/tests/includes/API/Test_Product_Variations_Controller.php index bc04062..1996bb1 100644 --- a/tests/includes/API/Test_Product_Variations_Controller.php +++ b/tests/includes/API/Test_Product_Variations_Controller.php @@ -200,8 +200,7 @@ public function test_variation_response_contains_sku_barcode(): void { $data = $response->get_data(); $this->assertEquals( 200, $response->get_status() ); - - $this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $data['barcode'] ); + $this->assertStringStartsWith( 'DUMMY SKU VARIABLE SMALL', $data['barcode'] ); } public function test_variation_response_contains_barcode(): void { @@ -273,7 +272,8 @@ public function test_variation_orderby_sku(): void { $data = $response->get_data(); $skus = wp_list_pluck( $data, 'sku' ); - $this->assertEquals( $skus, array( 'DUMMY SKU VARIABLE LARGE', 'DUMMY SKU VARIABLE SMALL' ) ); + $this->assertStringStartsWith( 'DUMMY SKU VARIABLE LARGE', $skus[0] ); + $this->assertStringStartsWith( 'DUMMY SKU VARIABLE SMALL', $skus[1] ); // reverse order $request->set_query_params( @@ -286,7 +286,8 @@ public function test_variation_orderby_sku(): void { $data = $response->get_data(); $skus = wp_list_pluck( $data, 'sku' ); - $this->assertEquals( $skus, array( 'DUMMY SKU VARIABLE SMALL', 'DUMMY SKU VARIABLE LARGE' ) ); + $this->assertStringStartsWith( 'DUMMY SKU VARIABLE SMALL', $skus[0] ); + $this->assertStringStartsWith( 'DUMMY SKU VARIABLE LARGE', $skus[1] ); } public function test_variation_orderby_barcode(): void {