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 setUpOnce() |
10
|
|
|
{ |
11
|
|
|
parent::setUpOnce(); |
12
|
|
|
// clear session |
13
|
|
|
ShoppingCart::singleton()->clear(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
ShopTest::setConfiguration(); //reset config |
20
|
|
|
$this->cart = ShoppingCart::singleton(); |
|
|
|
|
21
|
|
|
$this->product = $this->objFromFixture('Product', 'mp3player'); |
|
|
|
|
22
|
|
|
$this->product->publish('Stage', 'Live'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testAddToCart() |
26
|
|
|
{ |
27
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
|
|
|
|
28
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add another item"); |
|
|
|
|
29
|
|
|
$item = $this->cart->get($this->product); |
30
|
|
|
$this->assertEquals($item->Quantity, 2, "quantity is 2"); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testRemoveFromCart() |
34
|
|
|
{ |
35
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add item"); |
|
|
|
|
36
|
|
|
$this->assertTrue($this->cart->remove($this->product), "item was removed"); |
|
|
|
|
37
|
|
|
$item = $this->cart->get($this->product); |
38
|
|
|
$this->assertFalse($item, "item not in cart"); |
|
|
|
|
39
|
|
|
$this->assertFalse($this->cart->remove($this->product), "try remove non-existent item"); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testSetQuantity() |
43
|
|
|
{ |
44
|
|
|
$this->assertTrue((boolean)$this->cart->setQuantity($this->product, 25), "quantity set"); |
|
|
|
|
45
|
|
|
$item = $this->cart->get($this->product); |
46
|
|
|
$this->assertEquals($item->Quantity, 25, "quantity is 25"); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testClear() |
50
|
|
|
{ |
51
|
|
|
//$this->assertFalse($this->cart->current(),"there is no cart initally"); |
52
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
|
|
|
|
53
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add another item"); |
|
|
|
|
54
|
|
|
$this->assertEquals($this->cart->current()->class, "Order", "there a cart"); |
|
|
|
|
55
|
|
|
$this->assertTrue($this->cart->clear(), "clear the cart"); |
|
|
|
|
56
|
|
|
$this->assertFalse($this->cart->current(), "there is no cart"); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testProductVariations() |
60
|
|
|
{ |
61
|
|
|
$this->loadFixture('silvershop/tests/fixtures/variations.yml'); |
62
|
|
|
$ball1 = $this->objFromFixture('ProductVariation', 'redlarge'); |
63
|
|
|
$ball2 = $this->objFromFixture('ProductVariation', 'redsmall'); |
64
|
|
|
|
65
|
|
|
$this->assertTrue((boolean)$this->cart->add($ball1), "add one item"); |
|
|
|
|
66
|
|
|
$this->assertTrue((boolean)$this->cart->add($ball2), "add another item"); |
|
|
|
|
67
|
|
|
$this->assertTrue($this->cart->remove($ball1), "remove first item"); |
|
|
|
|
68
|
|
|
$this->assertFalse($this->cart->get($ball1), "first item not in cart"); |
|
|
|
|
69
|
|
|
$this->assertNotNull($this->cart->get($ball1), "second item is in cart"); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCartSingleton() |
73
|
|
|
{ |
74
|
|
|
$this->assertTrue((boolean)$this->cart->add($this->product), "add one item"); |
|
|
|
|
75
|
|
|
$order = $this->cart->current(); |
76
|
|
|
|
77
|
|
|
$this->assertEquals($order->ID, ShoppingCart::curr()->ID, "if singleton order ids will match"); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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: