|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverShop\Stock\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
6
|
|
|
use SilverShop\Stock\Model\ProductWarehouseStock; |
|
7
|
|
|
use SilverShop\Model\Order; |
|
8
|
|
|
use SilverShop\Stock\Model\ProductWarehouse; |
|
9
|
|
|
use SilverShop\Model\Product\OrderItem; |
|
10
|
|
|
use SilverShop\Page\Product; |
|
11
|
|
|
use SilverStripe\Forms\FieldList; |
|
12
|
|
|
use SilverShop\Model\Variation\Variation; |
|
13
|
|
|
use SilverShop\Model\Variation\OrderItem as VariationOrderItem; |
|
14
|
|
|
|
|
15
|
|
|
class ShopStockTest extends SapphireTest |
|
16
|
|
|
{ |
|
17
|
|
|
protected static $fixture_file = 'fixtures.yml'; |
|
18
|
|
|
|
|
19
|
|
|
private function setStockFor($item, $value) |
|
20
|
|
|
{ |
|
21
|
|
|
$warehouse = $this->objFromFixture(ProductWarehouse::class, 'warehouse'); |
|
22
|
|
|
$data = array( |
|
23
|
|
|
'WarehouseID' => $warehouse->ID, |
|
24
|
|
|
'ProductID' => $item->ID, |
|
25
|
|
|
'ProductClass' => $item->getStockBaseIdentifier() |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$stock = ProductWarehouseStock::get()->filter($data)->first(); |
|
29
|
|
|
|
|
30
|
|
|
if (!$stock) { |
|
31
|
|
|
$stock = new ProductWarehouseStock($data); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$stock->Quantity = $value; |
|
35
|
|
|
$stock->write(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
parent::setUp(); |
|
41
|
|
|
|
|
42
|
|
|
$this->phone = $this->objFromFixture(Product::class, 'phone'); |
|
|
|
|
|
|
43
|
|
|
$this->ball = $this->objFromFixture(Product::class, 'ball'); |
|
|
|
|
|
|
44
|
|
|
$this->ballRedLarge = $this->objFromFixture(Variation::class, 'redlarge'); |
|
|
|
|
|
|
45
|
|
|
$this->ballRedSmall = $this->objFromFixture(Variation::class, 'redsmall'); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$this->mp3 = $this->objFromFixture(Product::class, 'mp3player'); |
|
|
|
|
|
|
48
|
|
|
$this->cup = $this->objFromFixture(Product::class, 'cup'); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
public function testCanPurchaseVariation() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->setStockFor($this->ballRedSmall, 1); |
|
55
|
|
|
$this->assertTrue($this->ball->canPurchase()); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertTrue($this->ballRedSmall->canPurchase()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testGetTotalStockInCarts() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->setStockFor($this->phone, 10); |
|
63
|
|
|
|
|
64
|
|
|
$order = new Order(array( |
|
65
|
|
|
'Status' => 'Cart' |
|
66
|
|
|
)); |
|
67
|
|
|
|
|
68
|
|
|
$order->write(); |
|
69
|
|
|
|
|
70
|
|
|
$orderItem = new OrderItem(array( |
|
71
|
|
|
'ProductID' => $this->phone->ID, |
|
72
|
|
|
'OrderID' => $order->ID, |
|
73
|
|
|
'Quantity' => '5' |
|
74
|
|
|
)); |
|
75
|
|
|
|
|
76
|
|
|
$orderItem->write(); |
|
77
|
|
|
$this->assertEquals(5, $this->phone->getTotalStockInCarts()); |
|
78
|
|
|
|
|
79
|
|
|
// test variations |
|
80
|
|
|
$this->setStockFor($this->ballRedSmall, 5); |
|
81
|
|
|
|
|
82
|
|
|
$orderItem = $orderItem->newClassInstance(VariationOrderItem::class); |
|
83
|
|
|
$orderItem->VariationID = $this->ballRedSmall->ID; |
|
84
|
|
|
$orderItem->write(); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertEquals(5, $this->ballRedSmall->getTotalStockInCarts()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testHasAvailableStockProduct() |
|
90
|
|
|
{ |
|
91
|
|
|
// no stock defined so no. Stock must be opt in |
|
92
|
|
|
$this->assertFalse($this->cup->hasAvailableStock()); |
|
93
|
|
|
$this->setStockFor($this->phone, 10); |
|
94
|
|
|
$this->assertTrue($this->phone->hasAvailableStock(), 'Phone should have 10 stock'); |
|
95
|
|
|
$this->assertFalse($this->phone->hasAvailableStock(15), 'Phone should only have 10 in stock'); |
|
96
|
|
|
|
|
97
|
|
|
// on products will variations, look across variation. |
|
98
|
|
|
$this->setStockFor($this->ballRedLarge, 0); |
|
99
|
|
|
$this->setStockFor($this->ballRedSmall, 1); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertTrue($this->ball->hasAvailableStock()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testCanPurchaseNotEnough() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->setStockFor($this->phone, 0); |
|
107
|
|
|
$this->assertFalse($this->phone->canPurchase(null, 1)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testGetCmsFields() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->assertInstanceOf(FieldList::class, $this->phone->getCMSFields()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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: