forked from litecommerce/core
/
OptionSurcharge.php
267 lines (200 loc) · 7.89 KB
/
OptionSurcharge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
// vim: set ts=4 sw=4 sts=4 et:
/**
* LiteCommerce
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to licensing@litecommerce.com so we can send you a copy immediately.
*
* @category LiteCommerce
* @package Tests
* @subpackage Classes
* @author Creative Development LLC <info@cdev.ru>
* @copyright Copyright (c) 2010 Creative Development LLC <info@cdev.ru>. All rights reserved
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.litecommerce.com/
* @see ____file_see____
* @since 1.0.0
*/
class XLite_Tests_Module_CDev_ProductOptions_Model_OptionSurcharge extends XLite_Tests_TestCase
{
protected $product;
protected $testGroup = array(
'name' => 'Test name',
'fullname' => 'Test full name',
'orderby' => 10,
'type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::GROUP_TYPE,
'view_type' => XLite\Module\CDev\ProductOptions\Model\OptionGroup::SELECT_VISIBLE,
'cols' => 11,
'rows' => 12,
'enabled' => true,
);
protected $testOption = array(
'name' => 'Test option name',
'orderby' => 11,
'enabled' => true,
);
protected function setUp()
{
parent::setUp();
\XLite\Core\Database::getEM()->clear();
}
public function testCreate()
{
$group = $this->getTestGroup();
$list = $group->getOptions()->get(2)->getSurcharges();
$this->assertEquals(1, count($list), 'Check surcharges count');
$this->assertEquals('price', $list[0]->getType(), 'Check type');
$this->assertEquals(10, $list[0]->getModifier(), 'Check modifier');
$this->assertEquals('$', $list[0]->getModifierType(), 'Check modifier type');
$this->assertNotNull($list[0]->getSurchargeId(), 'Check id');
}
public function testUpdate()
{
$group = $this->getTestGroup();
$list = $group->getOptions()->get(2)->getSurcharges();
$list[0]->setModifier(20);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(20, $list[0]->getModifier(), 'Check modifier');
}
public function testDelete()
{
$group = $this->getTestGroup();
$list = $group->getOptions()->get(2)->getSurcharges();
foreach ($list as $i) {
\XLite\Core\Database::getEM()->remove($i);
}
$list->clear();
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(0, count($list), 'Check surcharges list');
}
public function testGetSign()
{
$group = $this->getTestGroup();
$s = $group->getOptions()->get(2)->getSurcharges()->get(0);
$this->assertEquals('+', $s->getSign(), '+ sign');
$s->setModifier(-10);
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals('−', $s->getSign(), '- sign');
$s->setModifier(0);
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals('', $s->getSign(), '0 sign');
}
public function testisEmpty()
{
$group = $this->getTestGroup();
$s = $group->getOptions()->get(2)->getSurcharges()->get(0);
$this->assertFalse($s->isEmpty(), 'not empty');
$s->setModifier(0);
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertTrue($s->isEmpty(), 'empty');
$s->setModifier(0.01);
$s->setModifierType('%');
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertTrue($s->isEmpty(), 'empty #2');
}
public function testGetAbsoluteValue()
{
$group = $this->getTestGroup();
$s = $group->getOptions()->get(2)->getSurcharges()->get(0);
$this->assertEquals(10, $s->getAbsoluteValue(), '10');
$s->setModifier(0);
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(0, $s->getAbsoluteValue(), '0');
$s->setModifier(0.01);
$s->setModifierType('%');
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(0, $s->getAbsoluteValue(), '0 #2');
$s->setType('weight');
$s->setModifier(20);
$s->setModifierType('$');
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(20, $s->getAbsoluteValue(), '20');
$s->setModifier(0.1);
$s->setModifierType('%');
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(0, $s->getAbsoluteValue(), '0 #3');
}
public function testGetPositiveAbsoluteValue()
{
$group = $this->getTestGroup();
$s = $group->getOptions()->get(2)->getSurcharges()->get(0);
$s->setModifier(-10);
\XLite\Core\Database::getEM()->persist($s);
\XLite\Core\Database::getEM()->flush();
$this->assertEquals(10, $s->getPositiveAbsoluteValue(), '10');
}
protected function getProduct()
{
if (!isset($this->product)) {
$list = \XLite\Core\Database::getRepo('XLite\Model\Product')->findFrame(1, 1);
$this->product = array_shift($list);
foreach ($list as $p) {
$p->detach();
}
if (!$this->product) {
$this->fail('Can not find enabled product');
}
// Clean option groups
foreach ($this->product->getOptionGroups() as $group) {
\XLite\Core\Database::getEM()->remove($group);
}
$this->product->getOptionGroups()->clear();
\XLite\Core\Database::getEM()->flush();
}
return $this->product;
}
protected function getTestGroup()
{
$group = new XLite\Module\CDev\ProductOptions\Model\OptionGroup();
$group->setProduct($this->getProduct());
$this->getProduct()->addOptionGroups($group);
$group->map($this->testGroup);
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
$option->setName('o2');
$option = new XLite\Module\CDev\ProductOptions\Model\Option();
$option->setGroup($group);
$group->addOptions($option);
$option->map($this->testOption);
$option->setName('o3');
$s = new XLite\Module\CDev\ProductOptions\Model\OptionSurcharge();
$s->setOption($option);
$s->setType('price');
$s->setModifier(10);
$s->setModifierType('$');
$option->addSurcharges($s);
$e = new XLite\Module\CDev\ProductOptions\Model\OptionException();
$e->setOption($option);
$e->setExceptionId(
\XLite\Core\Database::getRepo('XLite\Module\CDev\ProductOptions\Model\OptionException')
->getNextExceptionId()
);
$option->addExceptions($e);
\XLite\Core\Database::getEM()->persist($group);
\XLite\Core\Database::getEM()->flush();
return $group;
}
}