1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype https://www.mediotype.com/
*/
namespace Vertex\Tax\Test\Integration\CountryRestriction;
use Magento\Checkout\Api\Data\TotalsInformationInterface;
use Magento\Checkout\Api\Data\TotalsInformationInterfaceFactory;
use Magento\Checkout\Api\TotalsInformationManagementInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Vertex\Tax\Test\Integration\Builder\CartBuilder;
use Vertex\Tax\Test\Integration\Builder\CustomerBuilder;
use Vertex\Tax\Test\Integration\Builder\ProductBuilder;
use Vertex\Tax\Test\Integration\TestCase;
/**
* Ensure that the "use for countries shipping to" configuration setting is respected
*/
class CountryRestrictionTest extends TestCase
{
/** @var AddressInterfaceFactory */
private $addressFactory;
/** @var CartBuilder */
private $cartBuilder;
/** @var CustomerBuilder */
private $customerBuilder;
/** @var ProductBuilder */
private $productBuilder;
/** @var TotalsInformationManagementInterface */
private $totalManager;
/** @var TotalsInformationInterfaceFactory */
private $totalsInformationFactory;
/**
* Fetch objects necessary for running our test
*/
protected function setUp()
{
parent::setUp();
$this->totalManager = $this->getObject(TotalsInformationManagementInterface::class);
$this->totalsInformationFactory = $this->getObject(TotalsInformationInterfaceFactory::class);
$this->addressFactory = $this->getObject(AddressInterfaceFactory::class);
$this->customerBuilder = $this->getObject(CustomerBuilder::class);
$this->productBuilder = $this->getObject(ProductBuilder::class);
$this->cartBuilder = $this->getObject(CartBuilder::class);
}
/**
* Ensure that the "use for countries shipping to" configuration setting is respected
*
* @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
* @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
* @magentoConfigFixture default_store tax/vertex_settings/allowed_countries MX
* @magentoDbIsolation enabled
*
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function testUsesVertexWhenCountryIsEnabled()
{
$soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
$soapClient->expects($this->atLeastOnce())
->method('CalculateTax70')
->willReturn(new \stdClass());
$this->getSoapFactory()->setSoapClient($soapClient);
$this->runTotalsWithMexicoAddress();
}
/**
* Ensure that the "use for countries shipping to" configuration setting is respected
*
* @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
* @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
* @magentoConfigFixture default_store tax/vertex_settings/allowed_countries GB
* @magentoDbIsolation enabled
*
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function testDoesNotUseVertexWhenCountryIsDisabled()
{
$soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
$soapClient->expects($this->never())
->method('CalculateTax70')
->willReturn(new \stdClass());
$this->getSoapFactory()->setSoapClient($soapClient);
$this->runTotalsWithMexicoAddress();
}
/**
* Create a cart using a Mexico address and call totals against it
*
* Shared functionality for tests
*
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\State\InputMismatchException
*/
private function runTotalsWithMexicoAddress()
{
$product = $this->productBuilder->createExampleProduct();
$customer = $this->customerBuilder->createExampleCustomer();
$cart = $this->cartBuilder->setItems()
->addItem($product)
->create($customer->getId());
$address = $this->createAddress($customer);
/** @var TotalsInformationInterface $totalsInfo */
$totalsInfo = $this->totalsInformationFactory->create();
$totalsInfo->setAddress($address);
$totalsInfo->setShippingCarrierCode('flatrate');
$totalsInfo->setShippingMethodCode('flatrate');
$this->totalManager->calculate($cart->getId(), $totalsInfo);
}
/**
* Create Mexico Customer Address
*
* @param int $customerId
* @return AddressInterface
*/
private function createAddress($customerId)
{
/** @var AddressInterface $address */
$address = $this->addressFactory->create();
$address->setCustomerId($customerId);
$address->setStreet(['José María Pino Suárez 30', 'Centro Histórico, Centro']);
$address->setCity('Ciudad de México');
$address->setRegionCode('CDMX');
$address->setRegion('Ciudad de México');
$address->setPostcode('06060');
$address->setCountryId('MX');
$address->setFirstname(CustomerBuilder::EXAMPLE_CUSTOMER_FIRSTNAME);
$address->setLastname(CustomerBuilder::EXAMPLE_CUSTOMER_LASTNAME);
return $address;
}
}