1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Test; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\OptionItem; |
6
|
|
|
use Dynamic\FoxyStripe\Model\Order; |
7
|
|
|
use Dynamic\FoxyStripe\Model\OrderDetail; |
8
|
|
|
use Dynamic\FoxyStripe\Test\TestOnly\TestOption; |
9
|
|
|
use Dynamic\FoxyStripe\Test\TestOnly\TestProduct; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
|
13
|
|
|
class FoxyStripeOptionInventoryManagerTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected static $fixture_file = array( |
19
|
|
|
'../fixtures.yml', |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected static $extra_dataobjects = [ |
26
|
|
|
TestProduct::class, |
27
|
|
|
TestOption::class, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function testUpdateCMSFields() |
34
|
|
|
{ |
35
|
|
|
$object = $this->objFromFixture(TestOption::class, 'one'); |
36
|
|
|
$fields = $object->getCMSFields(); |
37
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
public function testGetHasInventory() |
44
|
|
|
{ |
45
|
|
|
/** @var TestOption $option */ |
46
|
|
|
$option = $this->objFromFixture(TestOption::class, 'one'); |
47
|
|
|
|
48
|
|
|
$option->ControlInventory = false; |
|
|
|
|
49
|
|
|
$option->PurchaseLimit = 0; |
|
|
|
|
50
|
|
|
$this->assertFalse($option->getHasInventory()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$option->ControlInventory = true; |
53
|
|
|
$option->PurchaseLimit = 0; |
54
|
|
|
$this->assertFalse($option->getHasInventory()); |
55
|
|
|
|
56
|
|
|
$option->ControlInventory = false; |
57
|
|
|
$option->PurchaseLimit = 10; |
58
|
|
|
$this->assertFalse($option->getHasInventory()); |
59
|
|
|
|
60
|
|
|
$option->ControlInventory = true; |
61
|
|
|
$option->PurchaseLimit = 10; |
62
|
|
|
$this->assertTrue($option->getHasInventory()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function testGetIsOptionAvailable() |
69
|
|
|
{ |
70
|
|
|
/** @var TestOption $option */ |
71
|
|
|
$option = $this->objFromFixture(TestOption::class, 'one'); |
72
|
|
|
$option->write(); |
73
|
|
|
|
74
|
|
|
// no inventory control |
75
|
|
|
$option->ControlInventory = false; |
|
|
|
|
76
|
|
|
$option->PurchaseLimit = 0; |
|
|
|
|
77
|
|
|
$this->assertTrue($option->getIsOptionAvailable()); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// inventory control, no inventory |
80
|
|
|
$option->ControlInventory = true; |
81
|
|
|
$option->PurchaseLimit = 0; |
82
|
|
|
$this->assertTrue($option->getIsOptionAvailable()); |
83
|
|
|
|
84
|
|
|
// inventory control, with inventory |
85
|
|
|
$option->ControlInventory = true; |
86
|
|
|
$option->PurchaseLimit = 10; |
87
|
|
|
$this->assertTrue($option->getIsOptionAvailable()); |
88
|
|
|
|
89
|
|
|
/** @var OrderDetail $detail */ |
90
|
|
|
$detail = OrderDetail::create(); |
91
|
|
|
$detail->Quantity = 10; |
|
|
|
|
92
|
|
|
$detail->write(); |
93
|
|
|
$detail->OptionItems()->add($option); |
94
|
|
|
|
95
|
|
|
// inventory control, no inventory left |
96
|
|
|
$option->ControlInventory = true; |
97
|
|
|
$option->PurchaseLimit = 10; |
98
|
|
|
$this->assertFalse($option->getIsOptionAvailable()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
|
|
public function testGetNumberPurchased() |
105
|
|
|
{ |
106
|
|
|
/** @var TestOption $option */ |
107
|
|
|
$option = $this->objFromFixture(TestOption::class, 'one'); |
108
|
|
|
$option->write(); |
109
|
|
|
|
110
|
|
|
$this->assertEquals(0, $option->getNumberPurchased()); |
|
|
|
|
111
|
|
|
|
112
|
|
|
/** @var OrderDetail $detail */ |
113
|
|
|
$detail = OrderDetail::create(); |
114
|
|
|
$detail->Quantity = 10; |
|
|
|
|
115
|
|
|
$detail->write(); |
116
|
|
|
$detail->OptionItems()->add($option); |
117
|
|
|
|
118
|
|
|
$this->assertEquals(10, $option->getNumberPurchased()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
*/ |
124
|
|
|
public function testGetOrders() |
125
|
|
|
{ |
126
|
|
|
/** @var TestOption $option */ |
127
|
|
|
$option = $this->objFromFixture(TestOption::class, 'one'); |
128
|
|
|
$option->write(); |
129
|
|
|
|
130
|
|
|
$this->assertEquals(0, $option->getOrders()->Count()); |
|
|
|
|
131
|
|
|
|
132
|
|
|
/** @var OrderDetail $detail */ |
133
|
|
|
$detail = OrderDetail::create(); |
134
|
|
|
$detail->Quantity = 10; |
|
|
|
|
135
|
|
|
$detail->write(); |
136
|
|
|
$detail->OptionItems()->add($option); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(1, $option->getOrders()->Count()); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|