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\ORM\FoxyStripeOptionInventoryManager; |
9
|
|
|
use Dynamic\FoxyStripe\Test\TestOnly\TestOption; |
10
|
|
|
use Dynamic\FoxyStripe\Test\TestOnly\TestProduct; |
11
|
|
|
use SilverStripe\Dev\SapphireTest; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
|
14
|
|
|
class FoxyStripeOptionInventoryManagerTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected static $fixture_file = array( |
20
|
|
|
'../fixtures.yml', |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected static $extra_dataobjects = [ |
27
|
|
|
TestProduct::class, |
28
|
|
|
TestOption::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
OptionItem::add_extension(FoxyStripeOptionInventoryManager::class); |
37
|
|
|
return parent::setUp(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
public function tearDown() |
44
|
|
|
{ |
45
|
|
|
OptionItem::remove_extension(FoxyStripeOptionInventoryManager::class); |
46
|
|
|
parent::tearDown(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
public function testUpdateCMSFields() |
53
|
|
|
{ |
54
|
|
|
$object = $this->objFromFixture(TestOption::class, 'one'); |
55
|
|
|
$fields = $object->getCMSFields(); |
56
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public function testGetHasInventory() |
63
|
|
|
{ |
64
|
|
|
/** @var TestOption $option */ |
65
|
|
|
$option = OptionItem::create(); |
66
|
|
|
//$option = $this->objFromFixture(TestOption::class, 'one'); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$option->ControlInventory = false; |
|
|
|
|
69
|
|
|
$option->PurchaseLimit = 0; |
|
|
|
|
70
|
|
|
$this->assertFalse($option->getHasInventory()); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$option->ControlInventory = true; |
73
|
|
|
$option->PurchaseLimit = 0; |
74
|
|
|
$this->assertFalse($option->getHasInventory()); |
75
|
|
|
|
76
|
|
|
$option->ControlInventory = false; |
77
|
|
|
$option->PurchaseLimit = 10; |
78
|
|
|
$this->assertFalse($option->getHasInventory()); |
79
|
|
|
|
80
|
|
|
$option->ControlInventory = true; |
81
|
|
|
$option->PurchaseLimit = 10; |
82
|
|
|
$this->assertTrue($option->getHasInventory()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function testGetIsOptionAvailable() |
89
|
|
|
{ |
90
|
|
|
/** @var TestOption $option */ |
91
|
|
|
$option = OptionItem::create(); |
92
|
|
|
$option->write(); |
93
|
|
|
//$option = $this->objFromFixture(TestOption::class, 'one'); |
|
|
|
|
94
|
|
|
|
95
|
|
|
// no inventory control |
96
|
|
|
$option->ControlInventory = false; |
|
|
|
|
97
|
|
|
$option->PurchaseLimit = 0; |
|
|
|
|
98
|
|
|
$this->assertTrue($option->getIsOptionAvailable()); |
|
|
|
|
99
|
|
|
|
100
|
|
|
// inventory control, no inventory |
101
|
|
|
$option->ControlInventory = true; |
102
|
|
|
$option->PurchaseLimit = 0; |
103
|
|
|
$this->assertFalse($option->getIsOptionAvailable()); |
104
|
|
|
|
105
|
|
|
// inventory control, with inventory |
106
|
|
|
$option->ControlInventory = true; |
107
|
|
|
$option->PurchaseLimit = 10; |
108
|
|
|
$this->assertTrue($option->getIsOptionAvailable()); |
109
|
|
|
|
110
|
|
|
$detail = OrderDetail::create(); |
111
|
|
|
$detail->Quantity = 10; |
112
|
|
|
$detail->write(); |
113
|
|
|
$detail->OrderOptions()->add($option); |
114
|
|
|
|
115
|
|
|
// inventory control, no inventory left |
116
|
|
|
$option->ControlInventory = true; |
117
|
|
|
$option->PurchaseLimit = 10; |
118
|
|
|
$this->assertFalse($option->getIsOptionAvailable()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
*/ |
124
|
|
|
public function testGetNumberPurchased() |
125
|
|
|
{ |
126
|
|
|
/** @var TestOption $option */ |
127
|
|
|
$option = OptionItem::create(); |
128
|
|
|
$option->write(); |
129
|
|
|
//$option = $this->objFromFixture(TestOption::class, 'one'); |
|
|
|
|
130
|
|
|
|
131
|
|
|
$this->assertEquals(0, $option->getNumberPurchased()); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$detail = OrderDetail::create(); |
134
|
|
|
$detail->Quantity = 10; |
135
|
|
|
$detail->write(); |
136
|
|
|
$detail->OrderOptions()->add($option); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(10, $option->getNumberPurchased()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
*/ |
144
|
|
|
public function testGetOrders() |
145
|
|
|
{ |
146
|
|
|
/** @var TestOption $option */ |
147
|
|
|
$option = OptionItem::create(); |
148
|
|
|
$option->write(); |
149
|
|
|
//$option = $this->objFromFixture(TestOption::class, 'one'); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$this->assertEquals(0, $option->getOrders()->Count()); |
|
|
|
|
152
|
|
|
|
153
|
|
|
$detail = OrderDetail::create(); |
154
|
|
|
$detail->Quantity = 10; |
155
|
|
|
$detail->write(); |
156
|
|
|
$detail->OrderOptions()->add($option); |
157
|
|
|
|
158
|
|
|
$this->assertEquals(1, $option->getOrders()->Count()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.