Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

AdjustmentSpec::its_description_is_mutable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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\Order\Model;
13
14
use PhpSpec\ObjectBehavior;
15
use Sylius\Component\Order\Model\AdjustableInterface;
16
use Sylius\Component\Order\Model\AdjustmentInterface;
17
use Sylius\Component\Order\Model\OrderInterface;
18
use Sylius\Component\Order\Model\OrderItemInterface;
19
use Sylius\Component\Order\Model\OrderItemUnitInterface;
20
21
/**
22
 * @author Paweł Jędrzejewski <[email protected]>
23
 * @author Michał Marcinkowski <[email protected]>
24
 */
25
class AdjustmentSpec extends ObjectBehavior
26
{
27
    function it_is_initializable()
28
    {
29
        $this->shouldHaveType('Sylius\Component\Order\Model\Adjustment');
30
    }
31
32
    function it_implements_Sylius_adjustment_interface()
33
    {
34
        $this->shouldImplement(AdjustmentInterface::class);
35
    }
36
37
    function it_has_no_id_by_default()
38
    {
39
        $this->getId()->shouldReturn(null);
40
    }
41
42
    function it_does_not_belong_to_an_adjustable_by_default()
43
    {
44
        $this->getAdjustable()->shouldReturn(null);
45
    }
46
47
    function it_allows_assigning_itself_to_an_adjustable(OrderInterface $order, OrderItemInterface $orderItem)
48
    {
49
        $order->addAdjustment($this->getWrappedObject())->shouldBeCalled();
50
        $this->setAdjustable($order);
51
        $this->getAdjustable()->shouldReturn($order);
52
53
        $order->removeAdjustment($this->getWrappedObject())->shouldBeCalled();
54
        $orderItem->addAdjustment($this->getWrappedObject())->shouldBeCalled();
55
        $this->setAdjustable($orderItem);
56
        $this->getAdjustable()->shouldReturn($orderItem);
57
    }
58
59
    function it_allows_detaching_itself_from_an_adjustable(
60
        OrderInterface $order,
61
        OrderItemInterface $orderItem,
62
        OrderItemUnitInterface $orderItemUnit
63
    ) {
64
        $order->addAdjustment($this->getWrappedObject())->shouldBeCalled();
65
        $this->setAdjustable($order);
66
        $this->getAdjustable()->shouldReturn($order);
67
68
        $order->removeAdjustment($this->getWrappedObject())->shouldBeCalled();
69
        $this->setAdjustable(null);
70
        $this->getAdjustable()->shouldReturn(null);
71
72
        $orderItem->addAdjustment($this->getWrappedObject())->shouldBeCalled();
73
        $this->setAdjustable($orderItem);
74
        $this->getAdjustable()->shouldReturn($orderItem);
75
76
        $orderItem->removeAdjustment($this->getWrappedObject())->shouldBeCalled();
77
        $this->setAdjustable(null);
78
        $this->getAdjustable()->shouldReturn(null);
79
80
        $orderItemUnit->addAdjustment($this->getWrappedObject())->shouldBeCalled();
81
        $this->setAdjustable($orderItemUnit);
82
        $this->getAdjustable()->shouldReturn($orderItemUnit);
83
84
        $orderItemUnit->removeAdjustment($this->getWrappedObject())->shouldBeCalled();
85
        $this->setAdjustable(null);
86
        $this->getAdjustable()->shouldReturn(null);
87
    }
88
89
    function it_throws_exception_during_not_supported_adjustable_class_set(AdjustableInterface $adjustable)
90
    {
91
        $this->shouldThrow(\InvalidArgumentException::class)->during('setAdjustable', [$adjustable]);
92
    }
93
94
    function it_throws_exception_during_adjustable_change_on_locked_adjustment(
95
        OrderItemInterface $orderItem,
96
        OrderItemInterface $otherOrderItem
97
    ) {
98
        $this->setAdjustable($orderItem);
99
        $this->lock();
100
        $this->shouldThrow(\LogicException::class)->during('setAdjustable', [null]);
101
        $this->shouldThrow(\LogicException::class)->during('setAdjustable', [$otherOrderItem]);
102
    }
103
104
    function it_has_no_type_by_default()
105
    {
106
        $this->getType()->shouldReturn(null);
107
    }
108
109
    function its_type_is_mutable()
110
    {
111
        $this->setType('some type');
112
        $this->getType()->shouldReturn('some type');
113
    }
114
115
    function it_has_no_label_by_default()
116
    {
117
        $this->getLabel()->shouldReturn(null);
118
    }
119
120
    function its_label_is_mutable()
121
    {
122
        $this->setLabel('Clothing tax (12%)');
123
        $this->getLabel()->shouldReturn('Clothing tax (12%)');
124
    }
125
126
    function it_has_amount_equal_to_0_by_default()
127
    {
128
        $this->getAmount()->shouldReturn(0);
129
    }
130
131
    function its_amount_is_mutable()
132
    {
133
        $this->setAmount(399);
134
        $this->getAmount()->shouldReturn(399);
135
    }
136
137
    function it_recalculates_adjustments_on_adjustable_entity_on_amount_change(
138
        OrderInterface $order,
139
        OrderItemInterface $orderItem,
140
        OrderItemUnitInterface $orderItemUnit
141
    ) {
142
        $order->addAdjustment($this->getWrappedObject())->shouldBeCalled();
143
        $this->setAdjustable($order);
144
        $order->recalculateAdjustmentsTotal()->shouldBeCalled();
145
        $this->setAmount(200);
146
147
        $order->removeAdjustment($this->getWrappedObject())->shouldBeCalled();
148
        $orderItem->addAdjustment($this->getWrappedObject())->shouldBeCalled();
149
        $this->setAdjustable($orderItem);
150
        $orderItem->recalculateAdjustmentsTotal()->shouldBeCalled();
151
        $this->setAmount(300);
152
153
        $orderItem->removeAdjustment($this->getWrappedObject())->shouldBeCalled();
154
        $orderItemUnit->addAdjustment($this->getWrappedObject())->shouldBeCalled();
155
        $this->setAdjustable($orderItemUnit);
156
        $orderItemUnit->recalculateAdjustmentsTotal()->shouldBeCalled();
157
        $this->setAmount(400);
158
    }
159
160
    function it_does_not_recalculate_adjustments_on_adjustable_entity_on_amount_change_when_adjustment_is_neutral(
161
        OrderItemUnitInterface $orderItemUnit
162
    ) {
163
        $orderItemUnit->addAdjustment($this->getWrappedObject())->shouldBeCalled();
164
        $this->setAdjustable($orderItemUnit);
165
        $orderItemUnit->recalculateAdjustmentsTotal()->shouldBeCalledTimes(1);
166
        $this->setNeutral(true);
167
        $this->setAmount(400);
168
    }
169
170
    function its_amount_should_accept_only_integer()
171
    {
172
        $this->setAmount(4498);
173
        $this->getAmount()->shouldBeInteger();
174
        $this->shouldThrow(\InvalidArgumentException::class)->during('setAmount', [44.98 * 100]);
175
        $this->shouldThrow(\InvalidArgumentException::class)->during('setAmount', ['4498']);
176
        $this->shouldThrow(\InvalidArgumentException::class)->during('setAmount', [round(44.98 * 100)]);
177
        $this->shouldThrow(\InvalidArgumentException::class)->during('setAmount', [array(4498)]);
178
        $this->shouldThrow(\InvalidArgumentException::class)->during('setAmount', [new \stdClass()]);
179
    }
180
181
    function it_is_not_neutral_by_default()
182
    {
183
        $this->shouldNotBeNeutral();
184
    }
185
186
    function its_neutrality_is_mutable()
187
    {
188
        $this->shouldNotBeNeutral();
189
        $this->setNeutral(true);
190
        $this->shouldBeNeutral();
191
    }
192
193
    function it_recalculate_adjustments_on_adjustable_entity_on_neutral_change(OrderItemUnitInterface $orderItemUnit)
194
    {
195
        $orderItemUnit->addAdjustment($this->getWrappedObject())->shouldBeCalled();
196
        $this->setAdjustable($orderItemUnit);
197
        $orderItemUnit->recalculateAdjustmentsTotal()->shouldBeCalled();
198
        $this->setNeutral(true);
199
    }
200
201
    function it_does_not_recalculate_adjustments_on_adjustable_entity_when_neutral_set_to_current_value(
202
        OrderInterface $order
203
    ) {
204
        $order->addAdjustment($this->getWrappedObject())->shouldBeCalled();
205
        $this->setAdjustable($order);
206
        $order->recalculateAdjustmentsTotal()->shouldNotBeCalled();
207
        $this->setNeutral(false);
208
    }
209
210
    function it_is_a_charge_if_amount_is_lesser_than_0()
211
    {
212
        $this->setAmount(-499);
213
        $this->shouldBeCharge();
214
215
        $this->setAmount(699);
216
        $this->shouldNotBeCharge();
217
    }
218
219
    function it_is_a_credit_if_amount_is_greater_than_0()
220
    {
221
        $this->setAmount(2999);
222
        $this->shouldBeCredit();
223
224
        $this->setAmount(-299);
225
        $this->shouldNotBeCredit();
226
    }
227
228
    function it_initializes_creation_date_by_default()
229
    {
230
        $this->getCreatedAt()->shouldHaveType('DateTime');
231
    }
232
233
    function it_has_no_last_update_date_by_default()
234
    {
235
        $this->getUpdatedAt()->shouldReturn(null);
236
    }
237
}
238