Skip to content

Commit 26ce8d2

Browse files
committed
test: add coverage for tax override changes
1 parent e3e18a7 commit 26ce8d2

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

tests/php/test-class-wc-connect-taxjar-integration.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,101 @@ public function test_override_item_tax_rates_returns_taxjar_rates_when_matched()
705705
WC_Tax::_delete_tax_rate( $rate_id );
706706
}
707707

708+
// -------------------------------------------------------------------------
709+
// override_order_item_taxes() tests
710+
// -------------------------------------------------------------------------
711+
712+
/**
713+
* Test no-op when response_rate_ids is empty (no TaxJar calc this request).
714+
*/
715+
public function test_override_order_item_taxes_noop_when_no_response() {
716+
$order = WC_Helper_Order::create_order();
717+
$items = $order->get_items();
718+
$item = reset( $items );
719+
$orig = $item->get_taxes();
720+
721+
$this->integration->override_order_item_taxes( $item, array() );
722+
723+
$this->assertSame( $orig, $item->get_taxes() );
724+
$order->delete( true );
725+
}
726+
727+
/**
728+
* Test no-op when item is not a WC_Order_Item_Product (e.g. fee or shipping).
729+
*/
730+
public function test_override_order_item_taxes_skips_non_product_items() {
731+
$this->set_private_property( 'response_rate_ids', array( '10-abc' => array( 1 ) ) );
732+
733+
$fee = new WC_Order_Item_Fee();
734+
$fee->set_total( '5.00' );
735+
736+
// Should not throw or modify.
737+
$this->integration->override_order_item_taxes( $fee, array() );
738+
739+
$this->assertEmpty( $fee->get_taxes()['total'] );
740+
}
741+
742+
/**
743+
* Test no-op when the product is not found in response_rate_ids (cross-state item).
744+
*/
745+
public function test_override_order_item_taxes_skips_unmatched_product() {
746+
$this->set_private_property( 'response_rate_ids', array( '999-abc' => array( 1 ) ) );
747+
748+
$order = WC_Helper_Order::create_order();
749+
$items = $order->get_items();
750+
$item = reset( $items );
751+
$orig = $item->get_taxes();
752+
753+
$this->integration->override_order_item_taxes( $item, array() );
754+
755+
$this->assertSame( $orig, $item->get_taxes() );
756+
$order->delete( true );
757+
}
758+
759+
/**
760+
* Test that TaxJar rates are applied for a matched base-group item.
761+
*/
762+
public function test_override_order_item_taxes_applies_taxjar_rates_when_matched() {
763+
$this->product = WC_Helper_Product::create_simple_product();
764+
$this->product->set_regular_price( '20.00' );
765+
$this->product->save();
766+
767+
// Insert a tax rate into the database.
768+
$rate_id = WC_Tax::_insert_tax_rate(
769+
array(
770+
'tax_rate_country' => 'US',
771+
'tax_rate_state' => 'CA',
772+
'tax_rate' => '10.0000',
773+
'tax_rate_name' => 'CA Tax',
774+
'tax_rate_shipping' => 'no',
775+
'tax_rate_compound' => 'no',
776+
'tax_rate_priority' => 1,
777+
'tax_rate_class' => '',
778+
)
779+
);
780+
781+
$product_id = $this->product->get_id();
782+
$this->set_private_property( 'response_rate_ids', array( $product_id . '-abc123' => array( $rate_id ) ) );
783+
784+
// Create an order item.
785+
$item = new WC_Order_Item_Product();
786+
$item->set_product( $this->product );
787+
$item->set_quantity( 1 );
788+
$item->set_total( '20.00' );
789+
$item->set_subtotal( '20.00' );
790+
791+
$this->integration->override_order_item_taxes( $item, array() );
792+
793+
$taxes = $item->get_taxes();
794+
$this->assertArrayHasKey( $rate_id, $taxes['total'] );
795+
$this->assertEquals( 2.0, (float) $taxes['total'][ $rate_id ] );
796+
$this->assertArrayHasKey( $rate_id, $taxes['subtotal'] );
797+
$this->assertEquals( 2.0, (float) $taxes['subtotal'][ $rate_id ] );
798+
799+
// Clean up.
800+
WC_Tax::_delete_tax_rate( $rate_id );
801+
}
802+
708803
// -------------------------------------------------------------------------
709804
// calculate_taxes_by_location() tests
710805
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)