Passed
Push — master ( 48c474...2a8ddd )
by Jason
02:28
created

testFoxyStripePurchaseForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Foxy\Inventory\Test\Extension;
4
5
use Dynamic\Foxy\Inventory\Test\TestOnly\Page\TestProduct;
6
use Dynamic\Foxy\Inventory\Test\TestOnly\Page\TestProductController;
7
use Dynamic\Foxy\Orders\Model\Order;
8
use Dynamic\Foxy\Orders\Model\OrderDetail;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\Form;
12
13
class ProductInventoryManagerTest 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
    ];
28
29
    /**
30
     * @var array
31
     */
32
    protected static $extra_controllers = [
33
        TestProductController::class,
34
    ];
35
36
    /**
37
     *
38
     */
39
    public function testUpdateCMSFields()
40
    {
41
        $object = $this->objFromFixture(TestProduct::class, 'one');
42
        $fields = $object->getCMSFields();
43
        $this->assertInstanceOf(FieldList::class, $fields);
44
        $this->assertNotNull($fields->dataFieldByName('ControlInventory'));
45
    }
46
47
    /**
48
     *
49
     */
50
    public function testFoxyStripePurchaseForm()
51
    {
52
        /** @var TestProduct $object */
53
        $object = $this->objFromFixture(TestProduct::class, 'one');
54
        /** @var TestProductController $controller */
55
        $controller = TestProductController::create($object);
56
        $form = $controller->AddToCartForm();
0 ignored issues
show
Bug introduced by
The method AddToCartForm() does not exist on Dynamic\Foxy\Inventory\T...e\TestProductController. 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

56
        /** @scrutinizer ignore-call */ 
57
        $form = $controller->AddToCartForm();
Loading history...
57
        $this->assertInstanceOf(Form::class, $form);
58
    }
59
60
    /**
61
     *
62
     */
63
    public function testGetHasInventory()
64
    {
65
        /** @var TestProduct $product */
66
        $product = $this->objFromFixture(TestProduct::class, 'one');
67
        $product->ControlInventory = false;
0 ignored issues
show
Bug Best Practice introduced by
The property ControlInventory does not exist on Dynamic\Foxy\Inventory\T...stOnly\Page\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
68
        $product->PurchaseLimit = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property PurchaseLimit does not exist on Dynamic\Foxy\Inventory\T...stOnly\Page\TestProduct. Since you implemented __set, consider adding a @property annotation.
Loading history...
69
        $this->assertFalse($product->getHasInventory());
0 ignored issues
show
Bug introduced by
The method getHasInventory() does not exist on Dynamic\Foxy\Inventory\T...stOnly\Page\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

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

91
        $this->assertTrue($product->/** @scrutinizer ignore-call */ getIsProductAvailable());
Loading history...
92
        // inventory control, no limit
93
        $product->ControlInventory = true;
94
        $product->PurchaseLimit = 0;
95
        $this->assertTrue($product->getIsProductAvailable());
96
        // inventory control, with limit
97
        $product->ControlInventory = true;
98
        $product->PurchaseLimit = 10;
99
        $this->assertTrue($product->getIsProductAvailable());
100
        /** @var OrderDetail $detail */
101
        $detail = OrderDetail::create();
102
        $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID;
103
        $detail->Quantity = 10;
104
        $detail->ProductID = $product->ID;
105
        $detail->write();
106
        // inventory control, no inventory left
107
        $product->ControlInventory = true;
108
        $product->PurchaseLimit = 10;
109
        $this->assertFalse($product->getIsProductAvailable());
110
    }
111
112
    /**
113
     *
114
     */
115
    public function testGetNumberPurchased()
116
    {
117
        /** @var TestProduct $product */
118
        $product = $this->objFromFixture(TestProduct::class, 'one');
119
        $this->assertEquals(0, $product->getNumberPurchased());
0 ignored issues
show
Bug introduced by
The method getNumberPurchased() does not exist on Dynamic\Foxy\Inventory\T...stOnly\Page\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

119
        $this->assertEquals(0, $product->/** @scrutinizer ignore-call */ getNumberPurchased());
Loading history...
120
        /** @var OrderDetail $detail */
121
        $detail = OrderDetail::create();
122
        $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID;
123
        $detail->Quantity = 10;
124
        $detail->ProductID = $product->ID;
125
        $detail->write();
126
        $this->assertEquals(10, $product->getNumberPurchased());
127
    }
128
129
    /**
130
     *
131
     */
132
    public function testGetOrders()
133
    {
134
        /** @var TestProduct $product */
135
        $product = $this->objFromFixture(TestProduct::class, 'one');
136
        $this->assertEquals(0, $product->getOrders()->Count());
0 ignored issues
show
Bug introduced by
The method getOrders() does not exist on Dynamic\Foxy\Inventory\T...stOnly\Page\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

136
        $this->assertEquals(0, $product->/** @scrutinizer ignore-call */ getOrders()->Count());
Loading history...
137
        /** @var OrderDetail $detail */
138
        $detail = OrderDetail::create();
139
        $detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID;
140
        $detail->Quantity = 10;
141
        $detail->ProductID = $product->ID;
142
        $detail->write();
143
        $this->assertEquals(1, $product->getOrders()->count());
144
    }
145
}
146