From d8837e801a23354a35f0ca5cfb8b9ffe803cf8f8 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 19 Feb 2020 14:20:06 +0000 Subject: [PATCH] Update tests to handle exception --- .../StoreApi/Utilities/ReserveStock.php | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/tests/php/RestApi/StoreApi/Utilities/ReserveStock.php b/tests/php/RestApi/StoreApi/Utilities/ReserveStock.php index 387252d46b3..6a097a3db4e 100644 --- a/tests/php/RestApi/StoreApi/Utilities/ReserveStock.php +++ b/tests/php/RestApi/StoreApi/Utilities/ReserveStock.php @@ -32,8 +32,7 @@ public function test_reserve_stock_for_order() { $order->set_status( 'checkout-draft' ); $order->save(); - $result = $class->reserve_stock_for_order( $order ); - $this->assertTrue( $result ); + $class->reserve_stock_for_order( $order ); $this->assertEquals( 4, $this->get_reserved_stock_by_product_id( $product->get_stock_managed_by_id() ) ); // Repeat. @@ -41,17 +40,38 @@ public function test_reserve_stock_for_order() { $order->set_status( 'checkout-draft' ); $order->save(); - $result = $class->reserve_stock_for_order( $order ); - $this->assertTrue( $result ); + $class->reserve_stock_for_order( $order ); $this->assertEquals( 8, $this->get_reserved_stock_by_product_id( $product->get_stock_managed_by_id() ) ); + } - // Repeat again - should not be enough stock for this. - $order = OrderHelper::create_order( 1, $product ); + /** + * Test that stock is reserved for draft orders. + * + * @expectedException Automattic\WooCommerce\Blocks\RestApi\StoreApi\Utilities\ReserveStockException + */ + public function test_reserve_stock_for_order_throws_exception() { + $class = new ReserveStock(); + + $product = ProductHelper::create_simple_product(); + $product->set_manage_stock( true ); + $product->set_stock( 10 ); + $product->save(); + + $order = OrderHelper::create_order( 1, $product ); // Note this adds 4 to the order. $order->set_status( 'checkout-draft' ); $order->save(); - $result = $class->reserve_stock_for_order( $order ); - $this->assertTrue( is_wp_error( $result ) ); + $order2 = OrderHelper::create_order( 1, $product ); + $order2->set_status( 'checkout-draft' ); + $order2->save(); + + $order3 = OrderHelper::create_order( 1, $product ); + $order3->set_status( 'checkout-draft' ); + $order3->save(); + + $class->reserve_stock_for_order( $order ); + $class->reserve_stock_for_order( $order2 ); + $class->reserve_stock_for_order( $order3 ); } /**