From c916037f10e3ad39ad99334bbc302fb94578f525 Mon Sep 17 00:00:00 2001 From: Nicola Mustone Date: Wed, 4 Mar 2015 09:00:33 +0100 Subject: [PATCH 1/3] test wc-product-functions.php --- tests/unit-tests/product-functions.php | 36 ++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/unit-tests/product-functions.php diff --git a/tests/unit-tests/product-functions.php b/tests/unit-tests/product-functions.php new file mode 100644 index 00000000000..165fd3a2e1a --- /dev/null +++ b/tests/unit-tests/product-functions.php @@ -0,0 +1,36 @@ +id ); + + $this->assertEquals( $product->id, $product_copy->id ); + } + + /** + * Test wc_update_product_stock() + * + * @since 2.3.0 + */ + public function test_wc_update_product_stock() { + $product = WC_Helper_Product::create_simple_product(); + + update_post_meta( $product->id, '_manage_stock', 'yes' ); + + wc_update_product_stock( $product->id, 5 ); + $this->assertEquals( 5, $product->stock ); + + update_post_meta( $product, '_manage_stock', 'no' ); + } +} From 72c2d3747847a478816389b8c0e13a43299e002c Mon Sep 17 00:00:00 2001 From: Nicola Mustone Date: Wed, 4 Mar 2015 09:25:15 +0100 Subject: [PATCH 2/3] restore _manage_stock properly --- tests/unit-tests/product-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-tests/product-functions.php b/tests/unit-tests/product-functions.php index 165fd3a2e1a..1031a456f3e 100644 --- a/tests/unit-tests/product-functions.php +++ b/tests/unit-tests/product-functions.php @@ -31,6 +31,6 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case { wc_update_product_stock( $product->id, 5 ); $this->assertEquals( 5, $product->stock ); - update_post_meta( $product, '_manage_stock', 'no' ); + update_post_meta( $product->id, '_manage_stock', 'no' ); } } From 3615e75de1a76cfd7998892c23f9d1a87b213243 Mon Sep 17 00:00:00 2001 From: Nicola Mustone Date: Wed, 4 Mar 2015 09:33:05 +0100 Subject: [PATCH 3/3] helper methods to get and delete a dummy product --- tests/unit-tests/product-functions.php | 45 +++++++++++++++++++++----- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/unit-tests/product-functions.php b/tests/unit-tests/product-functions.php index 1031a456f3e..702cbe00f22 100644 --- a/tests/unit-tests/product-functions.php +++ b/tests/unit-tests/product-functions.php @@ -5,6 +5,33 @@ * @since 2.3.0 */ class WC_Tests_Product_Functions extends WC_Unit_Test_Case { + /** + * @var object + * @access private + */ + private $_product = null; + + /** + * Helper method to get a product + * + * @since 2.3.0 + * @access private + */ + private function _get_product() { + $this->_product = WC_Helper_Product::create_simple_product(); + } + + /** + * Helper method to delete a product + * + * @since 2.3.0 + * @access private + */ + private function _delete_product() { + // Delete the previously created product + WC_Helper_Product::delete_product( $this->_product->id ); + $this->_product = null; + } /** * Test wc_get_product() @@ -12,10 +39,12 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case { * @since 2.3.0 */ public function test_wc_get_product() { - $product = WC_Helper_Product::create_simple_product(); - $product_copy = wc_get_product( $product->id ); + $this->_get_product(); + $product_copy = wc_get_product( $this->_product->id ); - $this->assertEquals( $product->id, $product_copy->id ); + $this->assertEquals( $this->_product->id, $product_copy->id ); + + $this->_delete_product(); } /** @@ -24,13 +53,13 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case { * @since 2.3.0 */ public function test_wc_update_product_stock() { - $product = WC_Helper_Product::create_simple_product(); + $this->_get_product(); - update_post_meta( $product->id, '_manage_stock', 'yes' ); + update_post_meta( $this->_product->id, '_manage_stock', 'yes' ); - wc_update_product_stock( $product->id, 5 ); - $this->assertEquals( 5, $product->stock ); + wc_update_product_stock( $this->_product->id, 5 ); + $this->assertEquals( 5, $this->_product->stock ); - update_post_meta( $product->id, '_manage_stock', 'no' ); + $this->_delete_product(); } }