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