Tests pass

This commit is contained in:
Mike Jolley 2017-07-25 17:25:06 +01:00
parent 4960bf0ca4
commit b68adf748b
4 changed files with 38 additions and 10 deletions

View File

@ -78,10 +78,11 @@ class WC_Discounts {
* Get all discount totals with precision.
*
* @since 3.2.0
* @param bool $in_cents Should the totals be returned in cents, or without precision.
* @return array
*/
public function get_discounts() {
return $this->discounts;
public function get_discounts( $in_cents = false ) {
return $in_cents ? $this->discounts : array_map( array( $this, 'remove_precision' ), $this->discounts );
}
/**
@ -173,13 +174,39 @@ class WC_Discounts {
}
/**
* Remove precision from a price.
* Add precision (deep) to a price.
*
* @param int $value Value to remove precision from.
* @since 3.2.0
* @param int|array $value Value to remove precision from.
* @return float
*/
protected function add_precision( $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $key => $subvalue ) {
$value[ $key ] = $this->add_precision( $subvalue );
}
} else {
$value = $value * $this->precision;
}
return $value;
}
/**
* Remove precision (deep) from a price.
*
* @since 3.2.0
* @param int|array $value Value to remove precision from.
* @return float
*/
protected function remove_precision( $value ) {
return wc_format_decimal( $value / $this->precision, wc_get_price_decimals() );
if ( is_array( $value ) ) {
foreach ( $value as $key => $subvalue ) {
$value[ $key ] = $this->remove_precision( $subvalue );
}
} else {
$value = wc_format_decimal( $value / $this->precision, wc_get_price_decimals() );
}
return $value;
}
/**

View File

@ -408,7 +408,7 @@ class WC_Totals {
$discounts->apply_coupon( $coupon );
}
$this->discount_totals = $discounts->get_discounts();
$this->discount_totals = $discounts->get_discounts( true );
$this->totals['discounts_total'] = array_sum( $this->discount_totals );
// See how much tax was 'discounted'.

View File

@ -6,8 +6,9 @@
*/
class WC_Tests_Discounts extends WC_Unit_Test_Case {
protected function get_items_for_discounts_class( $items ) {
$items = array();
protected function get_items_for_discounts_class() {
$items = array();
$precision = pow( 10, wc_get_price_decimals() );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = (object) array(
'key' => '',
@ -25,7 +26,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
);
$item->key = $cart_item_key;
$item->quantity = $cart_item['quantity'];
$item->price = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity'];
$item->price = $cart_item['data']->get_price() * $precision * $cart_item['quantity'];
$item->product = $cart_item['data'];
$items[ $cart_item_key ] = $item;
}

View File

@ -113,7 +113,7 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
'items_total_tax' => 5.40,
'total' => 90.40,
'taxes' => array(
1 => array(
$this->ids['tax_rate_ids'][0] => array(
'tax_total' => 11.40,
'shipping_tax_total' => 2.00,
),