Completed
Push — master ( 0f74a6...eca3e7 )
by Kamil
20:39
created

OrderShipmentTaxesApplicatorSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 1
nc 1
cc 1
eloc 5
nop 3
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\Applicator;
13
14
use Doctrine\Common\Collections\Collection;
15
use PhpSpec\ObjectBehavior;
16
use Prophecy\Argument;
17
use Sylius\Component\Addressing\Model\ZoneInterface;
18
use Sylius\Component\Core\Model\AdjustmentInterface;
19
use Sylius\Component\Core\Model\OrderInterface;
20
use Sylius\Component\Core\Model\ShipmentInterface;
21
use Sylius\Component\Core\Model\ShippingMethodInterface;
22
use Sylius\Component\Core\Taxation\Applicator\OrderShipmentTaxesApplicator;
23
use Sylius\Component\Core\Taxation\Applicator\OrderTaxesApplicatorInterface;
24
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
25
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
26
use Sylius\Component\Taxation\Model\TaxRateInterface;
27
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface;
28
29
/**
30
 * @mixin OrderShipmentTaxesApplicator
31
 *
32
 * @author Mateusz Zalewski <[email protected]>
33
 * @author Mark McKelvie <[email protected]>
34
 */
35
class OrderShipmentTaxesApplicatorSpec extends ObjectBehavior
36
{
37
    function let(
38
        CalculatorInterface $calculator,
39
        AdjustmentFactoryInterface $adjustmentsFactory,
40
        TaxRateResolverInterface $taxRateResolver
41
    ) {
42
        $this->beConstructedWith($calculator, $adjustmentsFactory, $taxRateResolver);
43
    }
44
45
    function it_is_initializable()
46
    {
47
        $this->shouldHaveType('Sylius\Component\Core\Taxation\Applicator\OrderShipmentTaxesApplicator');
48
    }
49
50
    function it_implements_order_shipment_taxes_applicator_interface()
51
    {
52
        $this->shouldImplement(OrderTaxesApplicatorInterface::class);
53
    }
54
55
    function it_applies_shipment_taxes_on_order_based_on_shipment_adjustments_and_rate(
56
        $adjustmentsFactory,
57
        $calculator,
58
        $taxRateResolver,
59
        AdjustmentInterface $shippingAdjustment,
60
        AdjustmentInterface $shippingTaxAdjustment,
61
        Collection $shippingAdjustments,
62
        OrderInterface $order,
63
        ShipmentInterface $shipment,
64
        ShippingMethodInterface $shippingMethod,
65
        TaxRateInterface $taxRate,
66
        ZoneInterface $zone
67
    ) {
68
        $order->getLastShipment()->willReturn($shipment);
69
        $shipment->getMethod()->willReturn($shippingMethod);
70
        $taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn($taxRate);
71
72
        $taxRate->getLabel()->willReturn('Simple tax (10%)');
73
        $taxRate->isIncludedInPrice()->willReturn(false);
74
75
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
76
        $shippingAdjustments->isEmpty()->willReturn(false);
77
        $shippingAdjustments->last()->willReturn($shippingAdjustment);
78
        $shippingAdjustment->getAmount()->willReturn(1000);
79
80
        $calculator->calculate(1000, $taxRate)->willReturn(100);
81
82
        $adjustmentsFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, 'Simple tax (10%)', 100, false)->willReturn($shippingTaxAdjustment);
83
        $order->addAdjustment($shippingTaxAdjustment)->shouldBeCalled();
84
85
        $this->apply($order, $zone);
86
    }
87
88
    function it_does_nothing_if_there_are_no_shipment_taxes_on_order(
89
        $adjustmentsFactory,
90
        $calculator,
91
        $taxRateResolver,
92
        AdjustmentInterface $shippingAdjustment,
93
        Collection $shippingAdjustments,
94
        OrderInterface $order,
95
        ShipmentInterface $shipment,
96
        ShippingMethodInterface $shippingMethod,
97
        TaxRateInterface $taxRate,
98
        ZoneInterface $zone
99
    ) {
100
        $order->getLastShipment()->willReturn($shipment);
101
        $shipment->getMethod()->willReturn($shippingMethod);
102
        $taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn($taxRate);
103
104
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
105
        $shippingAdjustments->isEmpty()->willReturn(false);
106
        $shippingAdjustments->last()->willReturn($shippingAdjustment);
107
        $shippingAdjustment->getAmount()->willReturn(1000);
108
109
        $calculator->calculate(1000, $taxRate)->willReturn(0);
110
111
        $adjustmentsFactory->createWithData(Argument::cetera())->shouldNotBeCalled();
112
        $order->addAdjustment(Argument::any())->shouldNotBeCalled();
113
114
        $this->apply($order, $zone);
115
    }
116
117
    function it_does_nothing_if_order_has_no_shipment($taxRateResolver, OrderInterface $order, ZoneInterface $zone)
118
    {
119
        $order->getLastShipment()->willReturn(null);
120
        $taxRateResolver->resolve(Argument::any())->shouldNotBeCalled();
121
122
        $this->apply($order, $zone);
123
    }
124
125
    function it_does_nothing_if_tax_rate_cannot_be_resolved(
126
        $taxRateResolver,
127
        Collection $shippingAdjustments,
128
        OrderInterface $order,
129
        ShipmentInterface $shipment,
130
        ShippingMethodInterface $shippingMethod,
131
        ZoneInterface $zone
132
    ) {
133
        $order->getLastShipment()->willReturn($shipment);
134
        $shipment->getMethod()->willReturn($shippingMethod);
135
136
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
137
        $shippingAdjustments->isEmpty()->willReturn(false);
138
139
        $taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn(null);
140
141
        $this->apply($order, $zone);
142
    }
143
144
    function it_does_nothing_if_order_has_no_shipping_adjustments(
145
        Collection $shippingAdjustments,
146
        OrderInterface $order,
147
        ShipmentInterface $shipment,
148
        ZoneInterface $zone
149
    ) {
150
        $order->getLastShipment()->willReturn($shipment);
151
152
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
153
        $shippingAdjustments->isEmpty()->willReturn(true);
154
155
        $shippingAdjustments->last()->shouldNotBeCalled();
156
157
        $this->apply($order, $zone);
158
    }
159
}
160