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\Processor; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
15
|
|
|
use PhpSpec\ObjectBehavior; |
16
|
|
|
use Prophecy\Argument; |
17
|
|
|
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface; |
18
|
|
|
use Sylius\Component\Addressing\Model\AddressInterface; |
19
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
20
|
|
|
use Sylius\Component\Core\Model\AdjustmentInterface; |
21
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
22
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
23
|
|
|
use Sylius\Component\Core\Provider\ZoneProviderInterface; |
24
|
|
|
use Sylius\Component\Core\Taxation\Exception\UnsupportedTaxCalculationStrategyException; |
25
|
|
|
use Sylius\Component\Core\Taxation\Processor\OrderTaxesProcessor; |
26
|
|
|
use Sylius\Component\Core\Taxation\Processor\OrderTaxesProcessorInterface; |
27
|
|
|
use Sylius\Component\Core\Taxation\Strategy\TaxCalculationStrategyInterface; |
28
|
|
|
use Sylius\Component\Registry\PrioritizedServiceRegistryInterface; |
29
|
|
|
use Zend\Stdlib\PriorityQueue; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @mixin OrderTaxesProcessor |
33
|
|
|
* |
34
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
35
|
|
|
* @author Mark McKelvie <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class OrderTaxesProcessorSpec extends ObjectBehavior |
38
|
|
|
{ |
39
|
|
|
function let( |
40
|
|
|
ZoneProviderInterface $defaultTaxZoneProvider, |
41
|
|
|
ZoneMatcherInterface $zoneMatcher, |
42
|
|
|
PrioritizedServiceRegistryInterface $strategyRegistry |
43
|
|
|
) { |
44
|
|
|
$this->beConstructedWith($defaultTaxZoneProvider, $zoneMatcher, $strategyRegistry); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function it_is_initializable() |
48
|
|
|
{ |
49
|
|
|
$this->shouldHaveType('Sylius\Component\Core\Taxation\Processor\OrderTaxesProcessor'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function it_implements_Sylius_taxation_processor_interface() |
53
|
|
|
{ |
54
|
|
|
$this->shouldImplement(OrderTaxesProcessorInterface::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function it_processes_taxes_using_a_supported_tax_calculation_strategy( |
58
|
|
|
ZoneMatcherInterface $zoneMatcher, |
59
|
|
|
AddressInterface $address, |
60
|
|
|
\Iterator $itemsIterator, |
61
|
|
|
Collection $items, |
62
|
|
|
OrderInterface $order, |
63
|
|
|
OrderItemInterface $orderItem, |
64
|
|
|
ZoneInterface $zone, |
65
|
|
|
PrioritizedServiceRegistryInterface $strategyRegistry, |
66
|
|
|
PriorityQueue $strategies, |
67
|
|
|
\Iterator $strategiesIterator, |
68
|
|
|
TaxCalculationStrategyInterface $strategyOne, |
69
|
|
|
TaxCalculationStrategyInterface $strategyTwo |
70
|
|
|
) { |
71
|
|
|
$order->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
72
|
|
|
$order->getItems()->willReturn($items); |
73
|
|
|
$order->isEmpty()->willReturn(false); |
74
|
|
|
|
75
|
|
|
$items->count()->willReturn(1); |
76
|
|
|
$items->getIterator()->willReturn($itemsIterator); |
77
|
|
|
$itemsIterator->rewind()->shouldBeCalled(); |
78
|
|
|
$itemsIterator->valid()->willReturn(true, false)->shouldBeCalled(); |
79
|
|
|
$itemsIterator->current()->willReturn($orderItem); |
80
|
|
|
$itemsIterator->next()->shouldBeCalled(); |
81
|
|
|
|
82
|
|
|
$orderItem->removeAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
83
|
|
|
|
84
|
|
|
$order->getShippingAddress()->willReturn($address); |
85
|
|
|
$zoneMatcher->match($address)->willReturn($zone); |
86
|
|
|
|
87
|
|
|
$strategyRegistry->all()->willReturn($strategies); |
88
|
|
|
|
89
|
|
|
$strategies->count()->willReturn(2); |
90
|
|
|
$strategies->getIterator()->willReturn($strategiesIterator); |
91
|
|
|
$strategiesIterator->rewind()->shouldBeCalled(); |
92
|
|
|
$strategiesIterator->valid()->willReturn(true, true, false)->shouldBeCalled(); |
93
|
|
|
$strategiesIterator->current()->willReturn($strategyOne, $strategyTwo); |
94
|
|
|
$strategiesIterator->next()->shouldBeCalled(); |
95
|
|
|
|
96
|
|
|
$strategyOne->supports($order, $zone)->willReturn(false)->shouldBeCalled(); |
97
|
|
|
$strategyOne->applyTaxes($order, $zone)->shouldNotBeCalled(); |
98
|
|
|
|
99
|
|
|
$strategyTwo->supports($order, $zone)->willReturn(true)->shouldBeCalled(); |
100
|
|
|
$strategyTwo->applyTaxes($order, $zone)->shouldBeCalled(); |
101
|
|
|
|
102
|
|
|
$this->shouldNotThrow(new UnsupportedTaxCalculationStrategyException())->duringProcess($order); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
function it_throws_an_exception_if_there_are_no_supported_tax_calculation_strategies( |
106
|
|
|
ZoneMatcherInterface $zoneMatcher, |
107
|
|
|
AddressInterface $address, |
108
|
|
|
\Iterator $itemsIterator, |
109
|
|
|
Collection $items, |
110
|
|
|
OrderInterface $order, |
111
|
|
|
OrderItemInterface $orderItem, |
112
|
|
|
ZoneInterface $zone, |
113
|
|
|
PrioritizedServiceRegistryInterface $strategyRegistry, |
114
|
|
|
PriorityQueue $strategies, |
115
|
|
|
\Iterator $strategiesIterator, |
116
|
|
|
TaxCalculationStrategyInterface $strategy |
117
|
|
|
) { |
118
|
|
|
$order->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
119
|
|
|
$order->getItems()->willReturn($items); |
120
|
|
|
$order->isEmpty()->willReturn(false); |
121
|
|
|
|
122
|
|
|
$items->count()->willReturn(1); |
123
|
|
|
$items->getIterator()->willReturn($itemsIterator); |
124
|
|
|
$itemsIterator->rewind()->shouldBeCalled(); |
125
|
|
|
$itemsIterator->valid()->willReturn(true, false)->shouldBeCalled(); |
126
|
|
|
$itemsIterator->current()->willReturn($orderItem); |
127
|
|
|
$itemsIterator->next()->shouldBeCalled(); |
128
|
|
|
|
129
|
|
|
$orderItem->removeAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
130
|
|
|
|
131
|
|
|
$order->getShippingAddress()->willReturn($address); |
132
|
|
|
$zoneMatcher->match($address)->willReturn($zone); |
133
|
|
|
|
134
|
|
|
$strategyRegistry->all()->willReturn($strategies); |
135
|
|
|
|
136
|
|
|
$strategies->count()->willReturn(1); |
137
|
|
|
$strategies->getIterator()->willReturn($strategiesIterator); |
138
|
|
|
$strategiesIterator->rewind()->shouldBeCalled(); |
139
|
|
|
$strategiesIterator->valid()->willReturn(true, false)->shouldBeCalled(); |
140
|
|
|
$strategiesIterator->current()->willReturn($strategy); |
141
|
|
|
$strategiesIterator->next()->shouldBeCalled(); |
142
|
|
|
|
143
|
|
|
$strategy->supports($order, $zone)->willReturn(false)->shouldBeCalled(); |
144
|
|
|
$strategy->applyTaxes($order, $zone)->shouldNotBeCalled(); |
145
|
|
|
|
146
|
|
|
$this->shouldThrow(new UnsupportedTaxCalculationStrategyException())->duringProcess($order); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function it_does_not_process_taxes_if_there_is_no_order_item(OrderInterface $order) |
150
|
|
|
{ |
151
|
|
|
$order->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
152
|
|
|
$order->getItems()->willReturn([]); |
153
|
|
|
$order->isEmpty()->willReturn(true); |
154
|
|
|
|
155
|
|
|
$order->getShippingAddress()->shouldNotBeCalled(); |
156
|
|
|
|
157
|
|
|
$this->process($order); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
function it_does_not_process_taxes_if_there_is_no_tax_zone( |
161
|
|
|
ZoneProviderInterface $defaultTaxZoneProvider, |
162
|
|
|
ZoneMatcherInterface $zoneMatcher, |
163
|
|
|
AddressInterface $address, |
164
|
|
|
\Iterator $itemsIterator, |
165
|
|
|
Collection $items, |
166
|
|
|
OrderInterface $order, |
167
|
|
|
OrderItemInterface $orderItem, |
168
|
|
|
PrioritizedServiceRegistryInterface $strategyRegistry |
169
|
|
|
) { |
170
|
|
|
$order->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
171
|
|
|
$order->getItems()->willReturn($items); |
172
|
|
|
$order->isEmpty()->willReturn(false); |
173
|
|
|
|
174
|
|
|
$items->count()->willReturn(1); |
175
|
|
|
$items->getIterator()->willReturn($itemsIterator); |
176
|
|
|
$itemsIterator->rewind()->shouldBeCalled(); |
177
|
|
|
$itemsIterator->valid()->willReturn(true, false)->shouldBeCalled(); |
178
|
|
|
$itemsIterator->current()->willReturn($orderItem); |
179
|
|
|
$itemsIterator->next()->shouldBeCalled(); |
180
|
|
|
|
181
|
|
|
$orderItem->removeAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT)->shouldBeCalled(); |
182
|
|
|
|
183
|
|
|
$order->getShippingAddress()->willReturn($address); |
184
|
|
|
$zoneMatcher->match($address)->willReturn(null); |
185
|
|
|
$defaultTaxZoneProvider->getZone()->willReturn(null); |
186
|
|
|
|
187
|
|
|
$strategyRegistry->all()->shouldNotBeCalled(); |
188
|
|
|
|
189
|
|
|
$this->process($order); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|