ShopStockTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 100
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setStockFor() 0 18 2
A setUp() 0 12 1
A testCanPurchaseVariation() 0 7 1
A testGetTotalStockInCarts() 0 28 1
A testHasAvailableStockProduct() 0 14 1
A testCanPurchaseNotEnough() 0 5 1
A testGetCmsFields() 0 4 1
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');
0 ignored issues
show
Bug introduced by
The property phone does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        $this->ball = $this->objFromFixture(Product::class, 'ball');
0 ignored issues
show
Bug introduced by
The property ball does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        $this->ballRedLarge = $this->objFromFixture(Variation::class, 'redlarge');
0 ignored issues
show
Bug introduced by
The property ballRedLarge does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
        $this->ballRedSmall = $this->objFromFixture(Variation::class, 'redsmall');
0 ignored issues
show
Bug introduced by
The property ballRedSmall does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
47
        $this->mp3 = $this->objFromFixture(Product::class, 'mp3player');
0 ignored issues
show
Bug introduced by
The property mp3 does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
        $this->cup = $this->objFromFixture(Product::class, 'cup');
0 ignored issues
show
Bug introduced by
The property cup does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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