|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ShoppingCartTest extends SapphireTest |
|
4
|
|
|
{ |
|
5
|
|
|
public static $fixture_file = 'silvershop/tests/fixtures/shop.yml'; |
|
6
|
|
|
public static $disable_theme = true; |
|
7
|
|
|
public static $use_draft_site = false; |
|
8
|
|
|
|
|
9
|
|
|
public function setUp() |
|
10
|
|
|
{ |
|
11
|
|
|
parent::setUp(); |
|
12
|
|
|
ShopTest::setConfiguration(); //reset config |
|
13
|
|
|
Config::inst()->update('Order', 'extensions', ['ShoppingCartTest_TestShoppingCartHooksExtension']); |
|
14
|
|
|
|
|
15
|
|
|
ShoppingCart::singleton()->clear(); |
|
16
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::reset(); |
|
17
|
|
|
$this->cart = ShoppingCart::singleton(); |
|
|
|
|
|
|
18
|
|
|
$this->product = $this->objFromFixture('Product', 'mp3player'); |
|
|
|
|
|
|
19
|
|
|
$this->product->publish('Stage', 'Live'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testAddToCart() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
|
|
|
|
|
|
25
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
26
|
|
|
['onStartOrder', 'beforeAdd', 'afterAdd'], |
|
27
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add another item"); |
|
|
|
|
|
|
31
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
32
|
|
|
['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeAdd', 'afterAdd'], |
|
33
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$item = $this->cart->get($this->product); |
|
37
|
|
|
$this->assertEquals($item->Quantity, 2, "quantity is 2"); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testRemoveFromCart() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add item"); |
|
|
|
|
|
|
43
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
44
|
|
|
['onStartOrder', 'beforeAdd', 'afterAdd'], |
|
45
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertTrue($this->cart->remove($this->product), "item was removed"); |
|
|
|
|
|
|
49
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
50
|
|
|
['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeRemove', 'afterRemove'], |
|
51
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
52
|
|
|
); |
|
53
|
|
|
$item = $this->cart->get($this->product); |
|
54
|
|
|
$this->assertFalse($item, "item not in cart"); |
|
|
|
|
|
|
55
|
|
|
$this->assertFalse($this->cart->remove($this->product), "try remove non-existent item"); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testSetQuantity() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->assertTrue((boolean)$this->cart->setQuantity($this->product, 25), "quantity set"); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
63
|
|
|
['onStartOrder', 'beforeSetQuantity', 'afterSetQuantity'], |
|
64
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$item = $this->cart->get($this->product); |
|
68
|
|
|
$this->assertEquals($item->Quantity, 25, "quantity is 25"); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testClear() |
|
73
|
|
|
{ |
|
74
|
|
|
//$this->assertFalse($this->cart->current(),"there is no cart initally"); |
|
75
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
|
|
|
|
|
|
76
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add another item"); |
|
|
|
|
|
|
77
|
|
|
$this->assertEquals($this->cart->current()->class, "Order", "there a cart"); |
|
|
|
|
|
|
78
|
|
|
$this->assertTrue($this->cart->clear(), "clear the cart"); |
|
|
|
|
|
|
79
|
|
|
$this->assertFalse($this->cart->current(), "there is no cart"); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testProductVariations() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->loadFixture('silvershop/tests/fixtures/variations.yml'); |
|
85
|
|
|
$ball1 = $this->objFromFixture('ProductVariation', 'redlarge'); |
|
86
|
|
|
$ball2 = $this->objFromFixture('ProductVariation', 'redsmall'); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertTrue((boolean)$this->cart->add($ball1), "add one item"); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
91
|
|
|
['onStartOrder', 'beforeAdd', 'afterAdd'], |
|
92
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertTrue((boolean)$this->cart->add($ball2), "add another item"); |
|
|
|
|
|
|
96
|
|
|
$this->assertTrue($this->cart->remove($ball1), "remove first item"); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
99
|
|
|
['onStartOrder', 'beforeAdd', 'afterAdd', 'beforeAdd', 'afterAdd', 'beforeRemove', 'afterRemove'], |
|
100
|
|
|
ShoppingCartTest_TestShoppingCartHooksExtension::$stack |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertFalse($this->cart->get($ball1), "first item not in cart"); |
|
|
|
|
|
|
104
|
|
|
$this->assertNotNull($this->cart->get($ball1), "second item is in cart"); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testCartSingleton() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
|
|
|
|
|
|
110
|
|
|
$order = $this->cart->current(); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals($order->ID, ShoppingCart::curr()->ID, "if singleton order ids will match"); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testAddProductWithVariations() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->loadFixture('silvershop/tests/fixtures/variations.yml'); |
|
118
|
|
|
$ball = $this->objFromFixture('Product', 'ball'); |
|
119
|
|
|
$redlarge = $this->objFromFixture('ProductVariation', 'redlarge'); |
|
120
|
|
|
// setting price of variation to zero, so it can't be added to cart. |
|
121
|
|
|
$redlarge->Price = 0; |
|
122
|
|
|
$redlarge->write(); |
|
123
|
|
|
|
|
124
|
|
|
$ball->BasePrice = 0; |
|
125
|
|
|
$ball->write(); |
|
126
|
|
|
|
|
127
|
|
|
$item = $this->cart->add($ball); |
|
128
|
|
|
$this->assertNotNull($item, "Product with variations can be added to cart"); |
|
|
|
|
|
|
129
|
|
|
$this->assertInstanceOf('ProductVariation_OrderItem', $item, 'A variation should be added to cart.'); |
|
|
|
|
|
|
130
|
|
|
$this->assertEquals(20, $item->Buyable()->Price, 'The buyable variation was added'); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testErrorInCartHooks() |
|
134
|
|
|
{ |
|
135
|
|
|
Config::inst()->update('Order', 'extensions', ['ShoppingCartTest_TestShoppingCartErroringHooksExtension']); |
|
136
|
|
|
|
|
137
|
|
|
ShoppingCart::singleton()->clear(); |
|
138
|
|
|
$cart = ShoppingCart::singleton(); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product, 1), "add one item"); |
|
|
|
|
|
|
141
|
|
|
$item = $cart->get($this->product); |
|
142
|
|
|
$this->assertFalse( |
|
|
|
|
|
|
143
|
|
|
(boolean)$this->cart->add($this->product, 1), |
|
144
|
|
|
"Cannot add more than one item, extension will error" |
|
145
|
|
|
); |
|
146
|
|
|
$this->assertEquals($item->Quantity, 1, "quantity is 1"); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
$this->assertTrue((boolean)$cart->setQuantity($this->product, 10), "quantity set"); |
|
|
|
|
|
|
149
|
|
|
$item = $cart->get($this->product); |
|
150
|
|
|
$this->assertEquals($item->Quantity, 10, "quantity is 10"); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
$this->assertFalse((boolean)$cart->setQuantity($this->product, 11), "Cannot set quantity to more than 10 items"); |
|
|
|
|
|
|
153
|
|
|
$item = $cart->get($this->product); |
|
154
|
|
|
$this->assertEquals($item->Quantity, 10, "quantity is 10"); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
class ShoppingCartTest_TestShoppingCartHooksExtension extends Extension implements TestOnly |
|
159
|
|
|
{ |
|
160
|
|
|
public static $stack = []; |
|
161
|
|
|
|
|
162
|
|
|
public static function reset() |
|
163
|
|
|
{ |
|
164
|
|
|
self::$stack = []; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function onStartOrder() |
|
168
|
|
|
{ |
|
169
|
|
|
self::$stack[] = 'onStartOrder'; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function beforeAdd($buyable, $quantity, $filter) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
self::$stack[] = 'beforeAdd'; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function afterAdd($item, $buyable, $quantity, $filter) |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
self::$stack[] = 'afterAdd'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function beforeRemove($buyable, $quantity, $filter) |
|
|
|
|
|
|
183
|
|
|
{ |
|
184
|
|
|
self::$stack[] = 'beforeRemove'; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function afterRemove($buyable, $quantity, $filter) |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
self::$stack[] = 'afterRemove'; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function beforeSetQuantity($buyable, $quantity, $filter) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
self::$stack[] = 'beforeSetQuantity'; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function afterSetQuantity($item, $buyable, $quantity, $filter) |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
|
|
self::$stack[] = 'afterSetQuantity'; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
class ShoppingCartTest_TestShoppingCartErroringHooksExtension extends Extension implements TestOnly |
|
204
|
|
|
{ |
|
205
|
|
|
public function beforeSetQuantity($buyable, $quantity, $filter) |
|
|
|
|
|
|
206
|
|
|
{ |
|
207
|
|
|
if($quantity > 10) { |
|
208
|
|
|
throw new Exception('Invalid quantity'); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function afterAdd($item, $buyable, $quantity, $filter) |
|
|
|
|
|
|
213
|
|
|
{ |
|
214
|
|
|
if($item->Quantity > 1) { |
|
215
|
|
|
throw new Exception('Invalid quantity'); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: