Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Update tests to handle exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Feb 19, 2020
1 parent 3678619 commit d8837e8
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions tests/php/RestApi/StoreApi/Utilities/ReserveStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,46 @@ 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.
$order = OrderHelper::create_order( 1, $product );
$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 );
}

/**
Expand Down

0 comments on commit d8837e8

Please sign in to comment.