|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package shop |
|
5
|
|
|
* @subpackage tests |
|
6
|
|
|
*/ |
|
7
|
|
|
class OrderModifierTest extends FunctionalTest |
|
8
|
|
|
{ |
|
9
|
|
|
public static $fixture_file = 'silvershop/tests/fixtures/shop.yml'; |
|
10
|
|
|
public static $disable_theme = true; |
|
11
|
|
|
public static $use_draft_site = true; |
|
12
|
|
|
|
|
13
|
|
|
protected $extraDataObjects = array('OrderModifierTest_TestModifier'); |
|
14
|
|
|
|
|
15
|
|
|
public function setUp() |
|
16
|
|
|
{ |
|
17
|
|
|
parent::setUp(); |
|
18
|
|
|
ShopTest::setConfiguration(); |
|
19
|
|
|
Order::config()->modifiers = array( |
|
20
|
|
|
"FlatTaxModifier", |
|
21
|
|
|
); |
|
22
|
|
|
FlatTaxModifier::config()->rate = 0.25; |
|
23
|
|
|
FlatTaxModifier::config()->name = "GST"; |
|
24
|
|
|
|
|
25
|
|
|
$this->mp3player = $this->objFromFixture('Product', 'mp3player'); |
|
|
|
|
|
|
26
|
|
|
$this->socks = $this->objFromFixture('Product', 'socks'); |
|
|
|
|
|
|
27
|
|
|
$this->mp3player->publish('Stage', 'Live'); |
|
28
|
|
|
$this->socks->publish('Stage', 'Live'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testModifierCalculation() |
|
32
|
|
|
{ |
|
33
|
|
|
$order = $this->createOrder(); |
|
34
|
|
|
$this->assertEquals(510, $order->calculate(), "Total with 25% tax"); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
//remove modifiers |
|
37
|
|
|
Order::config()->modifiers = null; |
|
38
|
|
|
$order->calculate(); |
|
39
|
|
|
$this->assertEquals(408, $order->calculate(), "Total with no modification"); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testModifierFailure() |
|
43
|
|
|
{ |
|
44
|
|
|
if (!ShopTools::DBConn()->supportsTransactions()) { |
|
45
|
|
|
$this->markTestSkipped( |
|
|
|
|
|
|
46
|
|
|
'The Database doesn\'t support transactions.' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
Config::inst()->update('Order', 'modifiers', array( |
|
51
|
|
|
'OrderModifierTest_TestModifier' |
|
52
|
|
|
)); |
|
53
|
|
|
$order = $this->createOrder(); |
|
54
|
|
|
$order->calculate(); |
|
55
|
|
|
$order->write(); |
|
56
|
|
|
|
|
57
|
|
|
// 408 from items + 10 from modifier + 25% from tax |
|
58
|
|
|
$this->assertEquals('522.5', $order->Total); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals(array('10', '104.5'), $order->Modifiers()->column('Amount')); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
OrderModifierTest_TestModifier::$value = 42; |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
// Calculate will now fail! |
|
66
|
|
|
$order->calculate(); |
|
67
|
|
|
} catch (Exception $e){} |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// reload order from DB |
|
70
|
|
|
$order = Order::get()->byID($order->ID); |
|
71
|
|
|
|
|
72
|
|
|
// Order Total should not have changed |
|
73
|
|
|
$this->assertEquals('522.5', $order->Total); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
76
|
|
|
array('10', '104.5'), |
|
77
|
|
|
$order->Modifiers()->column('Amount'), |
|
78
|
|
|
'Modifiers aren\'t allowed to change upon failure' |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function createOrder() |
|
83
|
|
|
{ |
|
84
|
|
|
$order = new Order(); |
|
85
|
|
|
$order->write(); |
|
86
|
|
|
$item1a = $this->mp3player->createItem(2); |
|
87
|
|
|
$item1a->write(); |
|
88
|
|
|
$order->Items()->add($item1a); |
|
|
|
|
|
|
89
|
|
|
$item1b = $this->socks->createItem(); |
|
90
|
|
|
$item1b->write(); |
|
91
|
|
|
$order->Items()->add($item1b); |
|
|
|
|
|
|
92
|
|
|
return $order; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
class OrderModifierTest_TestModifier extends OrderModifier implements TestOnly |
|
97
|
|
|
{ |
|
98
|
|
|
public static $value = 10; |
|
99
|
|
|
private $willFail = false; |
|
100
|
|
|
|
|
101
|
|
|
public function value($incoming) |
|
102
|
|
|
{ |
|
103
|
|
|
if (self::$value === 42) { |
|
104
|
|
|
$this->willFail = true; |
|
105
|
|
|
} |
|
106
|
|
|
return self::$value; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function onAfterWrite() |
|
110
|
|
|
{ |
|
111
|
|
|
parent::onAfterWrite(); |
|
112
|
|
|
if ($this->willFail) { |
|
113
|
|
|
user_error('Modifier failure!'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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: