Passed
Push — 1.3 ( 0611b0...cd4c68 )
by Morven
02:38
created

LineItemFactoryTest::testCheckStockLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 37
rs 9.536
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\ORM\ValidationException;
7
use SilverCommerce\OrdersAdmin\Model\LineItem;
8
use SilverCommerce\OrdersAdmin\Factory\LineItemFactory;
9
use SilverCommerce\CatalogueAdmin\Model\CatalogueProduct;
10
11
class LineItemFactoryTest extends SapphireTest
12
{
13
    protected static $fixture_file = 'OrdersScaffold.yml';
14
15
    public function testMakeItem()
16
    {
17
        $socks = $this->objFromFixture(CatalogueProduct::class, 'socks');
18
        $basic_item = LineItemFactory::create()
19
            ->setProduct($socks)
20
            ->setQuantity(3)
21
            ->makeItem()
22
            ->getItem();
23
24
        $this->assertNotEmpty($basic_item);
25
        $this->assertEquals("Socks", $basic_item->Title);
26
        $this->assertEquals(3, $basic_item->Quantity);
27
        $this->assertEquals(5.99, $basic_item->BasePrice);
28
        $this->assertEquals(17.97, $basic_item->Total);
29
        $this->assertEquals(CatalogueProduct::class, $basic_item->ProductClass);
30
31
        $notax = $this->objFromFixture(CatalogueProduct::class, 'notax');
32
        $notax_item = LineItemFactory::create()
33
            ->setProduct($notax)
34
            ->setQuantity(5)
35
            ->makeItem()
36
            ->getItem();
37
38
        $this->assertNotEmpty($notax_item);
39
        $this->assertEquals("No Tax Item", $notax_item->Title);
40
        $this->assertEquals(5, $notax_item->Quantity);
41
        $this->assertEquals(6.50, $notax_item->BasePrice);
42
        $this->assertEquals(32.50, $notax_item->Total);
43
        $this->assertEquals(CatalogueProduct::class, $notax_item->ProductClass);
44
45
        $this->expectException(ValidationException::class);
46
        LineItemFactory::create()->makeItem();
47
    }
48
49
    public function testUpdate()
50
    {
51
        $socks = $this->objFromFixture(CatalogueProduct::class, 'socks');
52
        $notax = $this->objFromFixture(CatalogueProduct::class, 'notax');
53
54
        $factory = LineItemFactory::create()
55
            ->setProduct($socks)
56
            ->setQuantity(1)
57
            ->makeItem();
58
    
59
        $this->assertEquals(1, $factory->getItem()->Quantity);
60
        $this->assertEquals(5.99, $factory->getItem()->BasePrice);
61
62
        $factory->setQuantity(3);
63
        $this->assertEquals(1, $factory->getItem()->Quantity);
64
65
        $factory->update();
66
        $this->assertEquals(3, $factory->getItem()->Quantity);
67
68
        $factory->setProduct($notax)->update();
69
        $this->assertEquals(3, $factory->getItem()->Quantity);
70
        $this->assertEquals(6.50, $factory->getItem()->BasePrice);
71
    }
72
73
    public function testFindBestTaxRate()
74
    {
75
        $item = $this->objFromFixture(LineItem::class, 'taxtestableuk');
76
        $estimate = $item->Parent();
77
        $factory = LineItemFactory::create()
78
            ->setItem($item)
79
            ->setParent($estimate);
80
81
        $this->assertEquals(0, $item->TaxPercentage);
82
        $this->assertEquals(20, $factory->findBestTaxRate()->Rate);
83
84
        $estimate->DeliveryCountry = "NZ";
85
        $estimate->DeliveryCounty = "AUK";
86
        $factory->setParent($estimate);
87
88
        $this->assertEquals(5, $factory->findBestTaxRate()->Rate);
89
90
        $estimate->DeliveryCountry = "US";
91
        $estimate->DeliveryCounty = "AL";
92
        $factory->setParent($estimate);
93
94
        $this->assertEquals(0, $factory->findBestTaxRate()->Rate);
95
96
        // Erronious result should return 0
97
        $estimate->DeliveryCountry = "DE";
98
        $estimate->DeliveryCounty = "BE";
99
        $factory->setParent($estimate);
100
101
        $this->assertEquals(0, $factory->findBestTaxRate()->Rate);
102
    }
103
104
    public function testCheckStockLevel()
105
    {
106
        $item = $this->objFromFixture(LineItem::class, 'sockitem');
107
        $factory = LineItemFactory::create()
108
            ->setItem($item);
109
110
        $this->assertTrue($factory->checkStockLevel());
111
112
        $factory
113
            ->setQuantity(5)
114
            ->update();
115
116
        $this->assertTrue($factory->checkStockLevel());
117
118
        $factory
119
            ->setQuantity(9)
120
            ->update();
121
122
        $this->assertTrue($factory->checkStockLevel());
123
124
        $factory
125
            ->setQuantity(10)
126
            ->update();
127
128
        $this->assertTrue($factory->checkStockLevel());
129
130
        $factory
131
            ->setQuantity(15)
132
            ->update();
133
134
        $this->assertFalse($factory->checkStockLevel());
135
136
        $factory
137
            ->setQuantity(20)
138
            ->update();
139
140
        $this->assertFalse($factory->checkStockLevel());
141
    }
142
}
143