[Unit Tests] Clean products after tested is complete

This commit is contained in:
Claudio Sanches 2017-11-16 16:56:30 -02:00
parent b32754a035
commit c140331dc3
4 changed files with 50 additions and 8 deletions

View File

@ -75,6 +75,8 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
),
),
), $product_reviews );
$product->delete( true );
}
/**
@ -87,6 +89,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$product = WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews' ) );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
}
/**
@ -124,6 +127,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
'email' => 'woo@woo.local',
'verified' => false,
), $data );
$product->delete( true );
}
/**
@ -137,6 +141,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews/' . $product_review_id ) );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
}
/**
@ -149,6 +154,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$product = WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/reviews/0' ) );
$this->assertEquals( 404, $response->get_status() );
$product->delete( true );
}
/**
@ -180,6 +186,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
'email' => 'woo@woo.local',
'verified' => false,
), $data );
$product->delete( true );
}
/**
@ -223,6 +230,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 400, $response->get_status() );
$product->delete( true );
}
/**
@ -255,6 +263,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'Justin', $data['name'] );
$this->assertEquals( 'woo2@woo.local', $data['email'] );
$this->assertEquals( 3, $data['rating'] );
$product->delete( true );
}
/**
@ -277,6 +286,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
}
/**
@ -298,6 +308,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 404, $response->get_status() );
$product->delete( true );
}
/**
@ -314,6 +325,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$product->delete( true );
}
/**
@ -330,6 +342,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
}
/**
@ -347,6 +360,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
$product->delete( true );
}
/**
@ -394,6 +408,7 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$data = $response->get_data();
$this->assertEquals( 3, count( $data ) );
$product->delete( true );
}
/**
@ -418,5 +433,6 @@ class Product_Reviews extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'email', $properties );
$this->assertArrayHasKey( 'verified', $properties );
$product->delete( true );
}
}

View File

@ -51,6 +51,11 @@ class Products_API extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'DUMMY SKU', $products[0]['sku'] );
$this->assertEquals( 'Dummy External Product', $products[1]['name'] );
$this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] );
foreach ( $products as $key => $value ) {
$product = wc_get_product( $value['id'] );
$product->delete( true );
}
}
/**
@ -63,6 +68,10 @@ class Products_API extends WC_REST_Unit_Test_Case {
WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
$this->assertEquals( 401, $response->get_status() );
$products = $response->get_data();
$product = wc_get_product( $products[0]['id'] );
$product->delete( true );
}
/**
@ -85,6 +94,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
'sku' => 'DUMMY EXTERNAL SKU',
'regular_price' => 10,
), $product );
$simple->delete( true );
}
/**
@ -97,6 +107,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
$product = WC_Helper_Product::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
}
/**
@ -116,6 +127,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
$variations = $response->get_data();
$this->assertEquals( 0, count( $variations ) );
$product->delete( true );
}
/**
@ -130,6 +142,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
$product->delete( true );
}
/**
@ -179,6 +192,7 @@ class Products_API extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'FIXED-SKU', $data['sku'] );
$this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] );
$this->assertContains( 'test upload image', $data['images'][0]['alt'] );
$product->delete( true );
// test variable product (variations are tested in product-variations.php)
$product = WC_Helper_Product::create_variation_product();
@ -463,6 +477,11 @@ class Products_API extends WC_REST_Unit_Test_Case {
$products = $response->get_data();
$this->assertEquals( 8, count( $products ) );
foreach ( $products as $key => $value ) {
$product = wc_get_product( $value['id'] );
$product->delete( true );
}
}
/**

View File

@ -137,6 +137,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
$discounts = new WC_Discounts();
$discounts->set_items_from_cart( false );
$this->assertEquals( array(), $discounts->get_items() );
$product->delete( true );
}
/**
@ -173,6 +174,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
$discounts->set_items_from_cart( WC()->cart );
$discounts->apply_coupon( $coupon );
$this->assertEquals( 0, $discounts->get_discounted_price( current( $discounts->get_items() ) ), print_r( $discounts->get_discounts(), true ) );
$product->delete( true );
}
/**
@ -185,6 +187,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
public function test_calculations( $test_data ) {
$this->last_test_data = $test_data;
$discounts = new WC_Discounts();
$products = array();
if ( isset( $test_data['tax_rate'] ) ) {
WC_Tax::_insert_tax_rate( $test_data['tax_rate'] );
@ -196,13 +199,13 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
}
}
foreach ( $test_data['cart'] as $item ) {
$product = WC_Helper_Product::create_simple_product();
$product->set_regular_price( $item['price'] );
$product->set_tax_status( 'taxable' );
$product->save();
$this->store_product( $product );
WC()->cart->add_to_cart( $product->get_id(), $item['qty'] );
foreach ( $test_data['cart'] as $key => $item ) {
$products[ $key ] = WC_Helper_Product::create_simple_product();
$products[ $key ]->set_regular_price( $item['price'] );
$products[ $key ]->set_tax_status( 'taxable' );
$products[ $key ]->save();
$this->store_product( $products[ $key ] );
WC()->cart->add_to_cart( $products[ $key ]->get_id(), $item['qty'] );
}
$discounts->set_items_from_cart( WC()->cart );
@ -222,6 +225,10 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
}
$this->assertEquals( $test_data['expected_total_discount'], $discount_total, 'Failed (' . print_r( $test_data, true ) . ' - ' . print_r( $discounts->get_discounts(), true ) . ')' );
foreach ( $products as $product ) {
$products->delete( true );
}
}
/**

View File

@ -11,7 +11,7 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
*
* @var string
*/
protected $csv_file = string;
protected $csv_file = '';
/**
* Load up the importer classes since they aren't loaded by default.