Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 57f9b0e

Browse files
author
Jens Schulze
committed
test(Cart): add tests to cart update actions for setting external tax rates
1 parent f6bfeeb commit 57f9b0e

File tree

4 files changed

+179
-21
lines changed

4 files changed

+179
-21
lines changed

src/Request/Carts/Command/CartSetCustomLineItemTaxRateAction.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
namespace Commercetools\Core\Request\Carts\Command;
77

88
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
910
use Commercetools\Core\Request\AbstractAction;
1011

1112
/**
1213
* @package Commercetools\Core\Request\Carts\Command
1314
* @link http://dev.commercetools.com/http-api-projects-carts.html#set-customlineitem-taxrate
1415
* @method string getAction()
1516
* @method CartSetCustomLineItemTaxRateAction setAction(string $action = null)
16-
* @method string getLineItemId()
17-
* @method CartRemoveLineItemAction setLineItemId(string $lineItemId = null)
1817
* @method string getCustomLineItemId()
1918
* @method CartSetCustomLineItemTaxRateAction setCustomLineItemId(string $customLineItemId = null)
2019
* @method ExternalTaxRateDraft getExternalTaxRate()
@@ -27,7 +26,7 @@ public function fieldDefinitions()
2726
return [
2827
'action' => [static::TYPE => 'string'],
2928
'customLineItemId' => [static::TYPE => 'string'],
30-
'externalTaxRate' => [static::TYPE => 'ExternalTaxRateDraft'],
29+
'externalTaxRate' => [static::TYPE => '\Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft'],
3130
];
3231
}
3332

@@ -44,10 +43,10 @@ public function __construct(array $data = [], $context = null)
4443
/**
4544
* @param $lineItemId
4645
* @param Context|callable $context
47-
* @return CartRemoveLineItemAction
46+
* @return CartSetCustomLineItemTaxRateAction
4847
*/
49-
public static function ofLineItemId($lineItemId, $context = null)
48+
public static function ofCustomLineItemId($lineItemId, $context = null)
5049
{
51-
return static::of($context)->setLineItemId($lineItemId);
50+
return static::of($context)->setCustomLineItemId($lineItemId);
5251
}
5352
}

src/Request/Carts/Command/CartSetLineItemTaxRateAction.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Commercetools\Core\Request\Carts\Command;
77

88
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
910
use Commercetools\Core\Request\AbstractAction;
1011

1112
/**
@@ -25,7 +26,7 @@ public function fieldDefinitions()
2526
return [
2627
'action' => [static::TYPE => 'string'],
2728
'lineItemId' => [static::TYPE => 'string'],
28-
'externalTaxRate' => [static::TYPE => 'ExternalTaxRateDraft'],
29+
'externalTaxRate' => [static::TYPE => '\Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft'],
2930
];
3031
}
3132

@@ -42,7 +43,7 @@ public function __construct(array $data = [], $context = null)
4243
/**
4344
* @param $lineItemId
4445
* @param Context|callable $context
45-
* @return CartRemoveLineItemAction
46+
* @return CartSetLineItemTaxRateAction
4647
*/
4748
public static function ofLineItemId($lineItemId, $context = null)
4849
{

src/Request/Carts/Command/CartSetShippingMethodTaxRateAction.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
namespace Commercetools\Core\Request\Carts\Command;
77

88
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
910
use Commercetools\Core\Request\AbstractAction;
1011

1112
/**
1213
* @package Commercetools\Core\Request\Carts\Command
1314
* @link http://dev.commercetools.com/http-api-projects-carts.html#set-shippingmethod-taxrate
1415
* @method string getAction()
1516
* @method CartSetShippingMethodTaxRateAction setAction(string $action = null)
16-
* @method string getLineItemId()
17-
* @method CartRemoveLineItemAction setLineItemId(string $lineItemId = null)
1817
* @method ExternalTaxRateDraft getExternalTaxRate()
1918
* @method CartSetShippingMethodTaxRateAction setExternalTaxRate(ExternalTaxRateDraft $externalTaxRate = null)
2019
*/
@@ -24,7 +23,7 @@ public function fieldDefinitions()
2423
{
2524
return [
2625
'action' => [static::TYPE => 'string'],
27-
'externalTaxRate' => [static::TYPE => 'ExternalTaxRateDraft'],
26+
'externalTaxRate' => [static::TYPE => '\Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft'],
2827
];
2928
}
3029

@@ -37,14 +36,4 @@ public function __construct(array $data = [], $context = null)
3736
parent::__construct($data, $context);
3837
$this->setAction('setShippingMethodTaxRate');
3938
}
40-
41-
/**
42-
* @param $lineItemId
43-
* @param Context|callable $context
44-
* @return CartRemoveLineItemAction
45-
*/
46-
public static function ofLineItemId($lineItemId, $context = null)
47-
{
48-
return static::of($context)->setLineItemId($lineItemId);
49-
}
5039
}

tests/integration/Cart/CartTaxModeTest.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Commercetools\Core\Model\Cart\CustomLineItemCollection;
1515
use Commercetools\Core\Model\Cart\CustomLineItemDraft;
1616
use Commercetools\Core\Model\Cart\CustomLineItemDraftCollection;
17+
use Commercetools\Core\Model\Cart\LineItemCollection;
18+
use Commercetools\Core\Model\Cart\LineItemDraft;
19+
use Commercetools\Core\Model\Cart\LineItemDraftCollection;
1720
use Commercetools\Core\Model\Common\Address;
1821
use Commercetools\Core\Model\Common\LocalizedString;
1922
use Commercetools\Core\Model\Common\Money;
@@ -33,6 +36,7 @@
3336
use Commercetools\Core\Request\Carts\Command\CartAddLineItemAction;
3437
use Commercetools\Core\Request\Carts\Command\CartAddPaymentAction;
3538
use Commercetools\Core\Request\Carts\Command\CartChangeLineItemQuantityAction;
39+
use Commercetools\Core\Request\Carts\Command\CartChangeTaxModeAction;
3640
use Commercetools\Core\Request\Carts\Command\CartRecalculateAction;
3741
use Commercetools\Core\Request\Carts\Command\CartRemoveCustomLineItemAction;
3842
use Commercetools\Core\Request\Carts\Command\CartRemoveDiscountCodeAction;
@@ -44,11 +48,14 @@
4448
use Commercetools\Core\Request\Carts\Command\CartSetCustomerIdAction;
4549
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomFieldAction;
4650
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomTypeAction;
51+
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemTaxRateAction;
4752
use Commercetools\Core\Request\Carts\Command\CartSetCustomShippingMethodAction;
4853
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomFieldAction;
4954
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomTypeAction;
55+
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTaxRateAction;
5056
use Commercetools\Core\Request\Carts\Command\CartSetShippingAddressAction;
5157
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodAction;
58+
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodTaxRateAction;
5259
use Commercetools\Core\Request\Customers\CustomerLoginRequest;
5360
use Commercetools\Core\Request\CustomField\Command\SetCustomFieldAction;
5461
use Commercetools\Core\Request\CustomField\Command\SetCustomTypeAction;
@@ -356,6 +363,168 @@ public function testAddCustomLineItemExternalTaxSubRatesOnly()
356363
$this->assertContains($subRate2, $subRates);
357364
}
358365

366+
public function testSetCustomLineItemExternalTaxRate()
367+
{
368+
$draft = $this->getDraft();
369+
$draft->setCustomLineItems(
370+
CustomLineItemDraftCollection::of()->add(
371+
CustomLineItemDraft::of()
372+
->setName(LocalizedString::ofLangAndText('en', 'Test'))
373+
->setQuantity(1)
374+
->setMoney(Money::ofCurrencyAndAmount('EUR', 100))
375+
->setSlug('test')
376+
)
377+
);
378+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL);
379+
$cart = $this->createCart($draft);
380+
381+
$taxRateName = 'test';
382+
$taxRateCountry = 'DE';
383+
$taxRate = 0.1;
384+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
385+
->addAction(
386+
CartSetCustomLineItemTaxRateAction::ofCustomLineItemId($cart->getCustomLineItems()->current()->getId())
387+
->setExternalTaxRate(
388+
ExternalTaxRateDraft::ofNameCountryAndAmount(
389+
$taxRateName,
390+
$taxRateCountry,
391+
$taxRate
392+
)
393+
)
394+
)
395+
;
396+
397+
$response = $request->executeWithClient($this->getClient());
398+
$cart = $request->mapResponse($response);
399+
$this->deleteRequest->setVersion($cart->getVersion());
400+
401+
$this->assertSame($taxRateName, $cart->getCustomLineItems()->current()->getTaxRate()->getName());
402+
$this->assertSame($taxRateCountry, $cart->getCustomLineItems()->current()->getTaxRate()->getCountry());
403+
$this->assertSame($taxRate, $cart->getCustomLineItems()->current()->getTaxRate()->getAmount());
404+
}
405+
406+
public function testSetLineItemExternalTaxRate()
407+
{
408+
$product = $this->getProduct();
409+
410+
$draft = $this->getDraft();
411+
$draft->setLineItems(
412+
LineItemDraftCollection::of()->add(
413+
LineItemDraft::of()
414+
->setProductId($product->getId())
415+
->setQuantity(1)
416+
->setVariantId($product->getMasterData()->getCurrent()->getMasterVariant()->getId())
417+
)
418+
);
419+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL);
420+
$cart = $this->createCart($draft);
421+
422+
$taxRateName = 'test';
423+
$taxRateCountry = 'DE';
424+
$taxRate = 0.1;
425+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
426+
->addAction(
427+
CartSetLineItemTaxRateAction::ofLineItemId($cart->getLineItems()->current()->getId())
428+
->setExternalTaxRate(
429+
ExternalTaxRateDraft::ofNameCountryAndAmount(
430+
$taxRateName,
431+
$taxRateCountry,
432+
$taxRate
433+
)
434+
)
435+
)
436+
;
437+
438+
$response = $request->executeWithClient($this->getClient());
439+
$cart = $request->mapResponse($response);
440+
$this->deleteRequest->setVersion($cart->getVersion());
441+
442+
$this->assertSame($taxRateName, $cart->getLineItems()->current()->getTaxRate()->getName());
443+
$this->assertSame($taxRateCountry, $cart->getLineItems()->current()->getTaxRate()->getCountry());
444+
$this->assertSame($taxRate, $cart->getLineItems()->current()->getTaxRate()->getAmount());
445+
}
446+
447+
public function testSetShippingMethodExternalTaxRate()
448+
{
449+
$shippingMethod = $this->getShippingMethod();
450+
451+
$draft = $this->getDraft();
452+
$draft->setShippingAddress(Address::of()->setCountry('DE')->setState($this->getRegion()));
453+
$draft->setShippingMethod(
454+
$shippingMethod->getReference()
455+
);
456+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL);
457+
$cart = $this->createCart($draft);
458+
459+
$taxRateName = 'test';
460+
$taxRateCountry = 'DE';
461+
$taxRate = 0.1;
462+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
463+
->addAction(
464+
CartSetShippingMethodTaxRateAction::of()
465+
->setExternalTaxRate(
466+
ExternalTaxRateDraft::ofNameCountryAndAmount(
467+
$taxRateName,
468+
$taxRateCountry,
469+
$taxRate
470+
)
471+
)
472+
)
473+
;
474+
475+
$response = $request->executeWithClient($this->getClient());
476+
$cart = $request->mapResponse($response);
477+
$this->deleteRequest->setVersion($cart->getVersion());
478+
479+
$this->assertSame($taxRateName, $cart->getShippingInfo()->getTaxRate()->getName());
480+
$this->assertSame($taxRateCountry, $cart->getShippingInfo()->getTaxRate()->getCountry());
481+
$this->assertSame($taxRate, $cart->getShippingInfo()->getTaxRate()->getAmount());
482+
}
483+
484+
public function testChangeTaxMode()
485+
{
486+
$product = $this->getProduct();
487+
$draft = $this->getDraft();
488+
489+
$taxRateName = 'test';
490+
$taxRateCountry = 'DE';
491+
$taxRate = 0.1;
492+
493+
$draft->setLineItems(
494+
LineItemDraftCollection::of()->add(
495+
LineItemDraft::of()
496+
->setProductId($product->getId())
497+
->setQuantity(1)
498+
->setVariantId($product->getMasterData()->getCurrent()->getMasterVariant()->getId())
499+
->setExternalTaxRate(
500+
ExternalTaxRateDraft::ofNameCountryAndAmount(
501+
$taxRateName,
502+
$taxRateCountry,
503+
$taxRate
504+
)
505+
)
506+
)
507+
);
508+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL);
509+
$cart = $this->createCart($draft);
510+
511+
$this->assertSame($taxRateName, $cart->getLineItems()->current()->getTaxRate()->getName());
512+
$this->assertSame($taxRateCountry, $cart->getLineItems()->current()->getTaxRate()->getCountry());
513+
$this->assertSame($taxRate, $cart->getLineItems()->current()->getTaxRate()->getAmount());
514+
515+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
516+
->addAction(
517+
CartChangeTaxModeAction::of()->setTaxMode(Cart::TAX_MODE_PLATFORM)
518+
)
519+
;
520+
521+
$response = $request->executeWithClient($this->getClient());
522+
$cart = $request->mapResponse($response);
523+
$this->deleteRequest->setVersion($cart->getVersion());
524+
525+
$this->assertNull($cart->getLineItems()->current()->getTaxRate());
526+
}
527+
359528
/**
360529
* @return CartDraft
361530
*/

0 commit comments

Comments
 (0)