CustomProductTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCustomProduct() 0 63 1
1
<?php
2
3
namespace SilverShop\Tests\Model\Product;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverStripe\Dev\FunctionalTest;
7
8
/**
9
 * @package    shop
10
 * @subpackage tests
11
 */
12
class CustomProductTest extends FunctionalTest
13
{
14
    protected static $use_draft_site = true;
15
16
    protected static $extra_dataobjects = [
17
        CustomProduct::class,
18
        CustomProduct_OrderItem::class,
19
    ];
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
        // clear session
25
        ShoppingCart::singleton()->clear();
26
    }
27
28
    public function testCustomProduct()
29
    {
30
        $thing = CustomProduct::create()->update(
31
            [
32
                "Title" => "Thing",
33
                "Price" => 30,
34
            ]
35
        );
36
        $thing->write();
37
38
        $cart = ShoppingCart::singleton();
39
40
        $options1 = ['Color' => 'Green', 'Size' => 5, 'Premium' => true];
41
        $this->assertTrue((bool)$cart->add($thing, 1, $options1), "add to customisation 1 to cart");
42
        $item = $cart->get($thing, $options1);
43
44
        $this->assertTrue((bool)$item, "item with customisation 1 exists");
45
        $this->assertEquals(1, $item->Quantity);
46
47
        $this->assertTrue((bool)$cart->add($thing, 2, $options1), "add another two customisation 1");
48
        $item = $cart->get($thing, $options1);
49
        $this->assertEquals(3, $item->Quantity, "quantity has updated correctly");
50
        $this->assertEquals("Green", $item->Color);
51
        $this->assertEquals(5, $item->Size);
52
        $this->assertEquals(1, $item->Premium); //should be true?
53
54
        $this->assertFalse((bool)$cart->get($thing), "try to get a non-customised product");
55
56
        $options2 = ['Color' => 'Blue', 'Size' => 6, 'Premium' => false];
57
        $this->assertTrue((bool)$cart->add($thing, 5, $options2), "add customisation 2 to cart");
58
        $item = $cart->get($thing, $options2);
59
        $this->assertTrue((bool)$item, "item with customisation 2 exists");
60
        $this->assertEquals(5, $item->Quantity);
61
62
        $options3 = ['Color' => 'Blue'];
63
        $this->assertTrue((bool)$cart->add($thing, 1, $options3), "add a sub-variant of customisation 2");
64
        $item = $cart->get($thing, $options3);
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
65
66
        $this->assertTrue((bool)$cart->add($thing), "add product with no customisation");
67
        $item = $cart->get($thing);
68
69
        $order = $cart->current();
70
        $items = $order->Items();
71
        $this->assertEquals(4, $items->Count(), "4 items in cart");
72
73
        //remove
74
        $cart->remove($thing, 2, $options2);
75
        $item = $cart->get($thing, $options2);
76
        $this->assertNotNull($item, 'item exists in cart');
77
        $this->assertEquals(3, $item->Quantity);
78
79
        $cart->clear();
80
81
        //set quantity
82
        $options4 = ['Size' => 12, 'Color' => 'Blue', 'Premium' => false];
83
        $resp = $cart->setQuantity($thing, 5, $options4);
0 ignored issues
show
Unused Code introduced by
The assignment to $resp is dead and can be removed.
Loading history...
84
85
        $item = $cart->get($thing, $options4);
86
        $this->assertTrue((bool)$item, 'item exists in cart');
87
88
        $this->assertEquals(5, $item->Quantity, "quantity is 5");
89
90
        $this->markTestIncomplete("what about default values that have been set");
91
        //test by using urls
92
        //add a partial match
93
    }
94
}
95