1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class DMSDocumentCartTest contains all the tests for {@link DMSDocumentCart} |
5
|
|
|
*/ |
6
|
|
|
class DMSDocumentCartTest extends SapphireTest |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected static $fixture_file = 'DMSDocumentCartTest.yml'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var DMSDocumentCart |
12
|
|
|
*/ |
13
|
|
|
protected $cart; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
$this->cart = singleton('DMSDocumentCart'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @expectedException DMSDocumentCartException |
23
|
|
|
* @expectedExceptionMessage Backend must implement DMSCartBackendInterface! |
24
|
|
|
*/ |
25
|
|
|
public function testConstructorThrowsExceptionWhenProvidedBackendDoesNotImplementInterface() |
26
|
|
|
{ |
27
|
|
|
DMSDocumentCart::create(new DMSDocument); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function testAddItem() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
33
|
|
|
|
34
|
|
|
$item = DMSRequestItem::create(); |
35
|
|
|
$item->setDocument($doc)->setQuantity(2); |
|
|
|
|
36
|
|
|
$this->cart->addItem($item); |
37
|
|
|
$this->assertFalse($this->cart->isCartEmpty()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testEmptyCart() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
43
|
|
|
|
44
|
|
|
$item = DMSRequestItem::create(); |
45
|
|
|
$item->setDocument($doc)->setQuantity(18); |
|
|
|
|
46
|
|
|
$this->cart->addItem($item); |
47
|
|
|
$this->assertFalse($this->cart->isCartEmpty()); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->cart->emptyCart(); |
50
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetBackend() |
54
|
|
|
{ |
55
|
|
|
$this->assertInstanceOf('DMSSessionBackend', $this->cart->getBackend()); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetBackURL() |
59
|
|
|
{ |
60
|
|
|
$url = 'TestURL'; |
61
|
|
|
$this->cart->setBackUrl($url); |
62
|
|
|
$this->assertEquals($url, $this->cart->getBackUrl()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testGetItem() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
68
|
|
|
|
69
|
|
|
$item = DMSRequestItem::create(); |
70
|
|
|
$item->setDocument($doc)->setQuantity(2); |
|
|
|
|
71
|
|
|
$this->cart->addItem($item); |
72
|
|
|
|
73
|
|
|
$this->assertEquals($doc, $this->cart->getItem($doc->ID)->getDocument()); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetItems() |
77
|
|
|
{ |
78
|
|
|
$item = DMSRequestItem::create(); |
79
|
|
|
$item->setDocument($this->objFromFixture('DMSDocument', 'doc1'))->setQuantity(2); |
|
|
|
|
80
|
|
|
$this->cart->addItem($item); |
81
|
|
|
$this->assertCount(1, $this->cart->getItems()); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$item2 = DMSRequestItem::create(); |
84
|
|
|
$item2->setDocument($this->objFromFixture('DMSDocument', 'limited_supply'))->setQuantity(2); |
|
|
|
|
85
|
|
|
$this->cart->addItem($item2); |
86
|
|
|
$this->assertCount(2, $this->cart->getItems()); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// Edge case handling for when a document is deleted in the background |
89
|
|
|
$item2->getDocument()->delete(); |
90
|
|
|
$this->assertCount(1, $this->cart->getItems()); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetReceiverInfo() |
94
|
|
|
{ |
95
|
|
|
$info = array('Name' => 'Jane'); |
96
|
|
|
$this->cart->setReceiverInfo($info); |
97
|
|
|
$this->assertEquals($info, $this->cart->getReceiverInfo()); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testIsCartEmpty() |
101
|
|
|
{ |
102
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
103
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
104
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2); |
|
|
|
|
105
|
|
|
$this->cart->addItem($item); |
106
|
|
|
$this->assertFalse($this->cart->isCartEmpty()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testRemoveItem() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
112
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
113
|
|
|
|
114
|
|
|
$item = DMSRequestItem::create(); |
115
|
|
|
$item->setDocument($doc)->setQuantity(2); |
|
|
|
|
116
|
|
|
$this->cart->addItem($item); |
117
|
|
|
$this->assertFalse($this->cart->isCartEmpty()); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$this->cart->removeItem($item); |
120
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
View Code Duplication |
public function testRemoveItemByID() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
126
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
127
|
|
|
|
128
|
|
|
$item = DMSRequestItem::create(); |
129
|
|
|
$item->setDocument($doc)->setQuantity(2); |
|
|
|
|
130
|
|
|
$this->cart->addItem($item); |
131
|
|
|
$this->assertFalse($this->cart->isCartEmpty()); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->cart->removeItemByID($doc->ID); |
134
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testUpdateItemQuantity() |
138
|
|
|
{ |
139
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
140
|
|
|
|
141
|
|
|
// Test Update |
142
|
|
|
$item = DMSRequestItem::create(); |
143
|
|
|
$item->setDocument($doc)->setQuantity(2); |
|
|
|
|
144
|
|
|
$this->cart->addItem($item); |
145
|
|
|
$this->assertEquals(2, $this->cart->getItem($doc->ID)->getQuantity()); |
|
|
|
|
146
|
|
|
|
147
|
|
|
$this->cart->updateItemQuantity($item->getItemID(), 17); |
148
|
|
|
$this->assertEquals(19, $this->cart->getItem($doc->ID)->getQuantity()); |
|
|
|
|
149
|
|
|
|
150
|
|
|
// Test Add |
151
|
|
|
$this->cart->updateItemQuantity($doc->ID, 16); |
152
|
|
|
$this->assertEquals(35, $this->cart->getItem($doc->ID)->getQuantity()); |
|
|
|
|
153
|
|
|
|
154
|
|
|
// Test Deduct |
155
|
|
|
$this->cart->updateItemQuantity($doc->ID, -16); |
156
|
|
|
$this->assertEquals(19, $this->cart->getItem($doc->ID)->getQuantity()); |
|
|
|
|
157
|
|
|
|
158
|
|
|
// Test deduct all items that it removes it |
159
|
|
|
$this->cart->updateItemQuantity($doc->ID, -19); |
160
|
|
|
$this->assertTrue($this->cart->isCartEmpty()); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testSaveSubmission() |
164
|
|
|
{ |
165
|
|
|
$controller = DMSCheckoutController::create(); |
166
|
|
|
|
167
|
|
|
// Form for use later |
168
|
|
|
$form = $controller->DMSDocumentRequestForm(); |
169
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
170
|
|
|
|
171
|
|
|
// Add some an item to the cart to assert later that its empty |
172
|
|
|
$item = DMSRequestItem::create($doc)->setQuantity(15); |
173
|
|
|
$controller->getCart()->addItem($item); |
174
|
|
|
|
175
|
|
|
$submissionID = $controller->getCart()->saveSubmission($form); |
176
|
|
|
$submission = DMSDocumentCartSubmission::get()->byID($submissionID); |
177
|
|
|
$this->assertEquals(15, $submission->Items()->first()->Quantity); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
View Code Duplication |
public function testIsInCart() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$doc = $this->objFromFixture('DMSDocument', 'doc1'); |
184
|
|
|
$item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2); |
|
|
|
|
185
|
|
|
$this->assertFalse($this->cart->isInCart($item->getItemId())); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$this->cart->addItem($item); |
188
|
|
|
$this->assertTrue($this->cart->isInCart($item->getItemId())); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Ensure that the cart contents are hashed an used for the cart summary cache key |
193
|
|
|
*/ |
194
|
|
|
public function testGetCartSummaryCacheKey() |
195
|
|
|
{ |
196
|
|
|
$item = DMSRequestItem::create($this->objFromFixture('DMSDocument', 'doc1'))->setQuantity(2); |
197
|
|
|
$item2 = DMSRequestItem::create($this->objFromFixture('DMSDocument', 'limited_supply'))->setQuantity(2); |
198
|
|
|
|
199
|
|
|
$this->cart->addItem($item)->addItem($item2); |
200
|
|
|
|
201
|
|
|
$hash = md5(serialize($this->cart->getItems())); |
202
|
|
|
$this->assertContains($hash, $this->cart->getCartSummaryCacheKey()); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.