Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions modules/ppcp-store-sync/src/Helper/CartHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ public static function calculate_totals( WC_Cart $wc_cart, string $currency_code
}

$totals = array(
'item_total' => self::money( $currency_code, $item_total ),
'shipping' => self::money( $currency_code, $shipping_total ),
'tax_total' => self::money( $currency_code, $tax_total ),
'amount' => self::money( $currency_code, $cart_total ),
'subtotal' => self::money( $currency_code, $item_total ),
'shipping' => self::money( $currency_code, $shipping_total ),
'tax' => self::money( $currency_code, $tax_total ),
'total' => self::money( $currency_code, $cart_total ),
);

if ( $discount_total > 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public function test_validate_skips_remove_action_coupons(): void
public function test_validate_returns_error_when_coupons_disabled(): void
{
// This test requires actual WooCommerce classes, so skip in unit test environment.
if (!class_exists('WC_Coupon') || !class_exists('WC_Discounts')) {
$this->markTestSkipped('WooCommerce classes not available in unit test environment');
// TODO: This test does not run in a unit-test environment. How is it executed?
if ( ! class_exists( 'WC_Coupon' ) || ! class_exists( 'WC_Discounts' ) ) {
$this->markTestSkipped( 'WooCommerce classes not available in unit test environment' );
}

// Stub wc_coupons_enabled to return false for this test.
Expand Down
110 changes: 76 additions & 34 deletions tests/PHPUnit/StoreSync/Response/CartResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ public function setUp(): void {
Monkey\Functions\when( 'get_woocommerce_currency' )->justReturn( 'USD' );
}

/**
* Asserts that the provided value is a valid money-schema
*
* @param mixed $entity An array with the keys "value" and "currency".
* @param null|float $value The expected monetary value.
* @param null|string $currency The expected currency; only verified when a non-empty string
* is provided.
* @return void
*/
private function assertMoneyValue( $entity, ?float $value = null, ?string $currency = null ): void {
// Verify array structure.
$this->assertIsArray( $entity, 'Expected an array with money entity.' );
$this->assertArrayHasKey( 'value', $entity, 'Money entity has no "value" key.' );
$this->assertArrayHasKey( 'currency_code', $entity, 'Money entity has no "currency_code" key.' );

// Verify data types.
$this->assertIsString( $entity['value'], 'The "value" item should be a numeric string.' );
$this->assertIsString( $entity['currency_code'], 'The "currency_code" item should be a string.' );

// Verify the number format of the value (2 decimals)
// Will catch invalid conversions, eg 0.010000000000001563 instead of 0.01
$this->assertMatchesRegularExpression( '/^\d+\.\d{2}$/', $entity['value'], 'The "value" should match format "XX.XX"' );

// Verify the numerical value.
if ( null !== $value ) {
$value_string = number_format( $value, 2 );
$this->assertEquals( $value_string, $entity['value'], 'Unexpected monetary value.' );
}

// Verify currency, if provided.
if ( $currency ) {
$this->assertEquals( $currency, $entity['currency_code'], 'Unexpected "currency_code" value.' );
}
}

// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -95,8 +130,7 @@ public function test_to_array_includes_applied_coupons_when_provided(): void {
$coupon = $result['applied_coupons'][0];
$this->assertEquals( 'TESTCOUPON', $coupon['code'] );
$this->assertEquals( '10% off', $coupon['description'] );
$this->assertEquals( 'USD', $coupon['discount_amount']['currency_code'] );
$this->assertEquals( '10.00', $coupon['discount_amount']['value'] );
$this->assertMoneyValue( $coupon['discount_amount'], 10.0, 'USD' );
}

/**
Expand Down Expand Up @@ -160,8 +194,7 @@ public function test_calculate_totals_includes_discount_field_when_coupons_appli
$result = $response->to_array();

$this->assertArrayHasKey( 'discount', $result['totals'] );
$this->assertEquals( 'USD', $result['totals']['discount']['currency_code'] );
$this->assertEquals( '10.00', $result['totals']['discount']['value'] );
$this->assertMoneyValue( $result['totals']['discount'], 10.0, 'USD' );
}

/**
Expand Down Expand Up @@ -204,9 +237,12 @@ public function test_calculate_totals_subtracts_discount_from_amount(): void {

$result = $response->to_array();

$this->assertEquals( '100.00', $result['totals']['item_total']['value'] );
$this->assertEquals( '20.00', $result['totals']['discount']['value'] );
$this->assertEquals( 80.0, $result['totals']['amount']['value'] );
// Item total: 2 * 50.00 = 100.00
// Discount: 20.00
// Amount: 100.00 - 20.00 = 80.00
$this->assertMoneyValue( $result['totals']['subtotal'], 100.0, 'USD' );
$this->assertMoneyValue( $result['totals']['discount'], 20.0, 'USD' );
$this->assertMoneyValue( $result['totals']['total'], 80.0, 'USD' );
}

// -------------------------------------------------------------------------
Expand All @@ -229,20 +265,22 @@ public function test_response_structure_matches_paypal_spec(): void {

$result = $response->to_array();

// Verify required fields exist
$this->assertArrayHasKey( 'id', $result );
$this->assertArrayHasKey( 'status', $result );
$this->assertArrayHasKey( 'validation_status', $result );
$this->assertArrayHasKey( 'validation_issues', $result );
$this->assertArrayHasKey( 'totals', $result );

$this->assertArrayHasKey( 'item_total', $result['totals'] );
// Verify totals structure
$this->assertArrayHasKey( 'subtotal', $result['totals'] );
$this->assertArrayHasKey( 'shipping', $result['totals'] );
$this->assertArrayHasKey( 'tax_total', $result['totals'] );
$this->assertArrayHasKey( 'amount', $result['totals'] );
$this->assertArrayHasKey( 'tax', $result['totals'] );
$this->assertArrayHasKey( 'total', $result['totals'] );

foreach ( array( 'item_total', 'shipping', 'tax_total', 'amount' ) as $field ) {
$this->assertArrayHasKey( 'currency_code', $result['totals'][ $field ] );
$this->assertArrayHasKey( 'value', $result['totals'][ $field ] );
// Verify each total has currency_code and value
foreach ( array( 'subtotal', 'shipping', 'tax', 'total' ) as $field ) {
$this->assertMoneyValue( $result['totals'][ $field ] );
}
}

Expand Down Expand Up @@ -278,8 +316,11 @@ public function test_multiple_coupons_discount_amounts_are_summed(): void {

$result = $response->to_array();

$this->assertEquals( '15.00', $result['totals']['discount']['value'] );
$this->assertEquals( 85.0, $result['totals']['amount']['value'] );
// Total discount should be 10.00 + 5.00 = 15.00
$this->assertMoneyValue( $result['totals']['discount'], 15.0, 'USD' );

// Amount should be 100.00 - 15.00 = 85.00
$this->assertMoneyValue( $result['totals']['total'], 85.0, 'USD' );
}

/**
Expand Down Expand Up @@ -311,9 +352,14 @@ public function test_discount_is_capped_when_exceeds_item_total(): void {

$result = $response->to_array();

$this->assertEquals( '24.99', $result['totals']['discount']['value'] );
$this->assertEquals( '0.01', $result['totals']['amount']['value'] );
$this->assertEquals( '25.00', $result['totals']['item_total']['value'] );
// Discount should be capped at subtotal - 0.01 = 24.99
$this->assertMoneyValue( $result['totals']['discount'], 24.99, 'USD' );

// Amount should be minimum 0.01 to satisfy PayPal
$this->assertMoneyValue( $result['totals']['total'], 0.01, 'USD' );

// Item total should remain unchanged
$this->assertMoneyValue( $result['totals']['subtotal'], 25.0, 'USD' );
}

/**
Expand All @@ -339,8 +385,11 @@ public function test_discount_equal_to_item_total_is_capped(): void {

$result = $response->to_array();

$this->assertEquals( '49.99', $result['totals']['discount']['value'] );
$this->assertEquals( '0.01', $result['totals']['amount']['value'] );
// Discount should be capped at subtotal - 0.01 = 49.99
$this->assertMoneyValue( $result['totals']['discount'], 49.99, 'USD' );

// Amount should be minimum 0.01 to satisfy PayPal
$this->assertMoneyValue( $result['totals']['total'], 0.01, 'USD' );
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -376,9 +425,7 @@ public function test_amount_formatting_avoids_floating_point_precision_issues():

$result = $response->to_array();

$this->assertIsString( $result['totals']['amount']['value'] );
$this->assertEquals( '0.01', $result['totals']['amount']['value'] );
$this->assertMatchesRegularExpression( '/^\d+\.\d{2}$/', $result['totals']['amount']['value'] );
$this->assertMoneyValue( $result['totals']['total'], 0.01, 'USD' );
}

/**
Expand All @@ -404,19 +451,14 @@ public function test_all_money_values_are_formatted_consistently(): void {

$result = $response->to_array();

$money_fields = array( 'item_total', 'discount', 'shipping', 'tax_total', 'amount' );
// All money values should be strings with 2 decimal places
$money_fields = array( 'subtotal', 'discount', 'shipping', 'tax', 'total' );
foreach ( $money_fields as $field ) {
if ( isset( $result['totals'][ $field ] ) ) {
$this->assertIsString(
$result['totals'][ $field ]['value'],
"Field {$field} value should be a string"
);
$this->assertMatchesRegularExpression(
'/^\d+\.\d{2}$/',
$result['totals'][ $field ]['value'],
"Field {$field} should match format 'XX.XX'"
);
if ( ! isset( $result['totals'][ $field ] ) ) {
continue;
}

$this->assertMoneyValue( $result['totals'][ $field ] );
}
}

Expand Down
Loading