Completed
Push — master ( f9ba0c...336b1b )
by Kamil
23:42
created

OrderShipmentTaxesByZoneApplicatorSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
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;
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\OrderShipmentTaxesByZoneApplicatorInterface;
23
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
24
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
25
use Sylius\Component\Taxation\Model\TaxRateInterface;
26
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface;
27
28
/**
29
 * @author Mateusz Zalewski <[email protected]>
30
 */
31
class OrderShipmentTaxesByZoneApplicatorSpec extends ObjectBehavior
32
{
33
    function let(
34
        CalculatorInterface $calculator,
35
        AdjustmentFactoryInterface $adjustmentsFactory,
36
        TaxRateResolverInterface $taxRateResolver
37
    ) {
38
        $this->beConstructedWith($calculator, $adjustmentsFactory, $taxRateResolver);
39
    }
40
41
    function it_is_initializable()
42
    {
43
        $this->shouldHaveType('Sylius\Component\Core\Taxation\OrderShipmentTaxesByZoneApplicator');
44
    }
45
46
    function it_implements_order_shipment_taxes_applicator_interface()
47
    {
48
        $this->shouldImplement(OrderShipmentTaxesByZoneApplicatorInterface::class);
49
    }
50
51
    function it_applies_shipment_taxes_on_order_based_on_shipment_adjustments_and_rate(
52
        $adjustmentsFactory,
53
        $calculator,
54
        $taxRateResolver,
55
        AdjustmentInterface $shippingAdjustment,
56
        AdjustmentInterface $shippingTaxAdjustment,
57
        Collection $shippingAdjustments,
58
        OrderInterface $order,
59
        ShipmentInterface $shipment,
60
        ShippingMethodInterface $shippingMethod,
61
        TaxRateInterface $taxRate,
62
        ZoneInterface $zone
63
    ) {
64
        $order->getLastShipment()->willReturn($shipment);
65
        $shipment->getMethod()->willReturn($shippingMethod);
66
        $taxRateResolver->resolve($shippingMethod, array('zone' => $zone))->willReturn($taxRate);
67
68
        $taxRate->getLabel()->willReturn('Simple tax (10%)');
69
        $taxRate->isIncludedInPrice()->willReturn(false);
70
71
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
72
        $shippingAdjustments->isEmpty()->willReturn(false);
73
        $shippingAdjustments->last()->willReturn($shippingAdjustment);
74
        $shippingAdjustment->getAmount()->willReturn(1000);
75
76
        $calculator->calculate(1000, $taxRate)->willReturn(100);
77
78
        $adjustmentsFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, 'Simple tax (10%)', 100, false)->willReturn($shippingTaxAdjustment);
79
        $order->addAdjustment($shippingTaxAdjustment)->shouldBeCalled();
80
81
        $this->apply($order, $zone);
82
    }
83
84
    function it_does_nothing_if_order_has_no_shipment($taxRateResolver, OrderInterface $order, ZoneInterface $zone)
85
    {
86
        $order->getLastShipment()->willReturn(null);
87
        $taxRateResolver->resolve(Argument::any())->shouldNotBeCalled();
88
89
        $this->apply($order, $zone);
90
    }
91
92
    function it_does_nothing_if_tax_rate_cannot_be_resolved(
93
        $taxRateResolver,
94
        Collection $shippingAdjustments,
95
        OrderInterface $order,
96
        ShipmentInterface $shipment,
97
        ShippingMethodInterface $shippingMethod,
98
        ZoneInterface $zone
99
    ) {
100
        $order->getLastShipment()->willReturn($shipment);
101
        $shipment->getMethod()->willReturn($shippingMethod);
102
103
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
104
        $shippingAdjustments->isEmpty()->willReturn(false);
105
106
        $taxRateResolver->resolve($shippingMethod, array('zone' => $zone))->willReturn(null);
107
108
        $this->apply($order, $zone);
109
    }
110
111
    function it_does_nothing_if_order_has_no_shipping_adjustments(
112
        Collection $shippingAdjustments,
113
        OrderInterface $order,
114
        ShipmentInterface $shipment,
115
        ZoneInterface $zone
116
    ) {
117
        $order->getLastShipment()->willReturn($shipment);
118
119
        $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
120
        $shippingAdjustments->isEmpty()->willReturn(true);
121
122
        $shippingAdjustments->last()->shouldNotBeCalled();
123
124
        $this->apply($order, $zone);
125
    }
126
}
127