Completed
Pull Request — master (#14)
by Matthew
28:27 queued 13:27
created

testGetNumberPurchased()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Test;
4
5
use Dynamic\FoxyStripe\Model\OptionGroup;
6
use Dynamic\FoxyStripe\Model\OptionItem;
7
use Dynamic\FoxyStripe\Model\Order;
8
use Dynamic\FoxyStripe\Model\OrderDetail;
9
use Dynamic\FoxyStripe\ORM\FoxyStripeOptionInventoryManager;
10
use Dynamic\FoxyStripe\Test\TestOnly\TestOption;
11
use Dynamic\FoxyStripe\Test\TestOnly\TestProduct;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Forms\FieldList;
14
15
class FoxyStripeOptionInventoryManagerTest extends SapphireTest
16
{
17
    /**
18
     * @var array
19
     */
20
    protected static $fixture_file = array(
21
        '../fixtures.yml',
22
    );
23
24
    /**
25
     * @var array
26
     */
27
    protected static $extra_dataobjects = [
28
        TestProduct::class,
29
        TestOption::class,
30
    ];
31
32
    /**
33
     *
34
     */
35
    public function setUp()
36
    {
37
        OptionItem::add_extension(FoxyStripeOptionInventoryManager::class);
38
        return parent::setUp();
39
    }
40
41
    /**
42
     *
43
     */
44
    public function tearDown()
45
    {
46
        OptionItem::remove_extension(FoxyStripeOptionInventoryManager::class);
47
        parent::tearDown();
48
    }
49
50
    /**
51
     *
52
     */
53
    public function testUpdateCMSFields()
54
    {
55
        /** @var OptionItem $option */
56
        $option = OptionItem::create();
57
        $fields = $option->getCMSFields();
58
        $this->assertInstanceOf(FieldList::class, $fields);
59
    }
60
61
    /**
62
     *
63
     */
64
    public function testGetHasInventory()
65
    {
66
        /** @var OptionItem $option */
67
        $option = OptionItem::create();
68
        //$option = $this->objFromFixture(TestOption::class, 'one');
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
69
70
        $option->ControlInventory = false;
71
        $option->PurchaseLimit = 0;
72
        $this->assertFalse($option->getHasInventory());
73
74
        $option->ControlInventory = true;
75
        $option->PurchaseLimit = 0;
76
        $this->assertFalse($option->getHasInventory());
77
78
        $option->ControlInventory = false;
79
        $option->PurchaseLimit = 10;
80
        $this->assertFalse($option->getHasInventory());
81
82
        $option->ControlInventory = true;
83
        $option->PurchaseLimit = 10;
84
        $this->assertTrue($option->getHasInventory());
85
    }
86
87
    /**
88
     *
89
     */
90
    public function testGetIsOptionAvailable()
91
    {
92
        /** @var OptionItem $option */
93
        $option = OptionItem::create();
94
        $option->ProductOptionGroupID = $this->objFromFixture(OptionGroup::class, 'default')->ID;
95
        $option->write();
96
        //$option = $this->objFromFixture(TestOption::class, 'one');
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
97
98
        // no inventory control
99
        $option->ControlInventory = false;
100
        $option->PurchaseLimit = 0;
101
        $this->assertTrue($option->getIsOptionAvailable());
102
103
        // inventory control, no inventory
104
        $option->ControlInventory = true;
105
        $option->PurchaseLimit = 0;
106
        $this->assertFalse($option->getIsOptionAvailable());
107
108
        // inventory control, with inventory
109
        $option->ControlInventory = true;
110
        $option->PurchaseLimit = 10;
111
        $this->assertTrue($option->getIsOptionAvailable());
112
113
        $detail = OrderDetail::create();
114
        $detail->Quantity = 10;
115
        $detail->write();
116
        $detail->OrderOptions()->add($option);
117
118
        // inventory control, no inventory left
119
        $option->ControlInventory = true;
120
        $option->PurchaseLimit = 10;
121
        $this->assertFalse($option->getIsOptionAvailable());
122
    }
123
124
    /**
125
     *
126
     */
127
    public function testGetNumberPurchased()
128
    {
129
        /** @var OptionItem $option */
130
        $option = OptionItem::create();
131
        $option->ProductOptionGroupID = $this->objFromFixture(OptionGroup::class, 'default')->ID;
132
        $option->write();
133
        //$option = $this->objFromFixture(TestOption::class, 'one');
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
134
135
        $this->assertEquals(0, $option->getNumberPurchased());
136
137
        $detail = OrderDetail::create();
138
        $detail->Quantity = 10;
139
        $detail->write();
140
        $detail->OrderOptions()->add($option);
141
142
        $this->assertEquals(10, $option->getNumberPurchased());
143
    }
144
145
    /**
146
     *
147
     */
148
    public function testGetOrders()
149
    {
150
        /** @var OptionItem $option */
151
        $option = OptionItem::create();
152
        $option->ProductOptionGroupID = $this->objFromFixture(OptionGroup::class, 'default')->ID;
153
        $option->write();
154
        //$option = $this->objFromFixture(TestOption::class, 'one');
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
155
156
        $this->assertEquals(0, $option->getOrders()->Count());
157
158
        $detail = OrderDetail::create();
159
        $detail->Quantity = 10;
160
        $detail->write();
161
        $detail->OrderOptions()->add($option);
162
163
        $this->assertEquals(1, $option->getOrders()->Count());
164
    }
165
}
166