Passed
Push — 1.0 ( 024343...5b8585 )
by Morven
10:07
created

ShoppingCartFactoryTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testRemoveItem() 0 10 1
A testDelete() 0 7 1
A testConstruction() 0 7 1
A setUp() 0 11 1
A testSave() 0 7 1
A testAddItem() 0 17 1
A testUpdateItem() 0 27 1
1
<?php
2
3
namespace SilverCommerce\ShoppingCart\Tests;
4
5
use SilverStripe\ORM\DataList;
6
use SilverStripe\Control\Cookie;
7
use SilverStripe\Control\Session;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverCommerce\Discounts\DiscountFactory;
13
use SilverCommerce\ShoppingCart\ShoppingCartFactory;
14
use SilverCommerce\OrdersAdmin\Model\LineItem;
15
16
class ShoppingCartFactoryTest extends SapphireTest
17
{
18
    /**
19
     * Add some scaffold order records
20
     *
21
     * @var string
22
     */
23
    protected static $fixture_file = 'ShoppingCart.yml';
24
25
    public function setUp()
26
    {
27
        // Ensure we setup a session and the current request
28
        $request = new HTTPRequest('GET', '/');
29
        $session = new Session(null);
30
        $session->init($request);
31
        $request->setSession($session);
32
        Injector::inst()
33
            ->registerService($request, HTTPRequest::class);
34
35
        parent::setUp();
36
    }
37
38
    /**
39
     * Test setting up the cart using ShoppingCartFactory
40
     * 
41
     * @return null
42
     */
43
    public function testConstruction()
44
    {
45
        Cookie::set(ShoppingCartFactory::COOKIE_NAME, "abc123");
46
47
        $cart = ShoppingCartFactory::create()->getCurrent();
48
49
        $this->assertEquals("abc123", $cart->AccessKey);
50
    }
51
52
    /**
53
     * Test adding an item to the shopping cart
54
     * 
55
     * @return null
56
     */
57
    public function testAddItem()
58
    {
59
        Cookie::set(ShoppingCartFactory::COOKIE_NAME, "abc123");
60
61
        $item = LineItem::create([
62
            "Title" => "A stock item",
63
            "Price" => 5.99,
64
            "Quantity" => 2
65
        ]);
66
67
        $cart = ShoppingCartFactory::create()
68
            ->addItem($item)
69
            ->getCurrent();
70
71
        $this->assertEquals(1, $cart->Items()->count());
72
        $this->assertEquals(2, $cart->TotalItems);
73
        $this->assertEquals(11.98, $cart->Total);
74
    }
75
76
    /**
77
     * Test updating an item increases the quantity and that
78
     * adding an existing item calls "update"
79
     * 
80
     * @return null
81
     */
82
    public function testUpdateItem()
83
    {
84
        Cookie::set(ShoppingCartFactory::COOKIE_NAME, "123abc");
85
86
        $cart = ShoppingCartFactory::create();
87
        $item = $cart->getCurrent()->Items()->first();
88
        $cart = $cart->updateItem($item, 2)->getCurrent();
89
90
        $this->assertEquals(1, $cart->Items()->count());
91
        $this->assertEquals(2, $cart->TotalItems);
92
        $this->assertEquals(11.98, $cart->Total);
93
    
94
        // Now test adding an new item that should update
95
        $item = LineItem::create([
96
            "Title" => "A cheap item",
97
            "Price" => 5.99,
98
            "Quantity" => 1,
99
            "StockID" => "Item1"
100
        ]);
101
102
        $cart = ShoppingCartFactory::create()
103
            ->addItem($item)
104
            ->getCurrent();
105
106
        $this->assertEquals(1, $cart->Items()->count());
107
        $this->assertEquals(3, $cart->TotalItems);
108
        $this->assertEquals(17.97, $cart->Total);
109
    }
110
111
    /**
112
     * Test removing an item
113
     * 
114
     * @return null
115
     */
116
    public function testRemoveItem()
117
    {
118
        Cookie::set(ShoppingCartFactory::COOKIE_NAME, "123abc");
119
120
        $cart = ShoppingCartFactory::create();
121
        $item = $cart->getCurrent()->Items()->first();
122
        $cart = $cart->removeItem($item)->getCurrent();
123
124
        $this->assertEquals(0, $cart->Items()->count());
125
        $this->assertEquals(0, $cart->TotalItems);
126
    }
127
128
    /**
129
     * Test deleting the cart
130
     * 
131
     * @return null
132
     */
133
    public function testDelete()
134
    {
135
        Cookie::set(ShoppingCartFactory::COOKIE_NAME, "123abc");
136
137
        $cart = ShoppingCartFactory::create()->delete();
138
139
        $this->assertFalse($cart->getCurrent()->exists());
140
    }
141
142
    /**
143
     * Test saving a new cart
144
     * 
145
     * @return null
146
     */
147
    public function testSave()
148
    {
149
        $cart = ShoppingCartFactory::create()
150
            ->save()
151
            ->getCurrent();
152
153
        $this->assertTrue($cart->exists());
154
    }
155
}