Completed
Push — master ( 32b4c2...be6d62 )
by Matthew
22:47 queued 07:45
created

testGetIsProductAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 31
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Test;
4
5
use Dynamic\FoxyStripe\Model\Order;
6
use Dynamic\FoxyStripe\Model\OrderDetail;
7
use Dynamic\FoxyStripe\Test\TestOnly\TestOption;
8
use Dynamic\FoxyStripe\Test\TestOnly\TestProduct;
9
use Dynamic\FoxyStripe\Test\TestOnly\TestProductController;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\Form;
13
14
class FoxyStripeInventoryManagerTest extends SapphireTest
15
{
16
    /**
17
     * @var array
18
     */
19
    protected static $fixture_file = array(
20
        '../fixtures.yml',
21
    );
22
23
    /**
24
     * @var array
25
     */
26
    protected static $extra_dataobjects = [
27
        TestProduct::class,
28
        TestOption::class,
29
    ];
30
31
    /**
32
     * @var array
33
     */
34
    protected static $extra_controllers = [
35
        TestProductController::class,
36
    ];
37
38
    /**
39
     *
40
     */
41
    public function testUpdateCMSFields()
42
    {
43
        $object = $this->objFromFixture(TestProduct::class, 'one');
44
        $fields = $object->getCMSFields();
45
        $this->assertInstanceOf(FieldList::class, $fields);
46
        $this->assertNotNull($fields->dataFieldByName('ControlInventory'));
47
    }
48
49
    /**
50
     *
51
     */
52
    public function testFoxyStripePurchaseForm()
53
    {
54
        /** @var TestProduct $object */
55
        $object = $this->objFromFixture(TestProduct::class, 'one');
56
        /** @var TestProductController $controller */
57
        $controller = TestProductController::create($object);
58
        $form = $controller->PurchaseForm();
59
        $this->assertInstanceOf(Form::class, $form);
60
    }
61
62
    /**
63
     *
64
     */
65
    public function testGetHasInventory()
66
    {
67
        /** @var TestProduct $product */
68
        $product = $this->objFromFixture(TestProduct::class, 'one');
69
70
        $product->ControlInventory = false;
0 ignored issues
show
Bug Best Practice introduced by
The property ControlInventory does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
71
        $product->PurchaseLimit = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property PurchaseLimit does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
72
        $this->assertFalse($product->getHasInventory());
0 ignored issues
show
Bug introduced by
The method getHasInventory() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. 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

72
        $this->assertFalse($product->/** @scrutinizer ignore-call */ getHasInventory());
Loading history...
73
74
        $product->ControlInventory = true;
75
        $product->PurchaseLimit = 0;
76
        $this->assertFalse($product->getHasInventory());
77
78
        $product->ControlInventory = false;
79
        $product->PurchaseLimit = 10;
80
        $this->assertFalse($product->getHasInventory());
81
82
        $product->ControlInventory = true;
83
        $product->PurchaseLimit = 10;
84
        $this->assertTrue($product->getHasInventory());
85
    }
86
87
    /**
88
     *
89
     */
90
    public function testGetIsProductAvailable()
91
    {
92
        /** @var TestProduct $product */
93
        $product = $this->objFromFixture(TestProduct::class, 'one');
94
95
        // no inventory control
96
        $product->ControlInventory = false;
0 ignored issues
show
Bug Best Practice introduced by
The property ControlInventory does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
97
        $product->PurchaseLimit = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property PurchaseLimit does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
98
        $this->assertTrue($product->getIsProductAvailable());
0 ignored issues
show
Bug introduced by
The method getIsProductAvailable() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. 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

98
        $this->assertTrue($product->/** @scrutinizer ignore-call */ getIsProductAvailable());
Loading history...
99
100
        // inventory control, no limit
101
        $product->ControlInventory = true;
102
        $product->PurchaseLimit = 0;
103
        $this->assertTrue($product->getIsProductAvailable());
104
105
        // inventory control, with limit
106
        $product->ControlInventory = true;
107
        $product->PurchaseLimit = 10;
108
        $this->assertTrue($product->getIsProductAvailable());
109
110
        /** @var OrderDetail $detail */
111
        $detail = OrderDetail::create();
112
        $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID;
113
        $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...
114
        $detail->ProductID = $product->ID;
115
        $detail->write();
116
117
        // inventory control, no inventory left
118
        $product->ControlInventory = true;
119
        $product->PurchaseLimit = 10;
120
        $this->assertFalse($product->getIsProductAvailable());
121
    }
122
123
    /**
124
     *
125
     */
126
    public function testGetNumberPurchased()
127
    {
128
        /** @var TestProduct $product */
129
        $product = $this->objFromFixture(TestProduct::class, 'one');
130
131
        $this->assertEquals(0, $product->getNumberPurchased());
0 ignored issues
show
Bug introduced by
The method getNumberPurchased() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. 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

131
        $this->assertEquals(0, $product->/** @scrutinizer ignore-call */ getNumberPurchased());
Loading history...
132
133
        /** @var OrderDetail $detail */
134
        $detail = OrderDetail::create();
135
        $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID;
136
        $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...
137
        $detail->ProductID = $product->ID;
138
        $detail->write();
139
140
        $this->assertEquals(10, $product->getNumberPurchased());
141
    }
142
143
    /**
144
     *
145
     */
146
    public function testGetOrders()
147
    {
148
        /** @var TestProduct $product */
149
        $product = $this->objFromFixture(TestProduct::class, 'one');
150
151
        $this->assertEquals(0, $product->getOrders()->Count());
0 ignored issues
show
Bug introduced by
The method getOrders() does not exist on Dynamic\FoxyStripe\Test\TestOnly\TestProduct. 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

151
        $this->assertEquals(0, $product->/** @scrutinizer ignore-call */ getOrders()->Count());
Loading history...
152
153
        /** @var OrderDetail $detail */
154
        $detail = OrderDetail::create();
155
        $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID;
156
        $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...
157
        $detail->ProductID = $product->ID;
158
        $detail->write();
159
160
        $this->assertEquals(1, $product->getOrders()->count());
161
    }
162
}
163