1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Inventory\Test\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Inventory\Test\TestOnly\Page\TestProduct; |
6
|
|
|
use Dynamic\Foxy\Inventory\Test\TestOnly\Page\TestProductController; |
7
|
|
|
use Dynamic\Foxy\Orders\Model\Order; |
8
|
|
|
use Dynamic\Foxy\Orders\Model\OrderDetail; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\Form; |
12
|
|
|
|
13
|
|
|
class ProductInventoryManagerTest 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
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected static $extra_controllers = [ |
33
|
|
|
TestProductController::class, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
public function testUpdateCMSFields() |
40
|
|
|
{ |
41
|
|
|
$object = $this->objFromFixture(TestProduct::class, 'one'); |
42
|
|
|
$fields = $object->getCMSFields(); |
43
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
44
|
|
|
$this->assertNotNull($fields->dataFieldByName('ControlInventory')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
|
|
public function testFoxyStripePurchaseForm() |
51
|
|
|
{ |
52
|
|
|
/** @var TestProduct $object */ |
53
|
|
|
$object = $this->objFromFixture(TestProduct::class, 'one'); |
54
|
|
|
/** @var TestProductController $controller */ |
55
|
|
|
$controller = TestProductController::create($object); |
56
|
|
|
$form = $controller->AddToCartForm(); |
|
|
|
|
57
|
|
|
$this->assertInstanceOf(Form::class, $form); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
*/ |
63
|
|
|
public function testGetHasInventory() |
64
|
|
|
{ |
65
|
|
|
/** @var TestProduct $product */ |
66
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'one'); |
67
|
|
|
$product->ControlInventory = false; |
|
|
|
|
68
|
|
|
$product->PurchaseLimit = 0; |
|
|
|
|
69
|
|
|
$this->assertFalse($product->getHasInventory()); |
|
|
|
|
70
|
|
|
$product->ControlInventory = true; |
71
|
|
|
$product->PurchaseLimit = 0; |
72
|
|
|
$this->assertFalse($product->getHasInventory()); |
73
|
|
|
$product->ControlInventory = false; |
74
|
|
|
$product->PurchaseLimit = 10; |
75
|
|
|
$this->assertFalse($product->getHasInventory()); |
76
|
|
|
$product->ControlInventory = true; |
77
|
|
|
$product->PurchaseLimit = 10; |
78
|
|
|
$this->assertTrue($product->getHasInventory()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
|
|
public function testGetIsProductAvailable() |
85
|
|
|
{ |
86
|
|
|
/** @var TestProduct $product */ |
87
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'one'); |
88
|
|
|
// no inventory control |
89
|
|
|
$product->ControlInventory = false; |
|
|
|
|
90
|
|
|
$product->PurchaseLimit = 0; |
|
|
|
|
91
|
|
|
$this->assertTrue($product->getIsProductAvailable()); |
|
|
|
|
92
|
|
|
// inventory control, no limit |
93
|
|
|
$product->ControlInventory = true; |
94
|
|
|
$product->PurchaseLimit = 0; |
95
|
|
|
$this->assertTrue($product->getIsProductAvailable()); |
96
|
|
|
// inventory control, with limit |
97
|
|
|
$product->ControlInventory = true; |
98
|
|
|
$product->PurchaseLimit = 10; |
99
|
|
|
$this->assertTrue($product->getIsProductAvailable()); |
100
|
|
|
/** @var OrderDetail $detail */ |
101
|
|
|
$detail = OrderDetail::create(); |
102
|
|
|
$detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; |
103
|
|
|
$detail->Quantity = 10; |
104
|
|
|
$detail->ProductID = $product->ID; |
105
|
|
|
$detail->write(); |
106
|
|
|
// inventory control, no inventory left |
107
|
|
|
$product->ControlInventory = true; |
108
|
|
|
$product->PurchaseLimit = 10; |
109
|
|
|
$this->assertFalse($product->getIsProductAvailable()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
*/ |
115
|
|
|
public function testGetNumberPurchased() |
116
|
|
|
{ |
117
|
|
|
/** @var TestProduct $product */ |
118
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'one'); |
119
|
|
|
$this->assertEquals(0, $product->getNumberPurchased()); |
|
|
|
|
120
|
|
|
/** @var OrderDetail $detail */ |
121
|
|
|
$detail = OrderDetail::create(); |
122
|
|
|
$detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; |
123
|
|
|
$detail->Quantity = 10; |
124
|
|
|
$detail->ProductID = $product->ID; |
125
|
|
|
$detail->write(); |
126
|
|
|
$this->assertEquals(10, $product->getNumberPurchased()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* |
131
|
|
|
*/ |
132
|
|
|
public function testGetOrders() |
133
|
|
|
{ |
134
|
|
|
/** @var TestProduct $product */ |
135
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'one'); |
136
|
|
|
$this->assertEquals(0, $product->getOrders()->Count()); |
|
|
|
|
137
|
|
|
/** @var OrderDetail $detail */ |
138
|
|
|
$detail = OrderDetail::create(); |
139
|
|
|
$detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; |
140
|
|
|
$detail->Quantity = 10; |
141
|
|
|
$detail->ProductID = $product->ID; |
142
|
|
|
$detail->write(); |
143
|
|
|
$this->assertEquals(1, $product->getOrders()->count()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|