|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\Test; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Shopware\Core\Checkout\Cart\Cart; |
|
8
|
|
|
use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation; |
|
9
|
|
|
use Shopware\Core\Checkout\Cart\LineItem\LineItem; |
|
10
|
|
|
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection; |
|
11
|
|
|
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice; |
|
12
|
|
|
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice; |
|
13
|
|
|
use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition; |
|
14
|
|
|
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection; |
|
15
|
|
|
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection; |
|
16
|
|
|
use Shopware\Core\Checkout\Cart\Tax\TaxDetector; |
|
17
|
|
|
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity; |
|
18
|
|
|
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity; |
|
19
|
|
|
use Shopware\Core\Checkout\Customer\CustomerEntity; |
|
20
|
|
|
use Shopware\Core\Checkout\Payment\PaymentMethodEntity; |
|
21
|
|
|
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity; |
|
22
|
|
|
use Shopware\Core\Content\Product\Cart\ProductGateway; |
|
23
|
|
|
use Shopware\Core\Defaults; |
|
24
|
|
|
use Shopware\Core\Framework\Context; |
|
25
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig; |
|
26
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\TaxFreeConfig; |
|
27
|
|
|
use Shopware\Core\Framework\Log\Package; |
|
28
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
|
29
|
|
|
use Shopware\Core\System\Country\Aggregate\CountryState\CountryStateEntity; |
|
30
|
|
|
use Shopware\Core\System\Country\CountryEntity; |
|
31
|
|
|
use Shopware\Core\System\Currency\CurrencyEntity; |
|
32
|
|
|
use Shopware\Core\System\DeliveryTime\DeliveryTimeEntity; |
|
33
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
|
34
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelDefinition; |
|
35
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelEntity; |
|
36
|
|
|
use Shopware\Core\System\Tax\TaxCollection; |
|
37
|
|
|
use Shopware\Core\System\Tax\TaxEntity; |
|
38
|
|
|
use Shopware\Core\Test\Integration\PaymentHandler\SyncTestPaymentHandler; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @internal |
|
42
|
|
|
*/ |
|
43
|
|
|
#[Package('checkout')] |
|
44
|
|
|
class Generator extends TestCase |
|
45
|
|
|
{ |
|
46
|
|
|
public static function createSalesChannelContext( |
|
47
|
|
|
?Context $baseContext = null, |
|
48
|
|
|
?CustomerGroupEntity $currentCustomerGroup = null, |
|
49
|
|
|
?SalesChannelEntity $salesChannel = null, |
|
50
|
|
|
?CurrencyEntity $currency = null, |
|
51
|
|
|
?TaxCollection $taxes = null, |
|
52
|
|
|
?CountryEntity $country = null, |
|
53
|
|
|
?CountryStateEntity $state = null, |
|
54
|
|
|
?CustomerAddressEntity $shipping = null, |
|
55
|
|
|
?PaymentMethodEntity $paymentMethod = null, |
|
56
|
|
|
?ShippingMethodEntity $shippingMethod = null, |
|
57
|
|
|
?CustomerEntity $customer = null, |
|
58
|
|
|
?string $token = null, |
|
59
|
|
|
?string $domainId = null, |
|
60
|
|
|
): SalesChannelContext { |
|
61
|
|
|
if (!$baseContext) { |
|
62
|
|
|
$baseContext = Context::createDefaultContext(); |
|
63
|
|
|
} |
|
64
|
|
|
if ($salesChannel === null) { |
|
65
|
|
|
$salesChannel = new SalesChannelEntity(); |
|
66
|
|
|
$salesChannel->setId('ffa32a50e2d04cf38389a53f8d6cd594'); |
|
67
|
|
|
$salesChannel->setNavigationCategoryId(Uuid::randomHex()); |
|
68
|
|
|
$salesChannel->setTaxCalculationType(SalesChannelDefinition::CALCULATION_TYPE_HORIZONTAL); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$currency = $currency ?: (new CurrencyEntity())->assign([ |
|
72
|
|
|
'id' => '4c8eba11bd3546d786afbed481a6e665', |
|
73
|
|
|
'factor' => 1, |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
$currency->setFactor(1); |
|
77
|
|
|
|
|
78
|
|
|
if (!$currentCustomerGroup) { |
|
79
|
|
|
$currentCustomerGroup = new CustomerGroupEntity(); |
|
80
|
|
|
$currentCustomerGroup->setId(TestDefaults::FALLBACK_CUSTOMER_GROUP); |
|
81
|
|
|
$currentCustomerGroup->setDisplayGross(true); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!$taxes) { |
|
85
|
|
|
$tax = new TaxEntity(); |
|
86
|
|
|
$tax->setId('4926035368e34d9fa695e017d7a231b9'); |
|
87
|
|
|
$tax->setName('test'); |
|
88
|
|
|
$tax->setTaxRate(19.0); |
|
89
|
|
|
|
|
90
|
|
|
$taxes = new TaxCollection([$tax]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!$country) { |
|
94
|
|
|
$country = new CountryEntity(); |
|
95
|
|
|
$country->setId('5cff02b1029741a4891c430bcd9e3603'); |
|
96
|
|
|
$country->setCustomerTax(new TaxFreeConfig(false, Defaults::CURRENCY, 0)); |
|
97
|
|
|
$country->setCompanyTax(new TaxFreeConfig(false, Defaults::CURRENCY, 0)); |
|
98
|
|
|
$country->setName('Germany'); |
|
99
|
|
|
} |
|
100
|
|
|
if (!$state) { |
|
101
|
|
|
$state = new CountryStateEntity(); |
|
102
|
|
|
$state->setId('bd5e2dcf547e4df6bb1ff58a554bc69e'); |
|
103
|
|
|
$state->setCountryId($country->getId()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!$shipping) { |
|
107
|
|
|
$shipping = new CustomerAddressEntity(); |
|
108
|
|
|
$shipping->setCountry($country); |
|
109
|
|
|
$shipping->setCountryState($state); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (!$paymentMethod) { |
|
113
|
|
|
$paymentMethod = (new PaymentMethodEntity())->assign( |
|
114
|
|
|
[ |
|
115
|
|
|
'id' => '19d144ffe15f4772860d59fca7f207c1', |
|
116
|
|
|
'handlerIdentifier' => SyncTestPaymentHandler::class, |
|
117
|
|
|
'name' => 'Generated Payment', |
|
118
|
|
|
'active' => true, |
|
119
|
|
|
] |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (!$shippingMethod) { |
|
124
|
|
|
$deliveryTime = new DeliveryTimeEntity(); |
|
125
|
|
|
$deliveryTime->setMin(1); |
|
126
|
|
|
$deliveryTime->setMax(2); |
|
127
|
|
|
$deliveryTime->setUnit(DeliveryTimeEntity::DELIVERY_TIME_DAY); |
|
128
|
|
|
|
|
129
|
|
|
$shippingMethod = new ShippingMethodEntity(); |
|
130
|
|
|
$shippingMethod->setDeliveryTime($deliveryTime); |
|
131
|
|
|
$shippingMethod->setId('8beeb66e9dda46b18891a059257a590e'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (!$customer) { |
|
135
|
|
|
$customer = (new CustomerEntity())->assign(['id' => Uuid::randomHex()]); |
|
136
|
|
|
$customer->setId(Uuid::randomHex()); |
|
137
|
|
|
$customer->setGroup($currentCustomerGroup); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return new SalesChannelContext( |
|
141
|
|
|
$baseContext, |
|
142
|
|
|
$token ?? Uuid::randomHex(), |
|
143
|
|
|
$domainId ?? Uuid::randomHex(), |
|
144
|
|
|
$salesChannel, |
|
145
|
|
|
$currency, |
|
146
|
|
|
$currentCustomerGroup, |
|
147
|
|
|
$taxes, |
|
148
|
|
|
$paymentMethod, |
|
149
|
|
|
$shippingMethod, |
|
150
|
|
|
ShippingLocation::createFromAddress($shipping), |
|
151
|
|
|
$customer, |
|
152
|
|
|
new CashRoundingConfig(2, 0.01, true), |
|
153
|
|
|
new CashRoundingConfig(2, 0.01, true), |
|
154
|
|
|
[] |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public static function createGrossPriceDetector(): TaxDetector |
|
159
|
|
|
{ |
|
160
|
|
|
return (new self())->createTaxDetector(true, false); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public static function createNetPriceDetector(): TaxDetector |
|
164
|
|
|
{ |
|
165
|
|
|
return (new self())->createTaxDetector(false, false); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public static function createNetDeliveryDetector(): TaxDetector |
|
169
|
|
|
{ |
|
170
|
|
|
return (new self())->createTaxDetector(false, true); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param QuantityPriceDefinition[] $priceDefinitions indexed by product number |
|
175
|
|
|
*/ |
|
176
|
|
|
public function createProductPriceGateway($priceDefinitions): ProductGateway |
|
177
|
|
|
{ |
|
178
|
|
|
/** @var MockObject|ProductGateway $mock */ |
|
179
|
|
|
$mock = $this->createMock(ProductGateway::class); |
|
180
|
|
|
$mock |
|
181
|
|
|
->method('get') |
|
182
|
|
|
->willReturn($priceDefinitions); |
|
183
|
|
|
|
|
184
|
|
|
return $mock; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public static function createCart(): Cart |
|
188
|
|
|
{ |
|
189
|
|
|
$cart = new Cart('test'); |
|
190
|
|
|
$cart->setLineItems( |
|
191
|
|
|
new LineItemCollection([ |
|
192
|
|
|
(new LineItem('A', 'product', 'A', 27)) |
|
193
|
|
|
->setPrice(new CalculatedPrice(10, 270, new CalculatedTaxCollection(), new TaxRuleCollection(), 27)), |
|
194
|
|
|
(new LineItem('B', 'test', 'B', 5)) |
|
195
|
|
|
->setGood(false) |
|
196
|
|
|
->setPrice(new CalculatedPrice(0, 0, new CalculatedTaxCollection(), new TaxRuleCollection())), |
|
197
|
|
|
]) |
|
198
|
|
|
); |
|
199
|
|
|
$cart->setPrice( |
|
200
|
|
|
new CartPrice( |
|
201
|
|
|
275.0, |
|
202
|
|
|
275.0, |
|
203
|
|
|
0, |
|
204
|
|
|
new CalculatedTaxCollection(), |
|
205
|
|
|
new TaxRuleCollection(), |
|
206
|
|
|
CartPrice::TAX_STATE_GROSS |
|
207
|
|
|
) |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
return $cart; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
private function createTaxDetector(bool $useGross, bool $isNetDelivery): TaxDetector |
|
214
|
|
|
{ |
|
215
|
|
|
/** @var MockObject|TaxDetector $mock */ |
|
216
|
|
|
$mock = $this->createMock(TaxDetector::class); |
|
217
|
|
|
$mock |
|
218
|
|
|
->method('useGross') |
|
219
|
|
|
->willReturn($useGross); |
|
220
|
|
|
|
|
221
|
|
|
$mock |
|
222
|
|
|
->method('isNetDelivery') |
|
223
|
|
|
->willReturn($isNetDelivery); |
|
224
|
|
|
|
|
225
|
|
|
return $mock; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|