1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Plane\Shop\Tests; |
4
|
|
|
|
5
|
|
|
use Plane\Shop\Cart; |
6
|
|
|
use Plane\Shop\CartItem; |
7
|
|
|
use OutOfBoundsException; |
8
|
|
|
use Plane\Shop\PaymentInterface; |
9
|
|
|
use Plane\Shop\ShippingInterface; |
10
|
|
|
use Plane\Shop\CartItemCollection; |
11
|
|
|
use Plane\Shop\Discount\EverySecondItemFreeDiscount; |
12
|
|
|
use Plane\Shop\Discount\TotalPriceThresholdDiscount; |
13
|
|
|
|
14
|
|
|
class CartTest extends \PHPUnit\Framework\TestCase |
15
|
|
|
{ |
16
|
|
|
use MoneyTrait; |
17
|
|
|
use CartTrait; |
18
|
|
|
|
19
|
|
|
const CURRENCY = 'USD'; |
20
|
|
|
|
21
|
|
|
protected function setUp(): void |
22
|
|
|
{ |
23
|
|
|
$this->createFirstCartItem(); |
24
|
|
|
$this->createSecondCartItem(); |
25
|
|
|
$this->createCart(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetCurrency() |
29
|
|
|
{ |
30
|
|
|
$this->assertSame(self::CURRENCY, $this->cart->getCurrency()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testGetShipping() |
34
|
|
|
{ |
35
|
|
|
$this->assertInstanceOf(ShippingInterface::class, $this->cart->getShipping()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetPayment() |
39
|
|
|
{ |
40
|
|
|
$this->assertInstanceOf(PaymentInterface::class, $this->cart->getPayment()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testFill() |
44
|
|
|
{ |
45
|
|
|
$cartItemCollection = new CartItemCollection; |
46
|
|
|
$cartItemCollection->addItem($this->firstCartItem); |
47
|
|
|
$cartItemCollection->addItem($this->secondCartItem); |
48
|
|
|
|
49
|
|
|
$cart = new Cart(self::CURRENCY); |
50
|
|
|
$cart->fill($cartItemCollection); |
51
|
|
|
|
52
|
|
|
$this->assertSame([ |
53
|
|
|
1 => $this->firstCartItem, |
54
|
|
|
2 => $this->secondCartItem |
55
|
|
|
], $cart->items()); |
56
|
|
|
|
57
|
|
|
$this->assertTrue(count($cart->items()) == 2); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function testAdd() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->cart->add($this->secondCartItem); |
63
|
|
|
|
64
|
|
|
$this->assertSame([ |
65
|
|
|
1 => $this->firstCartItem, |
66
|
|
|
2 => $this->secondCartItem |
67
|
|
|
], $this->cart->items()); |
68
|
|
|
|
69
|
|
|
$this->assertTrue(count($this->cart->items()) == 2); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGet() |
73
|
|
|
{ |
74
|
|
|
$this->assertSame($this->secondCartItem, $this->cart->get($this->secondCartItem->getId())); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testFailedGet() |
78
|
|
|
{ |
79
|
|
|
$this->expectException(OutOfBoundsException::class); |
80
|
|
|
|
81
|
|
|
$this->cart->get('4'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testUpdate() |
85
|
|
|
{ |
86
|
|
|
$firstAltered = $this->createMock(CartItem::class); |
87
|
|
|
$firstAltered->method('getId')->willReturn(1); |
|
|
|
|
88
|
|
|
$firstAltered->method('getQuantity')->willReturn(5); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$cart = new Cart(self::CURRENCY); |
91
|
|
|
$cart->add($this->firstCartItem); |
92
|
|
|
|
93
|
|
|
$this->assertTrue($cart->itemsQuantity() == 1); |
94
|
|
|
|
95
|
|
|
$cart->update($firstAltered); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->assertTrue($cart->itemsQuantity() == 5); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
View Code Duplication |
public function testRemove() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->cart->remove($this->firstCartItem->getId()); |
103
|
|
|
|
104
|
|
|
$this->assertSame([ |
105
|
|
|
2 => $this->secondCartItem |
106
|
|
|
], $this->cart->items()); |
107
|
|
|
|
108
|
|
|
$this->assertTrue(count($this->cart->items()) == 1); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testFailedRemove() |
112
|
|
|
{ |
113
|
|
|
$this->expectException(OutOfBoundsException::class); |
114
|
|
|
|
115
|
|
|
$this->cart->remove('3'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function testClear() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->cart->clear(); |
121
|
|
|
|
122
|
|
|
$this->assertSame([], $this->cart->items()); |
123
|
|
|
$this->assertTrue(count($this->cart->items()) == 0); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testItemsQuantity() |
127
|
|
|
{ |
128
|
|
|
$this->assertTrue($this->cart->itemsQuantity() == 3); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testWeight() |
132
|
|
|
{ |
133
|
|
|
$this->assertTrue($this->cart->weight() == 9.99); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testTotalNet() |
137
|
|
|
{ |
138
|
|
|
$this->assertTrue($this->getAmount($this->cart->totalNet()) == 11.50); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testTotalGross() |
142
|
|
|
{ |
143
|
|
|
$this->assertTrue($this->getAmount($this->cart->totalGross()) == 14.03); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testTax() |
147
|
|
|
{ |
148
|
|
|
$this->assertTrue($this->getAmount($this->cart->tax()) == 2.53); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testShippingCost() |
152
|
|
|
{ |
153
|
|
|
$this->assertTrue($this->getAmount($this->cart->shippingCost()) == 4.00); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testShippingCostWhenNoShippingSet() |
157
|
|
|
{ |
158
|
|
|
$cart = new Cart(self::CURRENCY); |
159
|
|
|
|
160
|
|
|
$this->assertTrue($this->getAmount($cart->shippingCost()) == 0.00); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testPaymentFee() |
164
|
|
|
{ |
165
|
|
|
$this->assertTrue($this->getAmount($this->cart->paymentFee()) == 5.50); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testPaymentFeeWhenNoPaymentSet() |
169
|
|
|
{ |
170
|
|
|
$cart = new Cart(self::CURRENCY); |
171
|
|
|
|
172
|
|
|
$this->assertTrue($this->getAmount($cart->paymentFee()) == 0.00); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testDiscounts() |
176
|
|
|
{ |
177
|
|
|
$cartDiscount = new TotalPriceThresholdDiscount('Discount description', $this->cart, [ |
178
|
|
|
'treshold' => 14, |
179
|
|
|
'discount' => 0.1 |
180
|
|
|
]); |
181
|
|
|
$this->cart->addDiscount($cartDiscount); |
182
|
|
|
|
183
|
|
|
$this->assertTrue($this->cart->getDiscounts() == [0 => $cartDiscount]); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testTotalAfterDiscounts() |
187
|
|
|
{ |
188
|
|
|
$secondFreeDisount = new EverySecondItemFreeDiscount('Every second product is free', $this->cart, [ |
189
|
|
|
'items' => $this->cart->items() |
190
|
|
|
]); |
191
|
|
|
$this->cart->addDiscount($secondFreeDisount); |
192
|
|
|
|
193
|
|
|
$priceTresholdDiscount = new TotalPriceThresholdDiscount('Discount description', $this->cart, [ |
194
|
|
|
'treshold' => 4, |
195
|
|
|
'discount' => 0.5 |
196
|
|
|
]); |
197
|
|
|
$this->cart->addDiscount($priceTresholdDiscount); |
198
|
|
|
|
199
|
|
|
$this->assertTrue($this->getAmount($this->cart->totalAfterDiscounts()) == 2.13); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.