1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace spec\Sylius\Component\Core\Taxation; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
15
|
|
|
use PhpSpec\ObjectBehavior; |
16
|
|
|
use Prophecy\Argument; |
17
|
|
|
use Sylius\Bundle\CoreBundle\Distributor\IntegerDistributorInterface; |
18
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
19
|
|
|
use Sylius\Component\Core\Model\AdjustmentInterface; |
20
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
21
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
22
|
|
|
use Sylius\Component\Core\Model\OrderItemUnitInterface; |
23
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
24
|
|
|
use Sylius\Component\Core\Taxation\OrderItemsTaxesByZoneApplicatorInterface; |
25
|
|
|
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface; |
26
|
|
|
use Sylius\Component\Taxation\Calculator\CalculatorInterface; |
27
|
|
|
use Sylius\Component\Taxation\Model\TaxRateInterface; |
28
|
|
|
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author Mateusz Zalewski <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class OrderItemsTaxesByZoneApplicatorSpec extends ObjectBehavior |
34
|
|
|
{ |
35
|
|
|
function let( |
36
|
|
|
CalculatorInterface $calculator, |
37
|
|
|
AdjustmentFactoryInterface $adjustmentsFactory, |
38
|
|
|
IntegerDistributorInterface $distributor, |
39
|
|
|
TaxRateResolverInterface $taxRateResolver |
40
|
|
|
) { |
41
|
|
|
$this->beConstructedWith($calculator, $adjustmentsFactory, $distributor, $taxRateResolver); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function it_is_initializable() |
45
|
|
|
{ |
46
|
|
|
$this->shouldHaveType('Sylius\Component\Core\Taxation\OrderItemsTaxesByZoneApplicator'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function it_implements_order_shipment_taxes_applicator_interface() |
50
|
|
|
{ |
51
|
|
|
$this->shouldImplement(OrderItemsTaxesByZoneApplicatorInterface::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function it_applies_taxes_on_units_based_on_item_total_and_rate( |
55
|
|
|
$adjustmentsFactory, |
56
|
|
|
$calculator, |
57
|
|
|
$distributor, |
58
|
|
|
$taxRateResolver, |
59
|
|
|
AdjustmentInterface $taxAdjustment1, |
60
|
|
|
AdjustmentInterface $taxAdjustment2, |
61
|
|
|
Collection $items, |
62
|
|
|
Collection $units, |
63
|
|
|
\Iterator $iterator, |
64
|
|
|
OrderInterface $order, |
65
|
|
|
OrderItemInterface $orderItem, |
66
|
|
|
OrderItemUnitInterface $unit1, |
67
|
|
|
OrderItemUnitInterface $unit2, |
68
|
|
|
ProductInterface $product, |
69
|
|
|
TaxRateInterface $taxRate, |
70
|
|
|
ZoneInterface $zone |
71
|
|
|
) { |
72
|
|
|
$order->getItems()->willReturn($items); |
73
|
|
|
|
74
|
|
|
$items->count()->willReturn(1); |
75
|
|
|
$items->getIterator()->willReturn($iterator); |
76
|
|
|
$iterator->rewind()->shouldBeCalled(); |
77
|
|
|
$iterator->valid()->willReturn(true, false)->shouldBeCalled(); |
78
|
|
|
$iterator->current()->willReturn($orderItem); |
79
|
|
|
$iterator->next()->shouldBeCalled(); |
80
|
|
|
|
81
|
|
|
$orderItem->getQuantity()->willReturn(2); |
82
|
|
|
|
83
|
|
|
$orderItem->getProduct()->willReturn($product); |
84
|
|
|
$taxRateResolver->resolve($product, array('zone' => $zone))->willReturn($taxRate); |
85
|
|
|
|
86
|
|
|
$orderItem->getTotal()->willReturn(1000); |
87
|
|
|
$calculator->calculate(1000, $taxRate)->willReturn(100); |
88
|
|
|
|
89
|
|
|
$taxRate->getLabel()->willReturn('Simple tax (10%)'); |
90
|
|
|
$taxRate->isIncludedInPrice()->willReturn(false); |
91
|
|
|
|
92
|
|
|
$orderItem->getUnits()->willReturn($units); |
93
|
|
|
|
94
|
|
|
$units->get(0)->willReturn($unit1); |
95
|
|
|
$units->get(1)->willReturn($unit2); |
96
|
|
|
|
97
|
|
|
$distributor->distribute(100, 2)->willReturn(array(50, 50)); |
98
|
|
|
|
99
|
|
|
$adjustmentsFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, 'Simple tax (10%)', 50, false)->willReturn($taxAdjustment1, $taxAdjustment2); |
100
|
|
|
|
101
|
|
|
$unit1->addAdjustment($taxAdjustment1)->shouldBeCalled(); |
102
|
|
|
$unit2->addAdjustment($taxAdjustment2)->shouldBeCalled(); |
103
|
|
|
|
104
|
|
|
$this->apply($order, $zone); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function it_does_nothing_if_order_item_has_no_units( |
108
|
|
|
$taxRateResolver, |
109
|
|
|
Collection $items, |
110
|
|
|
\Iterator $iterator, |
111
|
|
|
OrderInterface $order, |
112
|
|
|
OrderItemInterface $orderItem, |
113
|
|
|
ZoneInterface $zone |
114
|
|
|
) { |
115
|
|
|
$order->getItems()->willReturn($items); |
116
|
|
|
|
117
|
|
|
$items->count()->willReturn(1); |
118
|
|
|
$items->getIterator()->willReturn($iterator); |
119
|
|
|
$iterator->rewind()->shouldBeCalled(); |
120
|
|
|
$iterator->valid()->willReturn(true, false)->shouldBeCalled(); |
121
|
|
|
$iterator->current()->willReturn($orderItem); |
122
|
|
|
$iterator->next()->shouldBeCalled(); |
123
|
|
|
|
124
|
|
|
$orderItem->getQuantity()->willReturn(0); |
125
|
|
|
$taxRateResolver->resolve(Argument::any())->shouldNotBeCalled(); |
126
|
|
|
|
127
|
|
|
$this->apply($order, $zone); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
function it_does_nothing_if_tax_rate_cannot_be_resolved( |
131
|
|
|
$taxRateResolver, |
132
|
|
|
Collection $items, |
133
|
|
|
\Iterator $iterator, |
134
|
|
|
OrderInterface $order, |
135
|
|
|
OrderItemInterface $orderItem, |
136
|
|
|
ProductInterface $product, |
137
|
|
|
ZoneInterface $zone |
138
|
|
|
) { |
139
|
|
|
$order->getItems()->willReturn($items); |
140
|
|
|
|
141
|
|
|
$items->count()->willReturn(1); |
142
|
|
|
$items->getIterator()->willReturn($iterator); |
143
|
|
|
$iterator->rewind()->shouldBeCalled(); |
144
|
|
|
$iterator->valid()->willReturn(true, false)->shouldBeCalled(); |
145
|
|
|
$iterator->current()->willReturn($orderItem); |
146
|
|
|
$iterator->next()->shouldBeCalled(); |
147
|
|
|
|
148
|
|
|
$orderItem->getQuantity()->willReturn(5); |
149
|
|
|
|
150
|
|
|
$orderItem->getProduct()->willReturn($product); |
151
|
|
|
$taxRateResolver->resolve($product, array('zone' => $zone))->willReturn(null); |
152
|
|
|
|
153
|
|
|
$orderItem->getUnits()->shouldNotBeCalled(); |
154
|
|
|
|
155
|
|
|
$this->apply($order, $zone); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
function it_does_not_apply_taxes_with_amount_0( |
159
|
|
|
$adjustmentsFactory, |
160
|
|
|
$calculator, |
161
|
|
|
$distributor, |
162
|
|
|
$taxRateResolver, |
163
|
|
|
AdjustmentInterface $taxAdjustment1, |
164
|
|
|
AdjustmentInterface $taxAdjustment2, |
165
|
|
|
Collection $items, |
166
|
|
|
Collection $units, |
167
|
|
|
\Iterator $iterator, |
168
|
|
|
OrderInterface $order, |
169
|
|
|
OrderItemInterface $orderItem, |
170
|
|
|
OrderItemUnitInterface $unit1, |
171
|
|
|
OrderItemUnitInterface $unit2, |
172
|
|
|
ProductInterface $product, |
173
|
|
|
TaxRateInterface $taxRate, |
174
|
|
|
ZoneInterface $zone |
175
|
|
|
) { |
176
|
|
|
$order->getItems()->willReturn($items); |
177
|
|
|
|
178
|
|
|
$items->count()->willReturn(1); |
179
|
|
|
$items->getIterator()->willReturn($iterator); |
180
|
|
|
$iterator->rewind()->shouldBeCalled(); |
181
|
|
|
$iterator->valid()->willReturn(true, false)->shouldBeCalled(); |
182
|
|
|
$iterator->current()->willReturn($orderItem); |
183
|
|
|
$iterator->next()->shouldBeCalled(); |
184
|
|
|
|
185
|
|
|
$orderItem->getQuantity()->willReturn(2); |
186
|
|
|
|
187
|
|
|
$orderItem->getProduct()->willReturn($product); |
188
|
|
|
$taxRateResolver->resolve($product, array('zone' => $zone))->willReturn($taxRate); |
189
|
|
|
|
190
|
|
|
$orderItem->getTotal()->willReturn(1000); |
191
|
|
|
$calculator->calculate(1000, $taxRate)->willReturn(0); |
192
|
|
|
|
193
|
|
|
$taxRate->getLabel()->willReturn('Simple tax (0%)'); |
194
|
|
|
$taxRate->isIncludedInPrice()->willReturn(false); |
195
|
|
|
|
196
|
|
|
$orderItem->getUnits()->willReturn($units); |
197
|
|
|
|
198
|
|
|
$units->get(0)->willReturn($unit1); |
199
|
|
|
$units->get(1)->willReturn($unit2); |
200
|
|
|
|
201
|
|
|
$distributor->distribute(0, 2)->willReturn(array(0, 0)); |
202
|
|
|
|
203
|
|
|
$adjustmentsFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, 'Simple tax (0%)', 0, false)->willReturn($taxAdjustment1, $taxAdjustment2); |
204
|
|
|
|
205
|
|
|
$this->apply($order, $zone); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|