2020-01-10 13:53:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Utility Tests.
|
|
|
|
*/
|
|
|
|
|
2020-04-22 14:39:19 +00:00
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Utilities;
|
2020-01-10 13:53:16 +00:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use \WC_Helper_Order as OrderHelper;
|
|
|
|
use \WC_Helper_Product as ProductHelper;
|
2020-04-22 14:39:19 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\ReserveStock;
|
2020-01-10 13:53:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ReserveStock Utility Tests.
|
|
|
|
*/
|
|
|
|
class ReserveStockTests extends TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that stock is reserved for draft orders.
|
|
|
|
*/
|
|
|
|
public function test_reserve_stock_for_order() {
|
|
|
|
$class = new ReserveStock();
|
|
|
|
|
|
|
|
$product = ProductHelper::create_simple_product();
|
|
|
|
$product->set_manage_stock( true );
|
2020-08-12 14:54:02 +00:00
|
|
|
// this also saves product
|
|
|
|
wc_update_product_stock( $product, 10 );
|
2020-01-10 13:53:16 +00:00
|
|
|
|
|
|
|
$order = OrderHelper::create_order( 1, $product ); // Note this adds 4 to the order.
|
|
|
|
$order->set_status( 'checkout-draft' );
|
|
|
|
$order->save();
|
|
|
|
|
2020-03-06 11:44:20 +00:00
|
|
|
$class->reserve_stock_for_order( $order );
|
2020-01-10 13:53:16 +00:00
|
|
|
$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();
|
|
|
|
|
2020-03-06 11:44:20 +00:00
|
|
|
$class->reserve_stock_for_order( $order );
|
2020-01-10 13:53:16 +00:00
|
|
|
$this->assertEquals( 8, $this->get_reserved_stock_by_product_id( $product->get_stock_managed_by_id() ) );
|
2020-03-06 11:44:20 +00:00
|
|
|
}
|
2020-01-10 13:53:16 +00:00
|
|
|
|
2020-03-06 11:44:20 +00:00
|
|
|
/**
|
|
|
|
* Test that trying to reserve stock too much throws an exception.
|
|
|
|
*
|
2020-04-22 14:39:19 +00:00
|
|
|
* @expectedException Automattic\WooCommerce\Blocks\StoreApi\Utilities\ReserveStockException
|
2020-03-06 11:44:20 +00:00
|
|
|
*/
|
|
|
|
public function test_reserve_stock_for_order_throws_exception() {
|
|
|
|
$class = new ReserveStock();
|
|
|
|
|
|
|
|
$product = ProductHelper::create_simple_product();
|
|
|
|
$product->set_manage_stock( true );
|
2020-08-12 14:54:02 +00:00
|
|
|
// this also saves product
|
|
|
|
wc_update_product_stock( $product, 10 );
|
2020-03-06 11:44:20 +00:00
|
|
|
|
|
|
|
$order = OrderHelper::create_order( 1, $product ); // Note this adds 4 to the order.
|
2020-01-10 13:53:16 +00:00
|
|
|
$order->set_status( 'checkout-draft' );
|
|
|
|
$order->save();
|
|
|
|
|
2020-03-06 11:44:20 +00:00
|
|
|
$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 );
|
2020-01-10 13:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to get the count of reserved stock.
|
|
|
|
*
|
|
|
|
* @param integer $product_id
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
protected function get_reserved_stock_by_product_id( $product_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
return $wpdb->get_var(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT SUM( stock_table.`stock_quantity` ) FROM $wpdb->wc_reserved_stock stock_table WHERE stock_table.`product_id` = %d",
|
|
|
|
$product_id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|