Completed
Pull Request — master (#14)
by Matthew
14:42
created

testGetHasInventory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Test;
4
5
use Dynamic\FoxyStripe\Model\OptionItem;
6
use Dynamic\FoxyStripe\Model\Order;
7
use Dynamic\FoxyStripe\Model\OrderDetail;
8
use Dynamic\FoxyStripe\Test\TestOnly\TestOption;
9
use Dynamic\FoxyStripe\Test\TestOnly\TestProduct;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Forms\FieldList;
12
13
class FoxyStripeOptionInventoryManagerTest extends SapphireTest
14
{
15
    /**
16
     * @var array
17
     */
18
    protected static $fixture_file = array(
19
        '../fixtures.yml',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    protected static $extra_dataobjects = [
26
        TestProduct::class,
27
        TestOption::class,
28
    ];
29
30
    /**
31
     *
32
     */
33
    public function testUpdateCMSFields()
34
    {
35
        $object = $this->objFromFixture(TestOption::class, 'one');
36
        $fields = $object->getCMSFields();
37
        $this->assertInstanceOf(FieldList::class, $fields);
38
    }
39
40
    /**
41
     *
42
     */
43
    public function testGetHasInventory()
44
    {
45
        /** @var TestOption $option */
46
        $option = $this->objFromFixture(TestOption::class, 'one');
47
48
        $option->ControlInventory = false;
0 ignored issues
show
Bug Best Practice introduced by
The property ControlInventory does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __set, consider adding a @property annotation.
Loading history...
49
        $option->PurchaseLimit = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property PurchaseLimit does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __set, consider adding a @property annotation.
Loading history...
50
        $this->assertFalse($option->getHasInventory());
0 ignored issues
show
Bug introduced by
The method getHasInventory() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $this->assertFalse($option->/** @scrutinizer ignore-call */ getHasInventory());
Loading history...
51
52
        $option->ControlInventory = true;
53
        $option->PurchaseLimit = 0;
54
        $this->assertFalse($option->getHasInventory());
55
56
        $option->ControlInventory = false;
57
        $option->PurchaseLimit = 10;
58
        $this->assertFalse($option->getHasInventory());
59
60
        $option->ControlInventory = true;
61
        $option->PurchaseLimit = 10;
62
        $this->assertTrue($option->getHasInventory());
63
    }
64
65
    /**
66
     *
67
     */
68
    public function testGetIsOptionAvailable()
69
    {
70
        /** @var TestOption $option */
71
        $option = $this->objFromFixture(TestOption::class, 'one');
72
        $option->write();
73
74
        // no inventory control
75
        $option->ControlInventory = false;
0 ignored issues
show
Bug Best Practice introduced by
The property ControlInventory does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __set, consider adding a @property annotation.
Loading history...
76
        $option->PurchaseLimit = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property PurchaseLimit does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __set, consider adding a @property annotation.
Loading history...
77
        $this->assertTrue($option->getIsOptionAvailable());
0 ignored issues
show
Bug introduced by
The method getIsOptionAvailable() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        $this->assertTrue($option->/** @scrutinizer ignore-call */ getIsOptionAvailable());
Loading history...
78
79
        // inventory control, no limit
80
        $option->ControlInventory = true;
81
        $option->PurchaseLimit = 0;
82
        $this->assertTrue($option->getIsOptionAvailable());
83
84
        // inventory control, with limit
85
        $option->ControlInventory = true;
86
        $option->PurchaseLimit = 10;
87
        $this->assertTrue($option->getIsOptionAvailable());
88
89
        /** @var OrderDetail $detail */
90
        $detail = OrderDetail::create();
91
        $detail->Quantity = 10;
0 ignored issues
show
Documentation Bug introduced by
It seems like 10 of type integer is incompatible with the declared type SilverStripe\ORM\FieldType\DBInt of property $Quantity.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
        $detail->write();
93
        $detail->OptionItems()->add($option);
94
95
        // inventory control, no inventory left
96
        $option->ControlInventory = true;
97
        $option->PurchaseLimit = 10;
98
        $this->assertFalse($option->getIsOptionAvailable());
99
    }
100
101
    /**
102
     *
103
     */
104
    public function testGetNumberPurchased()
105
    {
106
        /** @var TestOption $option */
107
        $option = $this->objFromFixture(TestOption::class, 'one');
108
        $option->write();
109
110
        $this->assertEquals(0, $option->getNumberPurchased());
0 ignored issues
show
Bug introduced by
The method getNumberPurchased() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        $this->assertEquals(0, $option->/** @scrutinizer ignore-call */ getNumberPurchased());
Loading history...
111
112
        /** @var OrderDetail $detail */
113
        $detail = OrderDetail::create();
114
        $detail->Quantity = 10;
0 ignored issues
show
Documentation Bug introduced by
It seems like 10 of type integer is incompatible with the declared type SilverStripe\ORM\FieldType\DBInt of property $Quantity.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
115
        $detail->write();
116
        $detail->OptionItems()->add($option);
117
118
        $this->assertEquals(10, $option->getNumberPurchased());
119
    }
120
121
    /**
122
     *
123
     */
124
    public function testGetOrders()
125
    {
126
        /** @var TestOption $option */
127
        $option = $this->objFromFixture(TestOption::class, 'one');
128
        $option->write();
129
130
        $this->assertEquals(0, $option->getOrders()->Count());
0 ignored issues
show
Bug introduced by
The method getOrders() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestOption. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        $this->assertEquals(0, $option->/** @scrutinizer ignore-call */ getOrders()->Count());
Loading history...
131
132
        /** @var OrderDetail $detail */
133
        $detail = OrderDetail::create();
134
        $detail->Quantity = 10;
0 ignored issues
show
Documentation Bug introduced by
It seems like 10 of type integer is incompatible with the declared type SilverStripe\ORM\FieldType\DBInt of property $Quantity.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
135
        $detail->write();
136
        $detail->OptionItems()->add($option);
137
138
        echo 'Detail Count: ' . print_r(OrderDetail::get()->Count(), true);
139
140
        $this->assertEquals(1, $option->getOrders()->Count());
141
    }
142
}
143