Completed
Pull Request — master (#14)
by Matthew
18:13 queued 03:11
created

testGetOrders()   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
        OptionItem::remove_extension(FoxyStripeOptionInventoryManager::class);
56
57
        $object = $this->objFromFixture(TestOption::class, 'one');
58
        $fields = $object->getCMSFields();
59
        $this->assertInstanceOf(FieldList::class, $fields);
60
61
        OptionItem::add_extension(FoxyStripeOptionInventoryManager::class);
62
    }
63
64
    /**
65
     *
66
     */
67
    public function testGetHasInventory()
68
    {
69
        /** @var OptionItem $option */
70
        $option = OptionItem::create();
71
        //$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...
72
73
        $option->ControlInventory = false;
74
        $option->PurchaseLimit = 0;
75
        $this->assertFalse($option->getHasInventory());
76
77
        $option->ControlInventory = true;
78
        $option->PurchaseLimit = 0;
79
        $this->assertFalse($option->getHasInventory());
80
81
        $option->ControlInventory = false;
82
        $option->PurchaseLimit = 10;
83
        $this->assertFalse($option->getHasInventory());
84
85
        $option->ControlInventory = true;
86
        $option->PurchaseLimit = 10;
87
        $this->assertTrue($option->getHasInventory());
88
    }
89
90
    /**
91
     *
92
     */
93
    public function testGetIsOptionAvailable()
94
    {
95
        /** @var OptionItem $option */
96
        $option = OptionItem::create();
97
        $option->ProductOptionGroupID = $this->objFromFixture(OptionGroup::class, 'default')->ID;
98
        $option->write();
99
        //$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...
100
101
        // no inventory control
102
        $option->ControlInventory = false;
103
        $option->PurchaseLimit = 0;
104
        $this->assertTrue($option->getIsOptionAvailable());
105
106
        // inventory control, no inventory
107
        $option->ControlInventory = true;
108
        $option->PurchaseLimit = 0;
109
        $this->assertFalse($option->getIsOptionAvailable());
110
111
        // inventory control, with inventory
112
        $option->ControlInventory = true;
113
        $option->PurchaseLimit = 10;
114
        $this->assertTrue($option->getIsOptionAvailable());
115
116
        $detail = OrderDetail::create();
117
        $detail->Quantity = 10;
118
        $detail->write();
119
        $detail->OrderOptions()->add($option);
120
121
        // inventory control, no inventory left
122
        $option->ControlInventory = true;
123
        $option->PurchaseLimit = 10;
124
        $this->assertFalse($option->getIsOptionAvailable());
125
    }
126
127
    /**
128
     *
129
     */
130
    public function testGetNumberPurchased()
131
    {
132
        /** @var OptionItem $option */
133
        $option = OptionItem::create();
134
        $option->ProductOptionGroupID = $this->objFromFixture(OptionGroup::class, 'default')->ID;
135
        $option->write();
136
        //$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...
137
138
        $this->assertEquals(0, $option->getNumberPurchased());
139
140
        $detail = OrderDetail::create();
141
        $detail->Quantity = 10;
142
        $detail->write();
143
        $detail->OrderOptions()->add($option);
144
145
        $this->assertEquals(10, $option->getNumberPurchased());
146
    }
147
148
    /**
149
     *
150
     */
151
    public function testGetOrders()
152
    {
153
        /** @var OptionItem $option */
154
        $option = OptionItem::create();
155
        $option->ProductOptionGroupID = $this->objFromFixture(OptionGroup::class, 'default')->ID;
156
        $option->write();
157
        //$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...
158
159
        $this->assertEquals(0, $option->getOrders()->Count());
160
161
        $detail = OrderDetail::create();
162
        $detail->Quantity = 10;
163
        $detail->write();
164
        $detail->OrderOptions()->add($option);
165
166
        $this->assertEquals(1, $option->getOrders()->Count());
167
    }
168
}
169